Skip to content

Commit 67e6bf0

Browse files
committed
Fix
1 parent 0cd6f1f commit 67e6bf0

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

rules/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public function refactor(Node $node): ?Node
118118
$hasChanged = false;
119119
$variableNameToTypeCollection = $this->nullableObjectAssignCollector->collect($node);
120120

121+
$next = 0;
121122
foreach ($node->stmts as $key => $stmt) {
122123
// has callable on nullable variable of already collected name?
123124
$matchedNullableVariableNameToType = $this->matchedNullableVariableNameToType(
@@ -130,13 +131,15 @@ public function refactor(Node $node): ?Node
130131

131132
// adding type here + popping the variable name out
132133
$assertInstanceofExpression = $this->createAssertInstanceof($matchedNullableVariableNameToType);
133-
array_splice($node->stmts, $key, 0, [$assertInstanceofExpression]);
134+
array_splice($node->stmts, $key + $next, 0, [$assertInstanceofExpression]);
134135

135136
// remove variable name from nullable ones
136137
$hasChanged = true;
137138

138139
// from now on, the variable is not nullable, remove to avoid double asserts
139140
$variableNameToTypeCollection->remove($matchedNullableVariableNameToType);
141+
142+
++$next;
140143
}
141144

142145
if (! $hasChanged) {

0 commit comments

Comments
 (0)