Skip to content

Commit 3c1b698

Browse files
phpstan-botclaude
andcommitted
Refactor ThisDependentNodeVisitor with reusable static methods
Extract isThisVariable() and isStaticClassReference() as public static utility methods, and add containsThisDependentExpression() convenience method. Reuse isThisVariable() in InvalidAssignVarRule instead of inline $this check. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 783a313 commit 3c1b698

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PhpParser\Node\Expr\FuncCall;
99
use PhpParser\Node\Expr\MethodCall;
1010
use PhpParser\Node\Expr\StaticCall;
11-
use PhpParser\NodeTraverser;
1211
use PHPStan\Analyser\MutatingScope;
1312
use PHPStan\Analyser\Scope;
1413
use PHPStan\Analyser\TypeSpecifier;
@@ -422,12 +421,7 @@ private function isExpressionDependentOnTraitContext(Scope $scope, Expr $expr):
422421

423422
private static function isExpressionDependentOnThis(Expr $expr): bool
424423
{
425-
$visitor = new ThisDependentNodeVisitor();
426-
$traverser = new NodeTraverser();
427-
$traverser->addVisitor($visitor);
428-
$traverser->traverse([$expr]);
429-
430-
return $visitor->isFound();
424+
return ThisDependentNodeVisitor::containsThisDependentExpression($expr);
431425
}
432426

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

src/Rules/Comparison/ThisDependentNodeVisitor.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node\Expr;
88
use PhpParser\Node\Expr\Variable;
99
use PhpParser\Node\Name;
10+
use PhpParser\NodeTraverser;
1011
use PhpParser\NodeVisitorAbstract;
1112
use function in_array;
1213

@@ -15,6 +16,28 @@ final class ThisDependentNodeVisitor extends NodeVisitorAbstract
1516

1617
private bool $found = false;
1718

19+
public static function isThisVariable(Node $node): bool
20+
{
21+
return $node instanceof Variable && $node->name === 'this';
22+
}
23+
24+
public static function isStaticClassReference(Node $node): bool
25+
{
26+
return ($node instanceof Expr\StaticPropertyFetch || $node instanceof Expr\StaticCall)
27+
&& $node->class instanceof Name
28+
&& in_array($node->class->toString(), ['self', 'static', 'parent'], true);
29+
}
30+
31+
public static function containsThisDependentExpression(Expr $expr): bool
32+
{
33+
$visitor = new self();
34+
$traverser = new NodeTraverser();
35+
$traverser->addVisitor($visitor);
36+
$traverser->traverse([$expr]);
37+
38+
return $visitor->isFound();
39+
}
40+
1841
#[Override]
1942
public function beforeTraverse(array $nodes): ?array
2043
{
@@ -25,16 +48,7 @@ public function beforeTraverse(array $nodes): ?array
2548
#[Override]
2649
public function enterNode(Node $node): ?int
2750
{
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-
) {
51+
if (self::isThisVariable($node) || self::isStaticClassReference($node)) {
3852
$this->found = true;
3953
return self::STOP_TRAVERSAL;
4054
}

src/Rules/Operators/InvalidAssignVarRule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\Node\Expr\AssignRef;
1010
use PHPStan\Analyser\Scope;
1111
use PHPStan\DependencyInjection\RegisteredRule;
12+
use PHPStan\Rules\Comparison\ThisDependentNodeVisitor;
1213
use PHPStan\Rules\NullsafeCheck;
1314
use PHPStan\Rules\Rule;
1415
use PHPStan\Rules\RuleErrorBuilder;
@@ -72,7 +73,7 @@ public function processNode(Node $node, Scope $scope): array
7273
private function containsNonAssignableExpression(Expr $expr): bool
7374
{
7475
if ($expr instanceof Expr\Variable) {
75-
return $expr->name === 'this';
76+
return ThisDependentNodeVisitor::isThisVariable($expr);
7677
}
7778

7879
if ($expr instanceof Expr\PropertyFetch) {

0 commit comments

Comments
 (0)