Skip to content

Commit 783a313

Browse files
phpstan-botclaude
andcommitted
Extract dedicated ThisDependentNodeVisitor class
Replace inline NodeFinder usage with a dedicated NodeVisitor class for detecting $this-dependent expressions in trait context. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 47579a5 commit 783a313

File tree

2 files changed

+56
-18
lines changed

2 files changed

+56
-18
lines changed

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
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;
11+
use PhpParser\NodeTraverser;
1412
use PHPStan\Analyser\MutatingScope;
1513
use PHPStan\Analyser\Scope;
1614
use PHPStan\Analyser\TypeSpecifier;
@@ -424,22 +422,12 @@ private function isExpressionDependentOnTraitContext(Scope $scope, Expr $expr):
424422

425423
private static function isExpressionDependentOnThis(Expr $expr): bool
426424
{
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;
431-
}
432-
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-
}
425+
$visitor = new ThisDependentNodeVisitor();
426+
$traverser = new NodeTraverser();
427+
$traverser->addVisitor($visitor);
428+
$traverser->traverse([$expr]);
440429

441-
return false;
442-
}) !== null;
430+
return $visitor->isFound();
443431
}
444432

445433
private static function isSpecified(Scope $scope, Expr $node, Expr $expr): bool
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Comparison;
4+
5+
use Override;
6+
use PhpParser\Node;
7+
use PhpParser\Node\Expr;
8+
use PhpParser\Node\Expr\Variable;
9+
use PhpParser\Node\Name;
10+
use PhpParser\NodeVisitorAbstract;
11+
use function in_array;
12+
13+
final class ThisDependentNodeVisitor extends NodeVisitorAbstract
14+
{
15+
16+
private bool $found = false;
17+
18+
#[Override]
19+
public function beforeTraverse(array $nodes): ?array
20+
{
21+
$this->found = false;
22+
return null;
23+
}
24+
25+
#[Override]
26+
public function enterNode(Node $node): ?int
27+
{
28+
if ($node instanceof Variable && $node->name === 'this') {
29+
$this->found = true;
30+
return self::STOP_TRAVERSAL;
31+
}
32+
33+
if (
34+
($node instanceof Expr\StaticPropertyFetch || $node instanceof Expr\StaticCall)
35+
&& $node->class instanceof Name
36+
&& in_array($node->class->toString(), ['self', 'static', 'parent'], true)
37+
) {
38+
$this->found = true;
39+
return self::STOP_TRAVERSAL;
40+
}
41+
42+
return null;
43+
}
44+
45+
public function isFound(): bool
46+
{
47+
return $this->found;
48+
}
49+
50+
}

0 commit comments

Comments
 (0)