Merge pull request #1715 from rectorphp/constant-from-class

[SOLID] Fix PrivatizeLocalClassConstantRector for in-class use
This commit is contained in:
Tomáš Votruba 2019-07-09 18:55:33 +02:00 committed by GitHub
commit 940db5fc78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View File

@ -0,0 +1,13 @@
<?php
namespace Rector\SOLID\Tests\Rector\ClassConst\PrivatizeLocalClassConstantRector\Fixture;
class UsesConstant
{
private const SOME = ConstainsConstant::KEEP_PUBLIC . '_pcs';
}
class ConstainsConstant
{
public const KEEP_PUBLIC = 'cms';
}

View File

@ -18,6 +18,7 @@ final class PrivatizeLocalClassConstantRectorTest extends AbstractRectorTestCase
__DIR__ . '/Fixture/in_interface.php.inc',
__DIR__ . '/Fixture/in_interface_used_child_and_external.php.inc',
__DIR__ . '/Fixture/in_interface_used_child_and_extended.php.inc',
__DIR__ . '/Fixture/skip_used_in_another_class.php.inc',
]);
}

View File

@ -468,6 +468,7 @@ final class ParsedNodesByType
private function addClassConstantFetch(ClassConstFetch $classConstFetch): void
{
$constantName = $this->nameResolver->resolve($classConstFetch->name);
if ($constantName === 'class' || $constantName === null) {
// this is not a manual constant
return;
@ -485,6 +486,7 @@ final class ParsedNodesByType
} else {
$resolvedClassTypes = $this->nodeTypeResolver->resolve($classConstFetch->class);
$className = $this->matchClassTypeThatContainsConstant($resolvedClassTypes, $constantName);
if ($className === null) {
return;
}
@ -517,6 +519,10 @@ final class ParsedNodesByType
*/
private function matchClassTypeThatContainsConstant(array $resolvedClassTypes, string $constant): ?string
{
if (count($resolvedClassTypes) === 1) {
return $resolvedClassTypes[0];
}
foreach ($resolvedClassTypes as $resolvedClassType) {
$classOrInterface = $this->findClassOrInterface($resolvedClassType);
if ($classOrInterface === null) {