Skip to content

Commit

Permalink
[DeadCode] Use $hasRemoved flag on return ClassMethod node on RemoveU…
Browse files Browse the repository at this point in the history
…nusedConstructorParamRector (#4017)
  • Loading branch information
samsonasik committed May 29, 2023
1 parent 580c76d commit e604829
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,7 @@ parameters:
-
message: '#Offset 0 does not exist on array<PhpParser\\Node\\Stmt>\|null#'
path: rules/CodingStyle/Rector/ClassMethod/ReturnArrayClassMethodToYieldRector.php

-
message: '#Method "processRemoveParamWithKeys\(\)" returns bool type, so the name should start with is/has/was#'
path: rules/Removing/NodeManipulator/ComplexNodeRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ public function refactor(Node $node): ?Node
private function processRemoveParams(ClassMethod $classMethod): ?ClassMethod
{
$paramKeysToBeRemoved = [];
$totalParams = $classMethod->params;

foreach ($classMethod->params as $key => $param) {
if ($this->paramAnalyzer->isParamUsedInClassMethod($classMethod, $param)) {
continue;
Expand All @@ -108,14 +106,17 @@ private function processRemoveParams(ClassMethod $classMethod): ?ClassMethod
$paramKeysToBeRemoved[] = $key;
}

$this->complexNodeRemover
if ($paramKeysToBeRemoved === []) {
return null;
}

$hasRemoved = $this->complexNodeRemover
->processRemoveParamWithKeys($classMethod, $paramKeysToBeRemoved);
$newTotalParams = $classMethod->params;

if ($totalParams === $newTotalParams) {
return null;
if ($hasRemoved) {
return $classMethod;
}

return $classMethod;
return null;
}
}
10 changes: 7 additions & 3 deletions rules/Removing/NodeManipulator/ComplexNodeRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,29 +172,33 @@ private function removeConstructorDependency(Class_ $class, string $propertyName
/**
* @param int[] $paramKeysToBeRemoved
*/
public function processRemoveParamWithKeys(ClassMethod $classMethod, array $paramKeysToBeRemoved): void
public function processRemoveParamWithKeys(ClassMethod $classMethod, array $paramKeysToBeRemoved): bool
{
$totalKeys = count($classMethod->params) - 1;
$hasRemoved = false;

foreach ($paramKeysToBeRemoved as $paramKeyToBeRemoved) {
$startNextKey = $paramKeyToBeRemoved + 1;
for ($nextKey = $startNextKey; $nextKey <= $totalKeys; ++$nextKey) {
if (! isset($classMethod->params[$nextKey])) {
// no next param, break the inner loop, remove the param
unset($classMethod->params[$paramKeyToBeRemoved]);
return;
return true;
}

if (in_array($nextKey, $paramKeysToBeRemoved, true)) {
// keep searching next key not in $paramKeysToBeRemoved
continue;
}

return;
return $hasRemoved;
}

unset($classMethod->params[$paramKeyToBeRemoved]);
$hasRemoved = true;
}

return $hasRemoved;
}

/**
Expand Down

0 comments on commit e604829

Please sign in to comment.