[DeadCode] Add RemoveTestsOverriddenPrivateMethodParameterRector - #8254
Merged
Conversation
Remove a private method parameter whose value is thrown away by a direct assign before the parameter is ever read. Only the first mention of the parameter in the method body counts: it must be a plain `$param = ...;` statement at the top level, with no read of the parameter on the right side. Conditional overrides, by-ref/variadic/promoted params, `compact()`/`extract()`/`func_get_args()`/`get_defined_vars()` and variable variables are skipped. Caller args + `@param` cleanup is shared with RemoveUnusedPrivateMethodParameterRector via PrivateMethodParamRemover.
Mock overrides in test helper methods are the common case; keep the scope tight until the rule proves itself.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Removes a private method parameter whose value is thrown away by a direct assign, before the parameter is ever read.
Scoped to PHPUnit test classes for now — the common case is a test helper that takes a param and immediately replaces it with a mock. Scope can widen later once the rule proves itself.
use PHPUnit\Framework\TestCase; final class SomeTest extends TestCase { public function test() { - $this->createUser(new User()); + $this->createUser(); } - private function createUser($user) + private function createUser() { $user = $this->createMock(User::class); return $user; } }Docblock goes with the parameter:
All in-class caller args are updated too.
How it decides
Only the first mention of the parameter in the method body counts. It must be a plain
$param = ...;expression statement at the top level of the method, and the parameter must not appear on the right side.Skipped:
echo $value; $value = 5;if (...) { $value = 5; }$value = $value ?: 5;&$valuecompact(),extract(),func_get_args(),func_num_args(),func_get_arg(),get_defined_vars(),$$name__construct()new, not covered by caller args cleanupNotes
Caller args +
@paramremoval is now shared between this rule andRemoveUnusedPrivateMethodParameterRectorvia a newPrivateMethodParamRemover; the existing rule keeps its behaviour, its fixtures are unchanged.Rule is added to
DeadCodeLevel::RULES.