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

namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\Fixture;

use Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\Source\ParentClass;

final class FinalClassWithInheritedConstant extends ParentClass
{
public function run(): void
{
return static::FAILURE;
return static::SUCCESS;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\Fixture;

use Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\Source\ParentClass;

final class FinalClassWithInheritedConstant extends ParentClass
{
public function run(): void
{
return self::FAILURE;
return self::SUCCESS;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\Source;

class ParentClass
{
public const FAILURE = 1;
protected const SUCCESS = 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,41 @@
* @see \Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\ConvertStaticPrivateConstantToSelfRectorTest
*
* @see https://3v4l.org/8Y0ba
* @see https://3v4l.org/ZIeA1
* @see https://phpstan.org/r/11d4c850-1a40-4fae-b665-291f96104d11
*/
final class ConvertStaticPrivateConstantToSelfRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replaces static::* access to private constants with self::*',
'Replaces static::* constant access with self::* for private constants and in final classes.',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class Foo
class Foo
{
private const BAR = 'bar';
public const BAZ = 'baz';

public function run()
{
$bar = static::BAR;
$baz = static::BAZ;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class Foo
class Foo
{
private const BAR = 'bar';
public const BAZ = 'baz';

public function run()
{
$bar = self::BAR;
$baz = static::BAZ;
}
}
CODE_SAMPLE
Expand All @@ -68,7 +73,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Class_
{
if ($node->getConstants() === []) {
if ($node->getConstants() === [] && ! $node->isFinal()) {
return null;
}

Expand Down
Loading