Skip to content

Commit

Permalink
Fix message handler removal in RemoveUnusedPublicMethodParameterRector (
Browse files Browse the repository at this point in the history
#6001)

* add fixture

* Skip message handler in RemoveUnusedPublicMethodParameterRector

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jun 22, 2024
1 parent 657bf47 commit 93bccca
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
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);
}
}

0 comments on commit 93bccca

Please sign in to comment.