Skip to content

Commit b5ea28c

Browse files
committed
update custom vistors
1 parent aea9099 commit b5ea28c

File tree

6 files changed

+21
-7
lines changed

6 files changed

+21
-7
lines changed

rules/CodeQuality/Rector/FunctionLike/SimplifyUselessVariableRector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PhpParser\Node;
99
use PhpParser\Node\Expr\Assign;
1010
use PhpParser\Node\Expr\AssignOp;
11-
use PhpParser\Node\Expr\Closure;
1211
use PhpParser\Node\Expr\Ternary;
1312
use PhpParser\Node\Expr\Variable;
1413
use PhpParser\Node\Stmt;

rules/CodeQuality/Rector/If_/SimplifyIfNotNullReturnRector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
9-
use PhpParser\Node\Stmt;
109
use PhpParser\Node\Stmt\Else_;
1110
use PhpParser\Node\Stmt\If_;
1211
use PhpParser\Node\Stmt\Return_;

src/NodeManipulator/StmtsManipulator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
9-
use PhpParser\Node\Expr\Closure;
109
use PhpParser\Node\Expr\Variable;
1110
use PhpParser\Node\Stmt;
1211
use PhpParser\Node\Stmt\ClassMethod;

src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
use PhpParser\Node\Stmt\Global_;
1313
use PhpParser\NodeVisitor;
1414
use PhpParser\NodeVisitorAbstract;
15-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1615
use Rector\NodeTypeResolver\Node\AttributeKey;
1716
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
1817
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
18+
use Rector\PhpParser\NodeGroups;
19+
use Webmozart\Assert\Assert;
1920

2021
final class GlobalVariableNodeVisitor extends NodeVisitorAbstract implements ScopeResolverNodeVisitorInterface
2122
{
@@ -26,10 +27,12 @@ public function __construct(
2627

2728
public function enterNode(Node $node): ?Node
2829
{
29-
if (! $node instanceof StmtsAwareInterface) {
30+
if (! NodeGroups::matchesStmtsAware($node)) {
3031
return null;
3132
}
3233

34+
Assert::propertyExists($node, 'stmts');
35+
3336
if ($node->stmts === null) {
3437
return null;
3538
}

src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StaticVariableNodeVisitor.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
use PhpParser\Node\Stmt\Static_;
1313
use PhpParser\NodeVisitor;
1414
use PhpParser\NodeVisitorAbstract;
15-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1615
use Rector\NodeTypeResolver\Node\AttributeKey;
1716
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
1817
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
18+
use Rector\PhpParser\NodeGroups;
19+
use Webmozart\Assert\Assert;
1920

2021
final class StaticVariableNodeVisitor extends NodeVisitorAbstract implements ScopeResolverNodeVisitorInterface
2122
{
@@ -26,10 +27,12 @@ public function __construct(
2627

2728
public function enterNode(Node $node): ?Node
2829
{
29-
if (! $node instanceof StmtsAwareInterface) {
30+
if (! NodeGroups::matchesStmtsAware($node)) {
3031
return null;
3132
}
3233

34+
Assert::propertyExists($node, 'stmts');
35+
3336
if ($node->stmts === null) {
3437
return null;
3538
}

src/PhpParser/NodeGroups.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,15 @@ final class NodeGroups
3131
// custom Rector node
3232
FileWithoutNamespace::class,
3333
];
34+
35+
public static function matchesStmtsAware(\PhpParser\Node $node): bool
36+
{
37+
foreach (self::STMTS_AWARE_NODES as $stmtAwareNodeClass) {
38+
if (is_a($node, $stmtAwareNodeClass)) {
39+
return true;
40+
}
41+
}
42+
43+
return false;
44+
}
3445
}

0 commit comments

Comments
 (0)