From 4ea7d0f477ff10755172869818056824b2d42dcd Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 29 May 2023 19:22:43 +0700 Subject: [PATCH] [DeadCode] Clean up PHPStan notice on ComplexNodeRemover::processRemoveParamWithKeys() (#4018) --- phpstan.neon | 4 ---- .../RemoveUnusedConstructorParamRector.php | 4 ++-- .../NodeManipulator/ComplexNodeRemover.php | 14 +++++++------- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 0bfcc008967..64bee312417 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -739,7 +739,3 @@ parameters: - message: '#Offset 0 does not exist on array\|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 diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedConstructorParamRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedConstructorParamRector.php index ccac89ac469..561ed712a85 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedConstructorParamRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedConstructorParamRector.php @@ -110,10 +110,10 @@ private function processRemoveParams(ClassMethod $classMethod): ?ClassMethod return null; } - $hasRemoved = $this->complexNodeRemover + $removedParamKeys = $this->complexNodeRemover ->processRemoveParamWithKeys($classMethod, $paramKeysToBeRemoved); - if ($hasRemoved) { + if ($removedParamKeys !== []) { return $classMethod; } diff --git a/rules/Removing/NodeManipulator/ComplexNodeRemover.php b/rules/Removing/NodeManipulator/ComplexNodeRemover.php index 9b077f80076..f85345544e0 100644 --- a/rules/Removing/NodeManipulator/ComplexNodeRemover.php +++ b/rules/Removing/NodeManipulator/ComplexNodeRemover.php @@ -171,19 +171,19 @@ private function removeConstructorDependency(Class_ $class, string $propertyName /** * @param int[] $paramKeysToBeRemoved + * @return int[] */ - public function processRemoveParamWithKeys(ClassMethod $classMethod, array $paramKeysToBeRemoved): bool + public function processRemoveParamWithKeys(ClassMethod $classMethod, array $paramKeysToBeRemoved): array { $totalKeys = count($classMethod->params) - 1; - $hasRemoved = false; + $removedParamKeys = []; 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 true; + break; } if (in_array($nextKey, $paramKeysToBeRemoved, true)) { @@ -191,14 +191,14 @@ public function processRemoveParamWithKeys(ClassMethod $classMethod, array $para continue; } - return $hasRemoved; + return []; } unset($classMethod->params[$paramKeyToBeRemoved]); - $hasRemoved = true; + $removedParamKeys[] = $paramKeyToBeRemoved; } - return $hasRemoved; + return $removedParamKeys; } /**