Skip to content

Commit d5e8535

Browse files
authored
Ensure reset key of stmts/args before returning node when there is unset by key (#424)
* Ensure reset key of stmts/args before returning node * fix phpstan * fix phpstan * cs fix
1 parent ca9c906 commit d5e8535

11 files changed

+26
-5
lines changed

src/NodeAnalyzer/ApplicationAnalyzer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class ApplicationAnalyzer
99
{
1010
private ?string $version = null;
1111

12+
/**
13+
* @param class-string $applicationClass
14+
*/
1215
public function __construct(
1316
private string $applicationClass = 'Illuminate\Foundation\Application',
1417
) {}
@@ -20,13 +23,19 @@ public function setVersion(?string $version): static
2023
return $this;
2124
}
2225

26+
/**
27+
* @param class-string $applicationClass
28+
*/
2329
public function setApplicationClass(string $applicationClass): static
2430
{
2531
$this->applicationClass = $applicationClass;
2632

2733
return $this;
2834
}
2935

36+
/**
37+
* @return class-string $applicationClass
38+
*/
3039
public function getApplicationClass(): string
3140
{
3241
return $this->applicationClass;

src/Rector/ClassMethod/AddParentBootToModelClassMethodRector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function refactor(Node $node): ?Node
115115
$parentStaticCallExpression = new Expression($staticCall);
116116

117117
$node->stmts = array_merge([$parentStaticCallExpression], (array) $node->stmts);
118+
$node->stmts = array_values($node->stmts);
118119

119120
return $node;
120121
}

src/Rector/ClassMethod/AddParentRegisterToEventServiceProviderRector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public function refactor(Node $node): ?Node
111111
$parentStaticCallExpression = new Expression($staticCall);
112112

113113
$node->stmts = array_merge([$parentStaticCallExpression], (array) $node->stmts);
114+
$node->stmts = array_values($node->stmts);
114115

115116
return $node;
116117
}

src/Rector/ClassMethod/MigrateToSimplifiedAttributeRector.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ public function refactor(Node $node): ?Node
7777
}
7878
}
7979

80-
return $hasChanged
81-
? $node
82-
: null;
80+
if ($hasChanged) {
81+
$node->stmts = array_values($node->stmts);
8382

83+
return $node;
84+
}
85+
86+
return null;
8487
}
8588

8689
public function getRuleDefinition(): RuleDefinition

src/Rector/Class_/ModelCastsPropertyToCastsMethodRector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public function refactor(Node $node): ?Class_
100100
$method->addStmt(new Return_($stmt->props[0]->default));
101101
$methodNode = $method->getNode();
102102
$node->stmts[] = $methodNode;
103+
$node->stmts = array_values($node->stmts);
103104

104105
$this->restorePhpDoc($methodNode);
105106

src/Rector/Class_/PropertyDeferToDeferrableProviderToRector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function refactor(Node $node): ?Node
8080
}
8181

8282
unset($node->stmts[array_search($deferProperty, $node->stmts, true)]);
83+
$node->stmts = array_values($node->stmts);
8384

8485
$node->implements[] = new FullyQualified('Illuminate\Contracts\Support\DeferrableProvider');
8586

src/Rector/Class_/RemoveModelPropertyFromFactoriesRector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function refactor(Node $node): ?Node
7171
}
7272

7373
unset($node->stmts[$index]);
74+
$node->stmts = array_values($node->stmts);
7475

7576
break;
7677
}

src/Rector/Class_/UnifyModelDatesWithCastsRector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function getNodeTypes(): array
7676
/**
7777
* @param Class_ $node
7878
*/
79-
public function refactor(Node $node)
79+
public function refactor(Node $node): ?Node
8080
{
8181
if (! $this->isObjectType($node, new ObjectType('Illuminate\Database\Eloquent\Model'))) {
8282
return null;
@@ -128,8 +128,9 @@ public function refactor(Node $node)
128128
}
129129

130130
unset($node->stmts[array_search($datesProperty, $node->stmts, true)]);
131+
$node->stmts = array_values($node->stmts);
131132

132-
return null;
133+
return $node;
133134
}
134135

135136
private function createCastsProperty(): Property

src/Rector/MethodCall/WhereToWhereLikeRector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public function refactor(Node $node): ?Node
125125

126126
// Remove the second argument (the 'like' operator)
127127
unset($node->args[1]);
128+
$node->args = array_values($node->args);
128129

129130
return $node;
130131
}

src/Rector/Namespace_/FactoryDefinitionRector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public function refactor(Node $node): ?Node
141141
}
142142

143143
$node->stmts = array_merge($node->stmts, $factories);
144+
$node->stmts = array_values($node->stmts);
144145

145146
return $node;
146147
}

0 commit comments

Comments
 (0)