-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathImpossibleInstanceOfRule.php
More file actions
149 lines (128 loc) · 5.23 KB
/
ImpossibleInstanceOfRule.php
File metadata and controls
149 lines (128 loc) · 5.23 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Classes;
use PhpParser\Node;
use PHPStan\Analyser\CollectedDataEmitter;
use PHPStan\Analyser\NodeCallbackInvoker;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Parser\LastConditionVisitor;
use PHPStan\Rules\Comparison\ConstantConditionInTraitHelper;
use PHPStan\Rules\Comparison\PossiblyImpureTipHelper;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function sprintf;
/**
* @implements Rule<Node\Expr\Instanceof_>
*/
#[RegisteredRule(level: 4)]
final class ImpossibleInstanceOfRule implements Rule
{
public function __construct(
private RuleLevelHelper $ruleLevelHelper,
private PossiblyImpureTipHelper $possiblyImpureTipHelper,
private ConstantConditionInTraitHelper $constantConditionInTraitHelper,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
#[AutowiredParameter]
private bool $reportAlwaysTrueInLastCondition,
#[AutowiredParameter(ref: '%tips.treatPhpDocTypesAsCertain%')]
private bool $treatPhpDocTypesAsCertainTip,
)
{
}
public function getNodeType(): string
{
return Node\Expr\Instanceof_::class;
}
public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataEmitter $scope): array
{
if ($node->class instanceof Node\Name) {
$className = $scope->resolveName($node->class);
$classType = new ObjectType($className);
} else {
$classType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node->class) : $scope->getNativeType($node->class);
$allowed = new UnionType([
new StringType(),
new ObjectWithoutClassType(),
]);
$typeResult = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->class,
'',
static fn (Type $type): bool => !$allowed->isSuperTypeOf($type)->yes(),
);
if (!$typeResult->getType() instanceof ErrorType && !$allowed->isSuperTypeOf($typeResult->getType())->yes()) {
return [
RuleErrorBuilder::message(sprintf(
'Instanceof between %s and %s results in an error.',
$scope->getType($node->expr)->describe(VerbosityLevel::typeOnly()),
$classType->describe(VerbosityLevel::typeOnly()),
))->identifier('instanceof.invalidExprType')->build(),
];
}
}
$instanceofType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node) : $scope->getNativeType($node);
if (!$instanceofType instanceof ConstantBooleanType) {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $node);
return [];
}
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
}
$instanceofTypeWithoutPhpDocs = $scope->getNativeType($node);
if ($instanceofTypeWithoutPhpDocs instanceof ConstantBooleanType) {
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
}
if (!$this->treatPhpDocTypesAsCertainTip) {
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
}
$ruleErrorBuilder = $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
};
if (!$instanceofType->getValue()) {
$exprType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node->expr) : $scope->getNativeType($node->expr);
$ruleError = $addTip(RuleErrorBuilder::message(sprintf(
'Instanceof between %s and %s will always evaluate to false.',
$exprType->describe(VerbosityLevel::typeOnly()),
$classType->describe(VerbosityLevel::getRecommendedLevelByType($classType)),
)))->identifier('instanceof.alwaysFalse')->build();
if ($scope->isInTrait()) {
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $node, false, $ruleError);
return [];
}
return [$ruleError];
}
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($isLast === true && !$this->reportAlwaysTrueInLastCondition) {
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $node);
return [];
}
$exprType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node->expr) : $scope->getNativeType($node->expr);
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Instanceof between %s and %s will always evaluate to true.',
$exprType->describe(VerbosityLevel::typeOnly()),
$classType->describe(VerbosityLevel::getRecommendedLevelByType($classType)),
)));
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}
$errorBuilder->identifier('instanceof.alwaysTrue');
$ruleError = $errorBuilder->build();
if ($scope->isInTrait()) {
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $node, true, $ruleError);
return [];
}
return [$ruleError];
}
}