-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DeadCode] Add TargetRemoveClassMethodRector (#3240)
* [DeadCode] Add TargetRemoveClassMethodRector * merge fixtures to same test
- Loading branch information
1 parent
c78e255
commit 70fea54
Showing
7 changed files
with
198 additions
and
7 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
22 changes: 22 additions & 0 deletions
22
rules-tests/DeadCode/Rector/Class_/TargetRemoveClassMethodRector/Fixture/some_class.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,22 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Class_\TargetRemoveClassMethodRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function removeMe() | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Class_\TargetRemoveClassMethodRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
} | ||
|
||
?> |
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
16 changes: 16 additions & 0 deletions
16
rules-tests/DeadCode/Rector/Class_/TargetRemoveClassMethodRector/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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\DeadCode\Rector\Class_\TargetRemoveClassMethodRector; | ||
use Rector\DeadCode\ValueObject\TargetRemoveClassMethod; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->ruleWithConfiguration(TargetRemoveClassMethodRector::class, [ | ||
new TargetRemoveClassMethod( | ||
'Rector\Tests\DeadCode\Rector\Class_\TargetRemoveClassMethodRector\Fixture\SomeClass', | ||
'removeMe' | ||
), | ||
]); | ||
}; |
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
92 changes: 92 additions & 0 deletions
92
rules/DeadCode/Rector/Class_/TargetRemoveClassMethodRector.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,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DeadCode\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\DeadCode\ValueObject\TargetRemoveClassMethod; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @see \Rector\Tests\DeadCode\Rector\Class_\TargetRemoveClassMethodRector\TargetRemoveClassMethodRectorTest | ||
*/ | ||
final class TargetRemoveClassMethodRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
/** | ||
* @var TargetRemoveClassMethod[] | ||
*/ | ||
private array $targetRemoveClassMethods = []; | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Remove defined class method', [ | ||
new ConfiguredCodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
} | ||
CODE_SAMPLE | ||
, | ||
[new TargetRemoveClassMethod('SomeClass', 'run')] | ||
), | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node->isAnonymous()) { | ||
return null; | ||
} | ||
|
||
foreach ($this->targetRemoveClassMethods as $targetRemoveClassMethod) { | ||
if (! $this->isName($node, $targetRemoveClassMethod->getClassName())) { | ||
continue; | ||
} | ||
|
||
$classMethod = $node->getMethod($targetRemoveClassMethod->getMethodName()); | ||
if (! $classMethod instanceof ClassMethod) { | ||
continue; | ||
} | ||
|
||
$this->removeNode($classMethod); | ||
return null; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param mixed[] $configuration | ||
*/ | ||
public function configure(array $configuration): void | ||
{ | ||
Assert::notEmpty($configuration); | ||
Assert::allIsInstanceOf($configuration, TargetRemoveClassMethod::class); | ||
|
||
$this->targetRemoveClassMethods = $configuration; | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DeadCode\ValueObject; | ||
|
||
final class TargetRemoveClassMethod | ||
{ | ||
public function __construct( | ||
private readonly string $className, | ||
private readonly string $methodName | ||
) { | ||
} | ||
|
||
public function getClassName(): string | ||
{ | ||
return $this->className; | ||
} | ||
|
||
public function getMethodName(): string | ||
{ | ||
return $this->methodName; | ||
} | ||
} |