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,12 @@
<?php

namespace Rector\DeadCode\Tests\Rector\ClassConst\RemoveUnusedPrivateConstantRector\Fixture;

final class KeepStaticConstant
{
private const SOME_CONSTANT = 5;
public function run()
{
return static::SOME_CONSTANT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ final class RemoveUnusedPrivateConstantRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/keep_constant.php.inc']);
$this->doTestFiles([
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/keep_constant.php.inc',
__DIR__ . '/Fixture/keep_static_constant.php.inc',
]);
}

protected function getRectorClass(): string
Expand Down
13 changes: 7 additions & 6 deletions src/PhpParser/Node/Manipulator/ClassConstManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ public function getAllClassConstFetch(ClassConst $classConst): array
return false;
}

// is it the name match?
if ($this->nameResolver->resolve($node) !== 'self::' . $this->nameResolver->resolve($classConst)) {
return false;
}

return true;
return $this->isNameMatch($node, $classConst);
});
}

private function isNameMatch(Node $node, ClassConst $classConst): bool
{
return $this->nameResolver->resolve($node) === 'self::' . $this->nameResolver->resolve($classConst)
|| $this->nameResolver->resolve($node) === 'static::' . $this->nameResolver->resolve($classConst);
}
}