Skip to content

[DeadCode] Add RemoveTestsOverriddenPrivateMethodParameterRector - #8254

Merged
TomasVotruba merged 4 commits into
mainfrom
remove-overridden-private-method-param
Jul 31, 2026
Merged

[DeadCode] Add RemoveTestsOverriddenPrivateMethodParameterRector#8254
TomasVotruba merged 4 commits into
mainfrom
remove-overridden-private-method-param

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Jul 31, 2026

Copy link
Copy Markdown
Member

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:

-    /**
-     * @param \PHPUnit\Framework\MockObject\MockObject&User $user
-     */
-    private function prepareCommonMocks(Event $event, MockObject $user): void
+    private function prepareCommonMocks(Event $event): void
     {
         $user = $this->createMock(User::class);
         // ...
     }

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:

case why
class is not a PHPUnit test case out of scope for now
echo $value; $value = 5; read before override
if (...) { $value = 5; } override is conditional
$value = $value ?: 5; param read on the right side
&$value by-ref writes back to the caller
variadic / promoted param used by design
compact(), extract(), func_get_args(), func_num_args(), func_get_arg(), get_defined_vars(), $$name value readable without naming the variable
public/protected method out of scope, caller set is unknown
__construct() called via new, not covered by caller args cleanup
first-class callable, unpacked or mismatched named args at call site caller args cannot be rewritten safely

Notes

Caller args + @param removal is now shared between this rule and RemoveUnusedPrivateMethodParameterRector via a new PrivateMethodParamRemover; the existing rule keeps its behaviour, its fixtures are unchanged.

Rule is added to DeadCodeLevel::RULES.

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.
@TomasVotruba TomasVotruba changed the title [DeadCode] Add RemoveOverriddenPrivateMethodParameterRector [DeadCode] Add RemoveOverriddenPrivateMethodParameterRector, for test classes Jul 31, 2026
@TomasVotruba TomasVotruba changed the title [DeadCode] Add RemoveOverriddenPrivateMethodParameterRector, for test classes [DeadCode] Add RemoveTestsOverriddenPrivateMethodParameterRector Jul 31, 2026
@TomasVotruba
TomasVotruba merged commit e2c9d06 into main Jul 31, 2026
64 checks passed
@TomasVotruba
TomasVotruba deleted the remove-overridden-private-method-param branch July 31, 2026 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant