mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-03 09:50:49 +00:00
[Privatization] Skip constant exists on RepeatedLiteralToClassConstantRector (#873)
This commit is contained in:
parent
fccd592223
commit
430041fd55
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\RepeatedLiteralToClassConstantRector\Fixture;
|
||||
|
||||
class SkipExistsDifferentValue
|
||||
{
|
||||
private const REQUIRES = 'exists_different_value';
|
||||
|
||||
public function run($key, $items)
|
||||
{
|
||||
if ($key === 'requires') {
|
||||
return $items['requires'];
|
||||
}
|
||||
|
||||
return $items['requires'];
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\RepeatedLiteralToClassConstantRector\Fixture;
|
||||
|
||||
class UseExistsSameValue
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const REQUIRES = 'requires';
|
||||
|
||||
public function run($key, $items)
|
||||
{
|
||||
if ($key === 'requires') {
|
||||
return $items['requires'];
|
||||
}
|
||||
|
||||
return $items['requires'];
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\RepeatedLiteralToClassConstantRector\Fixture;
|
||||
|
||||
class UseExistsSameValue
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const REQUIRES = 'requires';
|
||||
|
||||
public function run($key, $items)
|
||||
{
|
||||
if ($key === self::REQUIRES) {
|
||||
return $items[self::REQUIRES];
|
||||
}
|
||||
|
||||
return $items[self::REQUIRES];
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -10,6 +10,7 @@ use PhpParser\Node\Expr\ClassConstFetch;
|
|||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Core\NodeManipulator\ClassInsertManipulator;
|
||||
use Rector\Core\Php\ReservedKeywordAnalyzer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
|
@ -156,7 +157,12 @@ CODE_SAMPLE
|
|||
*/
|
||||
private function replaceStringsWithClassConstReferences(Class_ $class, array $stringsToReplace): void
|
||||
{
|
||||
$this->traverseNodesWithCallable($class, function (Node $node) use ($stringsToReplace): ?ClassConstFetch {
|
||||
$classConsts = $class->getConstants();
|
||||
|
||||
$this->traverseNodesWithCallable($class, function (Node $node) use (
|
||||
$stringsToReplace,
|
||||
$classConsts
|
||||
): ?ClassConstFetch {
|
||||
if (! $node instanceof String_) {
|
||||
return null;
|
||||
}
|
||||
|
@ -165,11 +171,41 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
$isInMethod = (bool) $this->betterNodeFinder->findParentType($node, ClassMethod::class);
|
||||
if (! $isInMethod) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$constantName = $this->createConstName($node->value);
|
||||
if ($this->isClassConstExistsWithDifferentValue($classConsts, $constantName, $node->value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->nodeFactory->createSelfFetchConstant($constantName, $node);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassConst[] $classConsts
|
||||
*/
|
||||
private function isClassConstExistsWithDifferentValue(array $classConsts, string $constantName, string $value): bool
|
||||
{
|
||||
foreach ($classConsts as $classConst) {
|
||||
$consts = $classConst->consts;
|
||||
foreach ($consts as $const) {
|
||||
if (! $this->nodeNameResolver->isName($const->name, $constantName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $this->valueResolver->isValue($const->value, $value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $stringsToReplace
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue
Block a user