Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message handler removal in RemoveUnusedPublicMethodParameterRector #6001

Merged
merged 4 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPublicMethodParameterRector\Fixture;

use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
final readonly class SkipCommandHandler
{
public function __invoke(string $name): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Rector\DeadCode\NodeManipulator\ClassMethodParamRemover;
use Rector\DeadCode\NodeManipulator\VariadicFunctionLikeDetector;
use Rector\NodeAnalyzer\MagicClassMethodAnalyzer;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -23,7 +24,8 @@ final class RemoveUnusedPublicMethodParameterRector extends AbstractRector
public function __construct(
private readonly VariadicFunctionLikeDetector $variadicFunctionLikeDetector,
private readonly ClassMethodParamRemover $classMethodParamRemover,
private readonly MagicClassMethodAnalyzer $magicClassMethodAnalyzer
private readonly MagicClassMethodAnalyzer $magicClassMethodAnalyzer,
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
) {
}

Expand Down Expand Up @@ -72,26 +74,14 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
// may has child, or override parent that needs follow signature
// may have child, or override parent that needs to follow the signature
if (! $node->isFinal() || $node->extends instanceof FullyQualified || $node->implements !== []) {
return null;
}

$hasChanged = false;
foreach ($node->getMethods() as $classMethod) {
if (! $classMethod->isPublic()) {
continue;
}

if ($classMethod->params === []) {
continue;
}

if ($this->magicClassMethodAnalyzer->isUnsafeOverridden($classMethod)) {
continue;
}

if ($this->variadicFunctionLikeDetector->isVariadic($classMethod)) {
if ($this->shouldSkipClassMethod($classMethod, $node)) {
continue;
}

Expand All @@ -109,4 +99,30 @@ public function refactor(Node $node): ?Node

return null;
}

private function shouldSkipClassMethod(ClassMethod $classMethod, Class_ $class): bool
{
// private method is handled by different rule
if (! $classMethod->isPublic()) {
return true;
}

if ($classMethod->params === []) {
return true;
}

// parameter is required for contract coupling
if ($this->isName($classMethod->name, '__invoke') && $this->phpAttributeAnalyzer->hasPhpAttribute(
$class,
'Symfony\Component\Messenger\Attribute\AsMessageHandler'
)) {
return true;
}

if ($this->magicClassMethodAnalyzer->isUnsafeOverridden($classMethod)) {
return true;
}

return $this->variadicFunctionLikeDetector->isVariadic($classMethod);
}
}