Skip to content

Commit 037cf65

Browse files
authored
Add early return on NodeAttributeReIndexer (#6604)
* Add early return on NodeAttributeReIndexer * Add early return on NodeAttributeReIndexer * move reindex
1 parent 4cafa91 commit 037cf65

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/Application/NodeAttributeReIndexer.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,55 @@
1414
use PhpParser\Node\Expr\StaticCall;
1515
use PhpParser\Node\FunctionLike;
1616
use PhpParser\Node\MatchArm;
17+
use PhpParser\Node\Stmt\ClassLike;
1718
use PhpParser\Node\Stmt\ClassMethod;
19+
use PhpParser\Node\Stmt\Declare_;
1820
use PhpParser\Node\Stmt\Function_;
1921
use PhpParser\Node\Stmt\If_;
2022
use PhpParser\Node\Stmt\Switch_;
2123
use PhpParser\Node\Stmt\TryCatch;
24+
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
2225

2326
final class NodeAttributeReIndexer
2427
{
2528
public static function reIndexNodeAttributes(Node $node): ?Node
2629
{
27-
if ($node instanceof FunctionLike) {
28-
/** @var ClassMethod|Function_|Closure $node */
29-
$node->params = array_values($node->params);
30+
if (($node instanceof StmtsAwareInterface || $node instanceof ClassLike || $node instanceof Declare_) && $node->stmts !== null) {
31+
$node->stmts = array_values($node->stmts);
3032

31-
if ($node instanceof Closure) {
32-
$node->uses = array_values($node->uses);
33+
if ($node instanceof If_) {
34+
$node->elseifs = array_values($node->elseifs);
35+
return $node;
3336
}
37+
38+
if ($node instanceof TryCatch) {
39+
$node->catches = array_values($node->catches);
40+
return $node;
41+
}
42+
43+
return $node;
3444
}
3545

3646
if ($node instanceof CallLike) {
3747
/** @var FuncCall|MethodCall|New_|NullsafeMethodCall|StaticCall $node */
3848
$node->args = array_values($node->args);
49+
return $node;
3950
}
4051

41-
if ($node instanceof If_) {
42-
$node->elseifs = array_values($node->elseifs);
43-
}
52+
if ($node instanceof FunctionLike) {
53+
/** @var ClassMethod|Function_|Closure $node */
54+
$node->params = array_values($node->params);
55+
56+
if ($node instanceof Closure) {
57+
$node->uses = array_values($node->uses);
58+
}
4459

45-
if ($node instanceof TryCatch) {
46-
$node->catches = array_values($node->catches);
60+
return $node;
4761
}
4862

4963
if ($node instanceof Switch_) {
5064
$node->cases = array_values($node->cases);
65+
return $node;
5166
}
5267

5368
if ($node instanceof MatchArm && is_array($node->conds)) {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public function enterNode(Node $node): ?Node
2424
return null;
2525
}
2626

27-
$node->stmts = array_values($node->stmts);
28-
2927
// re-index stmt key under current node
3028
foreach ($node->stmts as $key => $childStmt) {
3129
$childStmt->setAttribute(AttributeKey::STMT_KEY, $key);

0 commit comments

Comments
 (0)