mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-22 02:52:23 +00:00
add ClassConstantReplacerRector
This commit is contained in:
parent
4c86550a62
commit
647dbd0cc6
|
@ -27,17 +27,9 @@ final class ClassConstAnalyzer
|
|||
|
||||
private function isClassName(ClassConstFetch $classConstFetchNode, string $className): bool
|
||||
{
|
||||
/** @var FullyQualified $className */
|
||||
$classFullyQualifiedName = $classConstFetchNode->class->getAttribute(Attribute::RESOLVED_NAME);
|
||||
$nodeClass = $this->resolveClass($classConstFetchNode);
|
||||
|
||||
if ($classFullyQualifiedName instanceof FullyQualified) {
|
||||
return $classFullyQualifiedName->toString() === $className;
|
||||
}
|
||||
|
||||
// e.g. "$form::FILLED"
|
||||
$nodeClassName = $classConstFetchNode->class->getAttribute(Attribute::CLASS_NAME);
|
||||
|
||||
return $nodeClassName === $className;
|
||||
return $nodeClass === $className;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -49,4 +41,27 @@ final class ClassConstAnalyzer
|
|||
|
||||
return in_array($nodeConstantName, $constantNames, true);
|
||||
}
|
||||
|
||||
public function matchTypes(Node $node, array $types): ?string
|
||||
{
|
||||
if (! $node instanceof ClassConstFetch) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$class = $this->resolveClass($node);
|
||||
|
||||
return in_array($class, $types, true) ? $class : null;
|
||||
}
|
||||
|
||||
private function resolveClass(ClassConstFetch $classConstFetchNode): string
|
||||
{
|
||||
$classFullyQualifiedName = $classConstFetchNode->class->getAttribute(Attribute::RESOLVED_NAME);
|
||||
|
||||
if ($classFullyQualifiedName instanceof FullyQualified) {
|
||||
return $classFullyQualifiedName->toString();
|
||||
}
|
||||
|
||||
// e.g. "$form::FILLED"
|
||||
return (string) $classConstFetchNode->class->getAttribute(Attribute::CLASS_NAME);
|
||||
}
|
||||
}
|
||||
|
|
84
src/Rector/Dynamic/ClassConstantReplacerRector.php
Normal file
84
src/Rector/Dynamic/ClassConstantReplacerRector.php
Normal file
|
@ -0,0 +1,84 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Dynamic;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\UseUse;
|
||||
use Rector\NodeAnalyzer\ClassConstAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
final class ClassConstantReplacerRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* class => [
|
||||
* OLD_CONSTANT => NEW_CONSTANT
|
||||
* ]
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private $oldToNewConstantsByClass = [];
|
||||
|
||||
/**
|
||||
* @var ClassConstAnalyzer
|
||||
*/
|
||||
private $classConstAnalyzer;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $activeType;
|
||||
|
||||
/**
|
||||
* @param string[] $oldToNewConstantsByClass
|
||||
*/
|
||||
public function __construct(array $oldToNewConstantsByClass, ClassConstAnalyzer $classConstAnalyzer)
|
||||
{
|
||||
$this->oldToNewConstantsByClass = $oldToNewConstantsByClass;
|
||||
$this->classConstAnalyzer = $classConstAnalyzer;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
$this->activeType = null;
|
||||
|
||||
foreach ($this->oldToNewConstantsByClass as $type => $oldToNewConstants) {
|
||||
$matchedType = $this->classConstAnalyzer->matchTypes($node, $this->getTypes());
|
||||
if ($matchedType) {
|
||||
$this->activeType = $matchedType;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassConstFetch $classConstFetchNode
|
||||
*/
|
||||
public function refactor(Node $classConstFetchNode): ?Node
|
||||
{
|
||||
$configuration = $this->oldToNewConstantsByClass[$this->activeType];
|
||||
$constantName = $classConstFetchNode->name->toString();
|
||||
|
||||
if (! isset($configuration[$constantName])) {
|
||||
return $classConstFetchNode;
|
||||
}
|
||||
|
||||
$newConstantName = $configuration[$constantName];
|
||||
|
||||
$classConstFetchNode->name = new Identifier($newConstantName);
|
||||
|
||||
return $classConstFetchNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function getTypes(): array
|
||||
{
|
||||
return array_keys($this->oldToNewConstantsByClass);
|
||||
}
|
||||
}
|
|
@ -7,10 +7,10 @@ rectors:
|
|||
'setDefaultOptions': 'configureOptions'
|
||||
'Symfony\Component\Form\FormTypeInterface':
|
||||
'getName': 'getBlockPrefix'
|
||||
# property access
|
||||
Symfony\Component\PropertyAccess\PropertyAccess:
|
||||
'getPropertyAccessor': 'createPropertyAccessor'
|
||||
|
||||
Rector\Rector\Dynamic\ClassReplacerRector:
|
||||
# form
|
||||
'Symfony\Component\Form\Util\VirtualFormAwareIterator': 'Symfony\Component\Form\Util\InheritDataAwareIterator'
|
||||
# property access
|
||||
Symfony\Component\PropertyAccess\PropertyAccess:
|
||||
'getPropertyAccessor': 'createPropertyAccessor'
|
||||
|
|
30
tests/Rector/Dynamic/ClassConstantReplacerRector/Test.php
Normal file
30
tests/Rector/Dynamic/ClassConstantReplacerRector/Test.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Dynamic\ClassConstantReplacerRector;
|
||||
|
||||
use Rector\Rector\Dynamic\ClassConstantReplacerRector;
|
||||
use Rector\Testing\PHPUnit\AbstractConfigurableRectorTestCase;
|
||||
|
||||
final class Test extends AbstractConfigurableRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFileMatchesExpectedContent(
|
||||
__DIR__ . '/wrong/wrong.php.inc',
|
||||
__DIR__ . '/correct/correct.php.inc'
|
||||
);
|
||||
}
|
||||
|
||||
protected function provideConfig(): string
|
||||
{
|
||||
return __DIR__ . '/config/rector.yml';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getRectorClasses(): array
|
||||
{
|
||||
return [ClassConstantReplacerRector::class];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
rectors:
|
||||
Rector\Rector\Dynamic\ClassConstantReplacerRector:
|
||||
'Symfony\Component\Form\FormEvents':
|
||||
'PRE_BIND': 'PRE_SUBMIT'
|
||||
'BIND': 'SUBMIT'
|
||||
'POST_BIND': 'POST_SUBMIT'
|
|
@ -0,0 +1,13 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function subscribe()
|
||||
{
|
||||
return [
|
||||
FormEvents::PRE_SUBMIT
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function subscribe()
|
||||
{
|
||||
return [
|
||||
FormEvents::PRE_BIND
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user