Skip to content

Commit 47579a5

Browse files
phpstan-botclaude
andcommitted
Use NodeFinder to detect $this-dependent expressions in trait context
Replace manual recursive traversal in isExpressionDependentOnThis() with NodeFinder::findFirst(), which handles all AST node types automatically via the php-parser traverser. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 53eb552 commit 47579a5

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use PhpParser\Node\Expr\FuncCall;
99
use PhpParser\Node\Expr\MethodCall;
1010
use PhpParser\Node\Expr\StaticCall;
11+
use PhpParser\Node\Expr\Variable;
12+
use PhpParser\Node\Name;
13+
use PhpParser\NodeFinder;
1114
use PHPStan\Analyser\MutatingScope;
1215
use PHPStan\Analyser\Scope;
1316
use PHPStan\Analyser\TypeSpecifier;
@@ -421,28 +424,22 @@ private function isExpressionDependentOnTraitContext(Scope $scope, Expr $expr):
421424

422425
private static function isExpressionDependentOnThis(Expr $expr): bool
423426
{
424-
if ($expr instanceof Expr\Variable && $expr->name === 'this') {
425-
return true;
426-
}
427-
428-
if ($expr instanceof Expr\PropertyFetch || $expr instanceof Expr\NullsafePropertyFetch) {
429-
return self::isExpressionDependentOnThis($expr->var);
430-
}
431-
432-
if ($expr instanceof Expr\MethodCall || $expr instanceof Expr\NullsafeMethodCall) {
433-
return self::isExpressionDependentOnThis($expr->var);
434-
}
435-
436-
if ($expr instanceof Expr\StaticPropertyFetch || $expr instanceof Expr\StaticCall) {
437-
if ($expr->class instanceof Expr) {
438-
return self::isExpressionDependentOnThis($expr->class);
427+
$nodeFinder = new NodeFinder();
428+
return $nodeFinder->findFirst([$expr], static function (Node $node): bool {
429+
if ($node instanceof Variable && $node->name === 'this') {
430+
return true;
439431
}
440432

441-
$className = $expr->class->toString();
442-
return in_array($className, ['self', 'static', 'parent'], true);
443-
}
433+
if (
434+
($node instanceof Expr\StaticPropertyFetch || $node instanceof Expr\StaticCall)
435+
&& $node->class instanceof Name
436+
&& in_array($node->class->toString(), ['self', 'static', 'parent'], true)
437+
) {
438+
return true;
439+
}
444440

445-
return false;
441+
return false;
442+
}) !== null;
446443
}
447444

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

0 commit comments

Comments
 (0)