mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-16 16:12:22 +00:00
Added support for replacing constant from different class
This commit is contained in:
parent
f29aae36e9
commit
2da8f8713d
|
@ -2,9 +2,11 @@
|
|||
|
||||
namespace Rector\Rector\Constant;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\ConfiguredCodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
@ -32,13 +34,20 @@ final class ClassConstantReplacerRector extends AbstractRector
|
|||
{
|
||||
return new RectorDefinition('Replaces defined class constants in their calls.', [
|
||||
new ConfiguredCodeSample(
|
||||
'$value = SomeClass::OLD_CONSTANT;',
|
||||
'$value = SomeClass::NEW_CONSTANT;',
|
||||
[
|
||||
'SomeClass' => [
|
||||
'OLD_CONSTANT' => 'NEW_CONSTANT',
|
||||
],
|
||||
]
|
||||
<<<'CODE_SAMPLE'
|
||||
$value = SomeClass::OLD_CONSTANT;
|
||||
$value = SomeClass::OTHER_OLD_CONSTANT;
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
$value = SomeClass::NEW_CONSTANT;
|
||||
$value = DifferentClass::NEW_CONSTANT;
|
||||
CODE_SAMPLE
|
||||
,
|
||||
['SomeClass' => [
|
||||
'OLD_CONSTANT' => 'NEW_CONSTANT',
|
||||
'OTHER_OLD_CONSTANT' => 'DifferentClass::NEW_CONSTANT',
|
||||
]]
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
@ -66,6 +75,10 @@ final class ClassConstantReplacerRector extends AbstractRector
|
|||
continue;
|
||||
}
|
||||
|
||||
if (Strings::contains($newConstant, '::')) {
|
||||
return $this->createClassConstantFetchNodeFromDoubleColonFormat($newConstant);
|
||||
}
|
||||
|
||||
$node->name = new Identifier($newConstant);
|
||||
|
||||
return $node;
|
||||
|
@ -74,4 +87,11 @@ final class ClassConstantReplacerRector extends AbstractRector
|
|||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function createClassConstantFetchNodeFromDoubleColonFormat(string $constant): ClassConstFetch
|
||||
{
|
||||
[$constantClass, $constantName] = explode('::', $constant);
|
||||
|
||||
return new ClassConstFetch(new FullyQualified($constantClass), new Identifier($constantName));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace Rector\Tests\Rector\Constant\ClassConstantReplacerRector;
|
|||
|
||||
use Rector\Rector\Constant\ClassConstantReplacerRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\Tests\Rector\Constant\ClassConstantReplacerRector\Source\DifferentClass;
|
||||
use Rector\Tests\Rector\Constant\ClassConstantReplacerRector\Source\LocalFormEvents;
|
||||
|
||||
final class ClassConstantReplacerRectorTest extends AbstractRectorTestCase
|
||||
|
@ -27,6 +28,7 @@ final class ClassConstantReplacerRectorTest extends AbstractRectorTestCase
|
|||
'PRE_BIND' => 'PRE_SUBMIT',
|
||||
'BIND' => 'SUBMIT',
|
||||
'POST_BIND' => 'POST_SUBMIT',
|
||||
'OLD_CONSTANT' => DifferentClass::class . '::NEW_CONSTANT',
|
||||
]];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,8 @@ class SomeClass
|
|||
{
|
||||
return [
|
||||
LocalFormEvents::PRE_BIND,
|
||||
LocalFormEvents::PRE_Bind
|
||||
LocalFormEvents::PRE_Bind,
|
||||
LocalFormEvents::OLD_CONSTANT,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +30,8 @@ class SomeClass
|
|||
{
|
||||
return [
|
||||
LocalFormEvents::PRE_SUBMIT,
|
||||
LocalFormEvents::PRE_SUBMIT
|
||||
LocalFormEvents::PRE_SUBMIT,
|
||||
\Rector\Tests\Rector\Constant\ClassConstantReplacerRector\Source\DifferentClass::NEW_CONSTANT,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Constant\ClassConstantReplacerRector\Source;
|
||||
|
||||
final class DifferentClass
|
||||
{
|
||||
public const NEW_CONSTANT = '';
|
||||
}
|
Loading…
Reference in New Issue
Block a user