-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathInstanceofHandler.php
More file actions
116 lines (102 loc) · 3.72 KB
/
InstanceofHandler.php
File metadata and controls
116 lines (102 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php declare(strict_types = 1);
namespace PHPStan\Analyser\ExprHandler;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\ExpressionResult;
use PHPStan\Analyser\ExpressionResultStorage;
use PHPStan\Analyser\ExprHandler;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\Traverser\InstanceOfClassTypeTraverser;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
use function array_merge;
use function strtolower;
/**
* @implements ExprHandler<Instanceof_>
*/
#[AutowiredService]
final class InstanceofHandler implements ExprHandler
{
public function supports(Expr $expr): bool
{
return $expr instanceof Instanceof_;
}
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
{
$exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep());
$hasYield = $exprResult->hasYield();
$throwPoints = $exprResult->getThrowPoints();
$impurePoints = $exprResult->getImpurePoints();
$isAlwaysTerminating = $exprResult->isAlwaysTerminating();
$scope = $exprResult->getScope();
if (!$expr->class instanceof Name) {
$classResult = $nodeScopeResolver->processExprNode($stmt, $expr->class, $scope, $storage, $nodeCallback, $context->enterDeep());
$scope = $classResult->getScope();
$hasYield = $hasYield || $classResult->hasYield();
$throwPoints = array_merge($throwPoints, $classResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $classResult->getImpurePoints());
$isAlwaysTerminating = $isAlwaysTerminating || $classResult->isAlwaysTerminating();
}
return new ExpressionResult(
$scope,
hasYield: $hasYield,
isAlwaysTerminating: $isAlwaysTerminating,
throwPoints: $throwPoints,
impurePoints: $impurePoints,
truthyScopeCallback: static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
falseyScopeCallback: static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
);
}
public function resolveType(MutatingScope $scope, Expr $expr): Type
{
$expressionType = $scope->getType($expr->expr);
if (
$scope->isInTrait()
&& TypeUtils::findThisType($expressionType) !== null
) {
return new BooleanType();
}
if ($expressionType->isNever()->yes()) {
return new ConstantBooleanType(false);
}
$uncertainty = false;
if ($expr->class instanceof Name) {
$unresolvedClassName = $expr->class->toString();
if (
strtolower($unresolvedClassName) === 'static'
&& $scope->isInClass()
) {
$classType = new StaticType($scope->getClassReflection());
} else {
$className = $scope->resolveName($expr->class);
$classType = new ObjectType($className);
}
} else {
$classType = $scope->getType($expr->class);
$traverser = new InstanceOfClassTypeTraverser();
$classType = TypeTraverser::map($classType, $traverser);
$uncertainty = $traverser->getUncertainty();
}
if ($classType->isSuperTypeOf(new MixedType())->yes()) {
return new BooleanType();
}
$isSuperType = $classType->isSuperTypeOf($expressionType);
if ($isSuperType->no()) {
return new ConstantBooleanType(false);
} elseif ($isSuperType->yes() && !$uncertainty) {
return new ConstantBooleanType(true);
}
return new BooleanType();
}
}