Skip to content

Commit e6a5b1a

Browse files
committed
remove rest of instanceof checks
1 parent 646c2c8 commit e6a5b1a

8 files changed

Lines changed: 41 additions & 23 deletions

File tree

phpstan.neon

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ parameters:
3434
scanDirectories:
3535
- stubs
3636

37-
scanFiles:
38-
- src/Contract/PhpParser/Node/StmtsAwareInterface.php
37+
# scanFiles:
38+
# - src/Contract/PhpParser/Node/StmtsAwareInterface.php
3939

4040
excludePaths:
4141
- '*tests/*/Fixture/*'
@@ -211,7 +211,7 @@ parameters:
211211
message: '#Function "(class_exists|interface_exists)\(\)" cannot be used/left in the code\: use ReflectionProvider\->has\*\(\) instead#'
212212
path: src/Skipper/SkipCriteriaResolver/SkippedClassResolver.php
213213

214-
- '#(.*?)\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts#'
214+
# - '#(.*?)\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts#'
215215

216216
# dev rule
217217
- '#Class "Rector\\Utils\\Rector\\MoveAbstractRectorToChildrenRector" is missing @see annotation with test case class reference#'
@@ -384,4 +384,3 @@ parameters:
384384
message: '#Method (.*?)refactor\(\) (never returns|should return) (.*?)#'
385385
paths:
386386
- rules/Php70/Rector/If_/IfToSpaceshipRector.php
387-
- rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php

preload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function isPHPStanTestPreloaded(): bool
2626
}
2727

2828
require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node.php';
29+
// @todo to be deprecated
2930
require_once __DIR__ . '/src/Contract/PhpParser/Node/StmtsAwareInterface.php';
3031
require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NodeAbstract.php';
3132
require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr.php';

rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
use PhpParser\Node\Stmt\Expression;
2020
use PhpParser\Node\Stmt\Function_;
2121
use PhpParser\NodeVisitor;
22-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
2322
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
2423
use Rector\NodeAnalyzer\VariableAnalyzer;
2524
use Rector\NodeManipulator\StmtsManipulator;
2625
use Rector\Php\ReservedKeywordAnalyzer;
26+
use Rector\PhpParser\Enum\NodeGroup;
2727
use Rector\PhpParser\Node\BetterNodeFinder;
2828
use Rector\Rector\AbstractRector;
2929
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -164,20 +164,17 @@ private function shouldSkip(array $stmts): bool
164164
*/
165165
private function collectAssignRefVariableNames(Stmt $stmt, array &$refVariableNames): void
166166
{
167-
if (! $stmt instanceof StmtsAwareInterface) {
167+
if (! NodeGroup::isStmtAwareNode($stmt)) {
168168
return;
169169
}
170170

171-
$this->traverseNodesWithCallable(
172-
$stmt,
173-
function (Node $subNode) use (&$refVariableNames): Node {
174-
if ($subNode instanceof AssignRef && $subNode->var instanceof Variable) {
175-
$refVariableNames[] = (string) $this->getName($subNode->var);
176-
}
177-
178-
return $subNode;
171+
$this->traverseNodesWithCallable($stmt, function (Node $subNode) use (&$refVariableNames): Node {
172+
if ($subNode instanceof AssignRef && $subNode->var instanceof Variable) {
173+
$refVariableNames[] = (string) $this->getName($subNode->var);
179174
}
180-
);
175+
176+
return $subNode;
177+
});
181178
}
182179

183180
/**

src/Application/NodeAttributeReIndexer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
use PhpParser\Node\Stmt\If_;
2222
use PhpParser\Node\Stmt\Switch_;
2323
use PhpParser\Node\Stmt\TryCatch;
24-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
24+
use Rector\PhpParser\Enum\NodeGroup;
25+
use Webmozart\Assert\Assert;
2526

2627
final class NodeAttributeReIndexer
2728
{
@@ -66,10 +67,13 @@ public static function reIndexNodeAttributes(Node $node): ?Node
6667

6768
private static function reIndexStmtsKeys(Node $node): ?Node
6869
{
69-
if (! $node instanceof StmtsAwareInterface && ! $node instanceof ClassLike && ! $node instanceof Declare_ && ! $node instanceof Block) {
70+
if (! NodeGroup::isStmtAwareNode(
71+
$node
72+
) && ! $node instanceof ClassLike && ! $node instanceof Declare_ && ! $node instanceof Block) {
7073
return null;
7174
}
7275

76+
Assert::propertyExists($node, 'stmts');
7377
if ($node->stmts === null) {
7478
return null;
7579
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
use PhpParser\NodeVisitor;
1414
use PhpParser\NodeVisitorAbstract;
1515
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
16-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1716
use Rector\NodeTypeResolver\Node\AttributeKey;
1817
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
18+
use Rector\PhpParser\Enum\NodeGroup;
19+
use Webmozart\Assert\Assert;
1920

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

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

34+
Assert::propertyExists($node, 'stmts');
3335
if ($node->stmts === null) {
3436
return null;
3537
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
use PhpParser\NodeVisitor;
1414
use PhpParser\NodeVisitorAbstract;
1515
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
16-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1716
use Rector\NodeTypeResolver\Node\AttributeKey;
1817
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
18+
use Rector\PhpParser\Enum\NodeGroup;
19+
use Webmozart\Assert\Assert;
1920

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

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

34+
Assert::propertyExists($node, 'stmts');
3335
if ($node->stmts === null) {
3436
return null;
3537
}

src/PhpParser/Enum/NodeGroup.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,15 @@ final class NodeGroup
7575
Interface_::class,
7676
Switch_::class,
7777
];
78+
79+
public static function isStmtAwareNode(Node $node): bool
80+
{
81+
foreach (self::STMTS_AWARE as $stmtAwareClass) {
82+
if (is_a($node, $stmtAwareClass, true)) {
83+
return true;
84+
}
85+
}
86+
87+
return false;
88+
}
7889
}

tests/BetterPhpDocParser/PhpDoc/ArrayItemNode/ArrayItemNodeTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1111
use Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter;
1212
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
13-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1413
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
14+
use Rector\PhpParser\Enum\NodeGroup;
1515
use Rector\PhpParser\Parser\RectorParser;
1616
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
17+
use Webmozart\Assert\Assert;
1718

1819
final class ArrayItemNodeTest extends AbstractLazyTestCase
1920
{
@@ -51,10 +52,11 @@ public function testUpdateNestedClassAnnotation(): void
5152
$classDocComment = null;
5253

5354
foreach ($newStmts as $newStmt) {
54-
if (! $newStmt instanceof StmtsAwareInterface) {
55+
if (! NodeGroup::isStmtAwareNode($newStmt)) {
5556
continue;
5657
}
5758

59+
Assert::propertyExists($newStmt, 'stmts');
5860
if ($newStmt->stmts === null) {
5961
continue;
6062
}

0 commit comments

Comments
 (0)