-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rector (9.1): ClassConstantToClassConstantRector to rename class …
…constants to a new class. (#282) * ConstantToClassConstantRector serves d8 and d9, it should be in the generic space * New rector (9.1): ClassConstantToClassConstantRector to rename class constants to a new class. * 0.19 changes --------- Co-authored-by: Ken Rickard <[email protected]>
- Loading branch information
1 parent
4ad1fc5
commit 2af405c
Showing
15 changed files
with
301 additions
and
31 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
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
112 changes: 112 additions & 0 deletions
112
src/Rector/Deprecation/ClassConstantToClassConstantRector.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,112 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DrupalRector\Rector\Deprecation; | ||
|
||
use DrupalRector\Rector\ValueObject\ClassConstantToClassConstantConfiguration; | ||
use DrupalRector\Rector\ValueObject\ConstantToClassConfiguration; | ||
use PhpParser\Node; | ||
use Rector\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* Replaces deprecated constant with class constant. | ||
* | ||
* What is covered: | ||
* - Replacement with a use statement. | ||
*/ | ||
class ClassConstantToClassConstantRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
/** | ||
* @var ClassConstantToClassConstantConfiguration[] | ||
*/ | ||
private array $constantToClassRenames; | ||
|
||
public function configure(array $configuration): void | ||
{ | ||
foreach ($configuration as $value) { | ||
if (!($value instanceof ClassConstantToClassConstantConfiguration)) { | ||
throw new \InvalidArgumentException(sprintf('Each configuration item must be an instance of "%s"', ConstantToClassConfiguration::class)); | ||
} | ||
} | ||
|
||
$this->constantToClassRenames = $configuration; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Fixes deprecated class contant use, used in Drupal 9.1 deprecations', [ | ||
new ConfiguredCodeSample( | ||
<<<'CODE_BEFORE' | ||
$value = Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_NAME; | ||
$value2 = Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT; | ||
$value3 = Symfony\Cmf\Component\Routing\RouteObjectInterface::CONTROLLER_NAME; | ||
CODE_BEFORE | ||
, | ||
<<<'CODE_AFTER' | ||
$value = \Drupal\Core\Routing\RouteObjectInterface::ROUTE_NAME; | ||
$value2 = \Drupal\Core\Routing\RouteObjectInterface::ROUTE_OBJECT; | ||
$value3 = \Drupal\Core\Routing\RouteObjectInterface::CONTROLLER_NAME; | ||
CODE_AFTER | ||
, | ||
[ | ||
new ClassConstantToClassConstantConfiguration( | ||
'Symfony\Cmf\Component\Routing\RouteObjectInterface', | ||
'ROUTE_NAME', | ||
'Drupal\Core\Routing\RouteObjectInterface', | ||
'ROUTE_NAME', | ||
), | ||
new ClassConstantToClassConstantConfiguration( | ||
'Symfony\Cmf\Component\Routing\RouteObjectInterface', | ||
'ROUTE_OBJECT', | ||
'Drupal\Core\Routing\RouteObjectInterface', | ||
'ROUTE_OBJECT', | ||
), | ||
new ClassConstantToClassConstantConfiguration( | ||
'Symfony\Cmf\Component\Routing\RouteObjectInterface', | ||
'CONTROLLER_NAME', | ||
'Drupal\Core\Routing\RouteObjectInterface', | ||
'CONTROLLER_NAME', | ||
), | ||
] | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ | ||
Node\Expr\ClassConstFetch::class, | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
assert($node instanceof Node\Expr\ClassConstFetch); | ||
|
||
foreach ($this->constantToClassRenames as $constantToClassRename) { | ||
if ($this->getName($node->name) === $constantToClassRename->getDeprecated() && $this->getName($node->class) === $constantToClassRename->getDeprecatedClass()) { | ||
// We add a fully qualified class name and the parameters in `rector.php` adds the use statement. | ||
$fully_qualified_class = new Node\Name\FullyQualified($constantToClassRename->getClass()); | ||
|
||
$name = new Node\Identifier($constantToClassRename->getConstant()); | ||
|
||
return new Node\Expr\ClassConstFetch($fully_qualified_class, $name); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
src/Rector/ValueObject/ClassConstantToClassConstantConfiguration.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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DrupalRector\Rector\ValueObject; | ||
|
||
use Rector\Validation\RectorAssert; | ||
|
||
final class ClassConstantToClassConstantConfiguration | ||
{ | ||
private string $deprecated; | ||
private string $class; | ||
private string $constant; | ||
|
||
private string $deprecatedClass; | ||
|
||
public function __construct(string $deprecatedClass, string $deprecated, string $class, string $constant) | ||
{ | ||
$this->deprecatedClass = $deprecatedClass; | ||
$this->deprecated = $deprecated; | ||
$this->class = $class; | ||
$this->constant = $constant; | ||
|
||
RectorAssert::className($deprecatedClass); | ||
RectorAssert::className($class); | ||
RectorAssert::constantName($deprecated); | ||
RectorAssert::constantName($constant); | ||
} | ||
|
||
public function getDeprecated(): string | ||
{ | ||
return $this->deprecated; | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return $this->class; | ||
} | ||
|
||
public function getConstant(): string | ||
{ | ||
return $this->constant; | ||
} | ||
|
||
public function getDeprecatedClass(): string | ||
{ | ||
return $this->deprecatedClass; | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
...ctor/Deprecation/ClassConstantToClassConstantRector/ConstantToClassConstantRectorTest.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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DrupalRector\Rector\Deprecation\ClassConstantToClassConstantRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
class ClassConstantToClassConstantRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @covers ::refactor | ||
* | ||
* @dataProvider provideData | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
/** | ||
* @return Iterator<<string>> | ||
*/ | ||
public static function provideData(): \Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__.'/fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
// must be implemented | ||
return __DIR__.'/config/configured_rule.php'; | ||
} | ||
} |
Oops, something went wrong.