Skip to content

Commit c2f5250

Browse files
phpstan-botclaude
andcommitted
Use NodeFinder to deduplicate assignable AST node traversal
Replace manual recursive traversal of PropertyFetch, MethodCall, NullsafePropertyFetch, NullsafeMethodCall, StaticPropertyFetch, and StaticCall in isExpressionDependentOnThis() with NodeFinder (which uses NodeTraverser+NodeVisitor internally). This eliminates the per-node-type recursion and makes the traversal reusable. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 38e8ef2 commit c2f5250

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Expr\FuncCall;
99
use PhpParser\Node\Expr\MethodCall;
1010
use PhpParser\Node\Expr\StaticCall;
11+
use PhpParser\NodeFinder;
1112
use PHPStan\Analyser\MutatingScope;
1213
use PHPStan\Analyser\Scope;
1314
use PHPStan\Analyser\TypeSpecifier;
@@ -408,28 +409,21 @@ private function isExpressionDependentOnTraitContext(Scope $scope, Expr $expr):
408409

409410
private static function isExpressionDependentOnThis(Expr $expr): bool
410411
{
411-
if ($expr instanceof Expr\Variable && $expr->name === 'this') {
412-
return true;
413-
}
414-
415-
if ($expr instanceof Expr\PropertyFetch || $expr instanceof Expr\NullsafePropertyFetch) {
416-
return self::isExpressionDependentOnThis($expr->var);
417-
}
418-
419-
if ($expr instanceof Expr\MethodCall || $expr instanceof Expr\NullsafeMethodCall) {
420-
return self::isExpressionDependentOnThis($expr->var);
421-
}
422-
423-
if ($expr instanceof Expr\StaticPropertyFetch || $expr instanceof Expr\StaticCall) {
424-
if ($expr->class instanceof Expr) {
425-
return self::isExpressionDependentOnThis($expr->class);
412+
return (new NodeFinder())->findFirst($expr, static function (Node $node): bool {
413+
if ($node instanceof Expr\Variable && $node->name === 'this') {
414+
return true;
426415
}
427416

428-
$className = $expr->class->toString();
429-
return in_array($className, ['self', 'static', 'parent'], true);
430-
}
417+
if (
418+
($node instanceof Expr\StaticPropertyFetch || $node instanceof Expr\StaticCall)
419+
&& !$node->class instanceof Expr
420+
&& in_array($node->class->toString(), ['self', 'static', 'parent'], true)
421+
) {
422+
return true;
423+
}
431424

432-
return false;
425+
return false;
426+
}) !== null;
433427
}
434428

435429
private static function isSpecified(Scope $scope, Expr $node, Expr $expr): bool

0 commit comments

Comments
 (0)