[Privatization] Skip constant exists on RepeatedLiteralToClassConstantRector (#873)

This commit is contained in:
Abdul Malik Ikhsan 2021-09-13 04:45:30 +07:00 committed by GitHub
parent fccd592223
commit 430041fd55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 1 deletions

View File

@ -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'];
}
}
?>

View File

@ -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];
}
}
?>

View File

@ -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
*/