Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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';
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function test(): void
__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',
]);
}

Expand Down
6 changes: 6 additions & 0 deletions src/NodeContainer/ParsedNodesByType.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ private function addClassConstant(ClassConst $classConst): void
private function addClassConstantFetch(ClassConstFetch $classConstFetch): void
{
$constantName = $this->nameResolver->resolve($classConstFetch->name);

if ($constantName === 'class' || $constantName === null) {
// this is not a manual constant
return;
Expand All @@ -485,6 +486,7 @@ private function addClassConstantFetch(ClassConstFetch $classConstFetch): void
} else {
$resolvedClassTypes = $this->nodeTypeResolver->resolve($classConstFetch->class);
$className = $this->matchClassTypeThatContainsConstant($resolvedClassTypes, $constantName);

if ($className === null) {
return;
}
Expand Down Expand Up @@ -517,6 +519,10 @@ private function addMethod(ClassMethod $classMethod): void
*/
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) {
Expand Down