-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DeadCode] Add RemoveUnusedParamInRequiredAutowireRector (#1301)
* [DeadCode] Add RemoveUnusedParamInRequiredAutowireRector * [ci-review] Rector Rectify * update example * skip if no required annotation or Required attribute * update example * implemented * add test * [ci-review] Rector Rectify * more fixture * more fixture * add see test * final touch: collect variables first, then loop collection Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
cfae725
commit 9ba0f63
Showing
8 changed files
with
242 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
12 changes: 12 additions & 0 deletions
12
...sedParamInRequiredAutowireRector/Fixture/skip_no_reqiured_attribute_or_annotation.php.inc
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedParamInRequiredAutowireRector\Fixture; | ||
|
||
class SkipNoRequiredAttributeOrAnnotation | ||
{ | ||
private $visibilityManipulator; | ||
|
||
public function something(VisibilityManipulator $visibilityManipulator) | ||
{ | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...Method/RemoveUnusedParamInRequiredAutowireRector/Fixture/with_required_annotation.php.inc
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedParamInRequiredAutowireRector\Fixture; | ||
|
||
class WithRequiredAnnotation | ||
{ | ||
private $visibilityManipulator; | ||
|
||
/** @required */ | ||
public function autowireSomeService(VisibilityManipulator $visibilityManipulator) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedParamInRequiredAutowireRector\Fixture; | ||
|
||
class WithRequiredAnnotation | ||
{ | ||
private $visibilityManipulator; | ||
|
||
/** @required */ | ||
public function autowireSomeService() | ||
{ | ||
} | ||
} | ||
|
||
?> |
35 changes: 35 additions & 0 deletions
35
...sMethod/RemoveUnusedParamInRequiredAutowireRector/Fixture/with_required_attribute.php.inc
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedParamInRequiredAutowireRector\Fixture; | ||
|
||
use Symfony\Contracts\Service\Attribute\Required; | ||
|
||
class WithRequiredAttribute | ||
{ | ||
private $visibilityManipulator; | ||
|
||
#[Required] | ||
public function autowireSomeService(VisibilityManipulator $visibilityManipulator) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedParamInRequiredAutowireRector\Fixture; | ||
|
||
use Symfony\Contracts\Service\Attribute\Required; | ||
|
||
class WithRequiredAttribute | ||
{ | ||
private $visibilityManipulator; | ||
|
||
#[Required] | ||
public function autowireSomeService() | ||
{ | ||
} | ||
} | ||
|
||
?> |
34 changes: 34 additions & 0 deletions
34
...moveUnusedParamInRequiredAutowireRector/RemoveUnusedParamInRequiredAutowireRectorTest.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedParamInRequiredAutowireRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use SplFileInfo; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class RemoveUnusedParamInRequiredAutowireRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SplFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...e/Rector/ClassMethod/RemoveUnusedParamInRequiredAutowireRector/config/configured_rule.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedParamInRequiredAutowireRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(RemoveUnusedParamInRequiredAutowireRector::class); | ||
}; |
117 changes: 117 additions & 0 deletions
117
rules/DeadCode/Rector/ClassMethod/RemoveUnusedParamInRequiredAutowireRector.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DeadCode\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedParamInRequiredAutowireRector\RemoveUnusedParamInRequiredAutowireRectorTest | ||
*/ | ||
final class RemoveUnusedParamInRequiredAutowireRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private PhpAttributeAnalyzer $phpAttributeAnalyzer | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Remove unused parameter in required autowire method', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use Symfony\Contracts\Service\Attribute\Required; | ||
final class SomeService | ||
{ | ||
private $visibilityManipulator; | ||
#[Required] | ||
public function autowireSomeService(VisibilityManipulator $visibilityManipulator) | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
use Symfony\Contracts\Service\Attribute\Required; | ||
final class SomeService | ||
{ | ||
private $visibilityManipulator; | ||
#[Required] | ||
public function autowireSomeService() | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($this->shouldSkip($node)) { | ||
return null; | ||
} | ||
|
||
$params = $node->params; | ||
if ($params === []) { | ||
return null; | ||
} | ||
|
||
/** @var Variable[] $variables */ | ||
$variables = $this->betterNodeFinder->findInstanceOf((array) $node->getStmts(), Variable::class); | ||
$hasRemovedParam = false; | ||
|
||
foreach ($params as $param) { | ||
$paramVar = $param->var; | ||
foreach ($variables as $variable) { | ||
if ($this->nodeComparator->areNodesEqual($variable, $paramVar)) { | ||
continue 2; | ||
} | ||
} | ||
|
||
$this->removeNode($param); | ||
$hasRemovedParam = true; | ||
} | ||
|
||
if (! $hasRemovedParam) { | ||
return null; | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
private function shouldSkip(ClassMethod $classMethod): bool | ||
{ | ||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod); | ||
$hasRequiredAnnotation = $phpDocInfo->hasByName('required'); | ||
$hasRequiredAttribute = $this->phpAttributeAnalyzer->hasPhpAttribute( | ||
$classMethod, | ||
'Symfony\Contracts\Service\Attribute\Required' | ||
); | ||
|
||
return ! $hasRequiredAnnotation && ! $hasRequiredAttribute; | ||
} | ||
} |
This file contains 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