Skip to content

Commit

Permalink
[DeadCode] Allow pass stmts instead of $node on StmtsManipulator::isV…
Browse files Browse the repository at this point in the history
…ariableUsedInNextStmt() (#5968)

* [DeadCode] Pass stmts instead of $node->stmts on StmtsManipulator::isVariableUsedInNextStmt()

* clean

* Fix phpstan
  • Loading branch information
samsonasik committed Jun 14, 2024
1 parent dfb55d1 commit aa71d0e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
20 changes: 7 additions & 13 deletions rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function refactorWithScope(Node $node, Scope $scope): null|ClassMethod|Fu
$hasChanged = false;

foreach ($assignedVariableNamesByStmtPosition as $stmtPosition => $variableName) {
if ($this->isVariableUsedInFollowingStmts($node, $stmtPosition, $variableName)) {
if ($this->isVariableUsedInFollowingStmts($stmts, $stmtPosition, $variableName)) {
continue;
}

Expand Down Expand Up @@ -142,16 +142,15 @@ private function hasCallLikeInAssignExpr(Expr $expr, Scope $scope): bool
);
}

/**
* @param Stmt[] $stmts
*/
private function isVariableUsedInFollowingStmts(
ClassMethod|Function_ $functionLike,
array $stmts,
int $assignStmtPosition,
string $variableName
): bool {
if ($functionLike->stmts === null) {
return false;
}

foreach ($functionLike->stmts as $key => $stmt) {
foreach ($stmts as $key => $stmt) {
// do not look yet
if ($key <= $assignStmtPosition) {
continue;
Expand All @@ -162,12 +161,7 @@ private function isVariableUsedInFollowingStmts(
continue;
}

$foundVariable = $this->betterNodeFinder->findVariableOfName($stmt, $variableName);
if ($foundVariable instanceof Variable) {
return true;
}

if ($this->stmtsManipulator->isVariableUsedInNextStmt($functionLike, $key, $variableName)) {
if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmts, $key, $variableName)) {
return true;
}
}
Expand Down
5 changes: 3 additions & 2 deletions rules/DeadCode/Rector/For_/RemoveDeadIfForeachForRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,15 @@ private function processIf(If_ $if, int $key, StmtsAwareInterface $stmtsAware):

private function processForForeach(For_|Foreach_ $for, int $key, StmtsAwareInterface $stmtsAware): void
{
$stmts = (array) $stmtsAware->stmts;
if ($for instanceof For_) {
$variables = $this->betterNodeFinder->findInstanceOf(
[...$for->init, ...$for->cond, ...$for->loop],
Variable::class
);
foreach ($variables as $variable) {
if ($this->stmtsManipulator->isVariableUsedInNextStmt(
$stmtsAware,
$stmts,
$key + 1,
(string) $this->getName($variable)
)) {
Expand All @@ -164,7 +165,7 @@ private function processForForeach(For_|Foreach_ $for, int $key, StmtsAwareInter
$variables = $this->betterNodeFinder->findInstanceOf($exprs, Variable::class);
foreach ($variables as $variable) {
if ($this->stmtsManipulator->isVariableUsedInNextStmt(
$stmtsAware,
$stmts,
$key + 1,
(string) $this->getName($variable)
)) {
Expand Down
4 changes: 3 additions & 1 deletion src/FamilyTree/NodeAnalyzer/ClassChildAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public function resolveParentClassMethodReturnType(ClassReflection $classReflect
*/
private function resolveParentClassMethods(ClassReflection $classReflection, string $methodName): array
{
if ($classReflection->hasNativeMethod($methodName) && $classReflection->getNativeMethod($methodName)->isPrivate()) {
if ($classReflection->hasNativeMethod($methodName) && $classReflection->getNativeMethod(
$methodName
)->isPrivate()) {
return [];
}

Expand Down
14 changes: 11 additions & 3 deletions src/NodeManipulator/StmtsManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,24 @@ function (Node $node) use (&$stmts): null {
return $stmts;
}

/**
* @param StmtsAwareInterface|Stmt[] $stmtsAware
*/
public function isVariableUsedInNextStmt(
StmtsAwareInterface $stmtsAware,
StmtsAwareInterface|array $stmtsAware,
int $jumpToKey,
string $variableName
): bool {
if ($stmtsAware->stmts === null) {
if ($stmtsAware instanceof StmtsAwareInterface && $stmtsAware->stmts === null) {
return false;
}

$stmts = array_slice($stmtsAware->stmts, $jumpToKey, null, true);
$stmts = array_slice(
$stmtsAware instanceof StmtsAwareInterface ? $stmtsAware->stmts : $stmtsAware,
$jumpToKey,
null,
true
);
if ((bool) $this->betterNodeFinder->findVariableOfName($stmts, $variableName)) {
return true;
}
Expand Down

0 comments on commit aa71d0e

Please sign in to comment.