Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DowngradePhp73] Ensure reset() after go to end() array and got the key on DowngradeArrayKeyFirstLastRector #214

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SomeClass
{
end($items);
$lastItemKey = key($items);
reset($items);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CastArrayKeyLastValue
$stmts = (array) $classMethod->stmts;
end($stmts);
$lastItemKey = key($stmts);
reset($stmts);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class DoubleCast
$stmts = (array)(array) $classMethod->stmts;
end($stmts);
$lastItemKey = key($stmts);
reset($stmts);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ final class InsideIfCond
{
end($items);
if (key($items)) {
reset($items);
return true;
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final class KeepInsideIf
} else {
end($argumentValue);
$this->completionValue = $argumentValue ? $argumentValue[key($argumentValue)] : null;
reset($argumentValue);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class KeepInsideIf
if (\is_array($argumentValue)) {
end($argumentValue);
$this->completionValue = $argumentValue ? $argumentValue[key($argumentValue)] : null;
reset($argumentValue);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MethodCallAsArg
$getArgs = $node->getArgs();
end($getArgs);
$lastItemKey = key($getArgs);
reset($getArgs);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class PossibleNonArrayOrObjectArrayKeyLast
}

if (!\is_array($v) || 1 !== \count($v) || 10 !== \strlen($k = (string) key($v)) || "\x9D" !== $k[0] || "\0" !== $k[5] || "\x5F" !== $k[9]) {
reset($v);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ private function refactorArrayKeyLast(

$newStmts[] = $stmt;

$resetExpression = new Expression($this->nodeFactory->createFuncCall('reset', [$array]));

if (! $stmt instanceof StmtsAwareInterface && ! $stmt instanceof Return_) {
$newStmts[] = new Expression($this->nodeFactory->createFuncCall('reset', [$array]));
}

if ($stmt instanceof StmtsAwareInterface) {
$stmt->stmts = array_merge([$resetExpression], $stmt->stmts);
}

return $newStmts;
}

Expand Down
Loading