-
-
Notifications
You must be signed in to change notification settings - Fork 681
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 82c7838b40946ccab2fd42240370ffed406aed7c
rectorphp/rector-src@82c7838 [Renaming] Add RenameAttributeRector (#6009)
- Loading branch information
1 parent
fb1ff0f
commit a3156b5
Showing
5 changed files
with
124 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare (strict_types=1); | ||
namespace Rector\Renaming\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Property; | ||
use Rector\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\Renaming\ValueObject\RenameAttribute; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use RectorPrefix202406\Webmozart\Assert\Assert; | ||
/** | ||
* @see \Rector\Tests\Renaming\Rector\Class_\RenameAttributeRector\RenameAttributeRectorTest | ||
*/ | ||
final class RenameAttributeRector extends AbstractRector implements ConfigurableRectorInterface, MinPhpVersionInterface | ||
{ | ||
/** | ||
* @var RenameAttribute[] | ||
*/ | ||
private $renameAttributes = []; | ||
public function getRuleDefinition() : RuleDefinition | ||
{ | ||
return new RuleDefinition('Rename attribute class names', [new ConfiguredCodeSample(<<<'CODE_SAMPLE' | ||
#[SimpleRoute()] | ||
class SomeClass | ||
{ | ||
} | ||
CODE_SAMPLE | ||
, <<<'CODE_SAMPLE' | ||
#[BasicRoute()] | ||
class SomeClass | ||
{ | ||
} | ||
CODE_SAMPLE | ||
, [])]); | ||
} | ||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes() : array | ||
{ | ||
return [Class_::class, ClassMethod::class, Property::class]; | ||
} | ||
/** | ||
* @param Class_|ClassMethod|Property $node | ||
*/ | ||
public function refactor(Node $node) : ?\PhpParser\Node | ||
{ | ||
$hasChanged = \false; | ||
foreach ($node->attrGroups as $attrGroup) { | ||
foreach ($attrGroup->attrs as $attr) { | ||
foreach ($this->renameAttributes as $renameAttribute) { | ||
if ($this->isName($attr->name, $renameAttribute->getOldAttribute())) { | ||
$attr->name = new FullyQualified($renameAttribute->getNewAttribute()); | ||
$hasChanged = \true; | ||
} | ||
} | ||
} | ||
} | ||
if ($hasChanged) { | ||
return $node; | ||
} | ||
return null; | ||
} | ||
/** | ||
* @param mixed[] $configuration | ||
*/ | ||
public function configure(array $configuration) : void | ||
{ | ||
Assert::allIsAOf($configuration, RenameAttribute::class); | ||
$this->renameAttributes = $configuration; | ||
} | ||
public function provideMinPhpVersion() : int | ||
{ | ||
return PhpVersionFeature::ATTRIBUTES; | ||
} | ||
} |
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\Renaming\ValueObject; | ||
|
||
/** | ||
* @api | ||
*/ | ||
final class RenameAttribute | ||
{ | ||
/** | ||
* @readonly | ||
* @var string | ||
*/ | ||
private $oldAttribute; | ||
/** | ||
* @readonly | ||
* @var string | ||
*/ | ||
private $newAttribute; | ||
public function __construct(string $oldAttribute, string $newAttribute) | ||
{ | ||
$this->oldAttribute = $oldAttribute; | ||
$this->newAttribute = $newAttribute; | ||
} | ||
public function getOldAttribute() : string | ||
{ | ||
return $this->oldAttribute; | ||
} | ||
public function getNewAttribute() : string | ||
{ | ||
return $this->newAttribute; | ||
} | ||
} |
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