🌐 US-Proxy
class="logged-out env-production page-responsive" style="word-wrap: break-word;" >
Skip to content

Commit e5caec5

Browse files
ondrejmirtesclaude
andcommitted
Fold reasons tip into addTip closure
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent a01080e commit e5caec5

5 files changed

Lines changed: 32 additions & 36 deletions

File tree

src/Rules/Classes/ImpossibleInstanceOfRule.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE
8484
return [];
8585
}
8686

87-
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node): RuleErrorBuilder {
87+
$exprType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node->expr) : $scope->getNativeType($node->expr);
88+
$reasons = $classType->isSuperTypeOf($exprType)->reasons;
89+
90+
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node, $reasons): RuleErrorBuilder {
91+
if ($reasons !== []) {
92+
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder->acceptsReasonsTip($reasons));
93+
}
94+
8895
if (!$this->treatPhpDocTypesAsCertain) {
8996
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
9097
}
@@ -104,19 +111,11 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE
104111
};
105112

106113
if (!$instanceofType->getValue()) {
107-
$exprType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node->expr) : $scope->getNativeType($node->expr);
108-
109-
$errorBuilder = RuleErrorBuilder::message(sprintf(
114+
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
110115
'Instanceof between %s and %s will always evaluate to false.',
111116
$exprType->describe(VerbosityLevel::typeOnly()),
112117
$classType->describe(VerbosityLevel::getRecommendedLevelByType($classType)),
113-
));
114-
$reasons = $classType->isSuperTypeOf($exprType)->reasons;
115-
if ($reasons !== []) {
116-
$errorBuilder = $this->possiblyImpureTipHelper->addTip($scope, $node, $errorBuilder->acceptsReasonsTip($reasons));
117-
} else {
118-
$errorBuilder = $addTip($errorBuilder);
119-
}
118+
)));
120119
$ruleError = $errorBuilder->identifier('instanceof.alwaysFalse')->build();
121120
if ($scope->isInTrait()) {
122121
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $node, false, $ruleError);
@@ -132,7 +131,6 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE
132131
return [];
133132
}
134133

135-
$exprType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node->expr) : $scope->getNativeType($node->expr);
136134
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
137135
'Instanceof between %s and %s will always evaluate to true.',
138136
$exprType->describe(VerbosityLevel::typeOnly()),

src/Rules/Comparison/ImpossibleCheckTypeFunctionCallRule.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE
5353
return [];
5454
}
5555

56-
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node): RuleErrorBuilder {
56+
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node, $reasons): RuleErrorBuilder {
57+
if ($reasons !== []) {
58+
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder->acceptsReasonsTip($reasons));
59+
}
60+
5761
if (!$this->treatPhpDocTypesAsCertain) {
5862
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
5963
}
@@ -72,16 +76,11 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE
7276
};
7377

7478
if (!$isAlways) {
75-
$errorBuilder = RuleErrorBuilder::message(sprintf(
79+
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
7680
'Call to function %s()%s will always evaluate to false.',
7781
$functionName,
7882
$this->impossibleCheckTypeHelper->getArgumentsDescription($scope, $node->getArgs()),
79-
));
80-
if ($reasons !== []) {
81-
$errorBuilder = $this->possiblyImpureTipHelper->addTip($scope, $node, $errorBuilder->acceptsReasonsTip($reasons));
82-
} else {
83-
$errorBuilder = $addTip($errorBuilder);
84-
}
83+
)));
8584
$ruleError = $errorBuilder->identifier('function.impossibleType')->build();
8685
if ($scope->isInTrait()) {
8786
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $node, false, $ruleError);

src/Rules/Comparison/ImpossibleCheckTypeMethodCallRule.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE
5555
return [];
5656
}
5757

58-
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node): RuleErrorBuilder {
58+
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node, $reasons): RuleErrorBuilder {
59+
if ($reasons !== []) {
60+
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder->acceptsReasonsTip($reasons));
61+
}
62+
5963
if (!$this->treatPhpDocTypesAsCertain) {
6064
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
6165
}
@@ -75,17 +79,12 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE
7579

7680
if (!$isAlways) {
7781
$method = $this->getMethod($node->var, $node->name->name, $scope);
78-
$errorBuilder = RuleErrorBuilder::message(sprintf(
82+
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
7983
'Call to method %s::%s()%s will always evaluate to false.',
8084
$method->getDeclaringClass()->getDisplayName(),
8185
$method->getName(),
8286
$this->impossibleCheckTypeHelper->getArgumentsDescription($scope, $node->getArgs()),
83-
));
84-
if ($reasons !== []) {
85-
$errorBuilder = $this->possiblyImpureTipHelper->addTip($scope, $node, $errorBuilder->acceptsReasonsTip($reasons));
86-
} else {
87-
$errorBuilder = $addTip($errorBuilder);
88-
}
87+
)));
8988
$ruleError = $errorBuilder->identifier('method.impossibleType')->build();
9089
if ($scope->isInTrait()) {
9190
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $node, false, $ruleError);

src/Rules/Comparison/ImpossibleCheckTypeStaticMethodCallRule.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE
5555
return [];
5656
}
5757

58-
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node): RuleErrorBuilder {
58+
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node, $reasons): RuleErrorBuilder {
59+
if ($reasons !== []) {
60+
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder->acceptsReasonsTip($reasons));
61+
}
62+
5963
if (!$this->treatPhpDocTypesAsCertain) {
6064
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
6165
}
@@ -76,17 +80,12 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE
7680
if (!$isAlways) {
7781
$method = $this->getMethod($node->class, $node->name->name, $scope);
7882

79-
$errorBuilder = RuleErrorBuilder::message(sprintf(
83+
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
8084
'Call to static method %s::%s()%s will always evaluate to false.',
8185
$method->getDeclaringClass()->getDisplayName(),
8286
$method->getName(),
8387
$this->impossibleCheckTypeHelper->getArgumentsDescription($scope, $node->getArgs()),
84-
));
85-
if ($reasons !== []) {
86-
$errorBuilder = $this->possiblyImpureTipHelper->addTip($scope, $node, $errorBuilder->acceptsReasonsTip($reasons));
87-
} else {
88-
$errorBuilder = $addTip($errorBuilder);
89-
}
88+
)));
9089
$ruleError = $errorBuilder->identifier('staticMethod.impossibleType')->build();
9190
if ($scope->isInTrait()) {
9291
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $node, false, $ruleError);

tests/PHPStan/Rules/Classes/ImpossibleInstanceOfRuleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ public function testBug13469(): void
620620
[
621621
sprintf('Instanceof between Bug13469\Foo and Stringable will always evaluate to %s.', PHP_VERSION_ID >= 80000 ? 'true' : 'false'),
622622
23,
623+
PHP_VERSION_ID < 80000 ? 'Final class Bug13469\Foo does not implement interface Stringable.' : null,
623624
],
624625
]);
625626
}

0 commit comments

Comments
 (0)