Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Rector to commit 70fea54e77ffd186e41540d2b36c5362b073fbc4
rectorphp/rector-src@70fea54 [DeadCode] Add TargetRemoveClassMethodRector (#3240)
- Loading branch information
1 parent
d72c810
commit 96d8a22
Showing
8 changed files
with
158 additions
and
14 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
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
<?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 RectorPrefix202212\Webmozart\Assert\Assert; | ||
/** | ||
* @see \Rector\Tests\DeadCode\Rector\Class_\TargetRemoveClassMethodRector\TargetRemoveClassMethodRectorTest | ||
*/ | ||
final class TargetRemoveClassMethodRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
/** | ||
* @var TargetRemoveClassMethod[] | ||
*/ | ||
private $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,31 @@ | ||
<?php | ||
|
||
declare (strict_types=1); | ||
namespace Rector\DeadCode\ValueObject; | ||
|
||
final class TargetRemoveClassMethod | ||
{ | ||
/** | ||
* @readonly | ||
* @var string | ||
*/ | ||
private $className; | ||
/** | ||
* @readonly | ||
* @var string | ||
*/ | ||
private $methodName; | ||
public function __construct(string $className, string $methodName) | ||
{ | ||
$this->className = $className; | ||
$this->methodName = $methodName; | ||
} | ||
public function getClassName() : string | ||
{ | ||
return $this->className; | ||
} | ||
public function getMethodName() : string | ||
{ | ||
return $this->methodName; | ||
} | ||
} |
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
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
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
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
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