Skip to content

Commit ab29a4a

Browse files
authored
fix: convert static::* to self::* for all constant access in final classes (#7159)
* fix: convert static::* to self::* for all constant access in final classes * fix: remove code sample
1 parent b1506ef commit ab29a4a

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\Fixture;
4+
5+
use Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\Source\ParentClass;
6+
7+
final class FinalClassWithInheritedConstant extends ParentClass
8+
{
9+
public function run(): void
10+
{
11+
return static::FAILURE;
12+
return static::SUCCESS;
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\Fixture;
21+
22+
use Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\Source\ParentClass;
23+
24+
final class FinalClassWithInheritedConstant extends ParentClass
25+
{
26+
public function run(): void
27+
{
28+
return self::FAILURE;
29+
return self::SUCCESS;
30+
}
31+
}
32+
33+
?>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\Source;
4+
5+
class ParentClass
6+
{
7+
public const FAILURE = 1;
8+
protected const SUCCESS = 0;
9+
}

rules/CodeQuality/Rector/ClassConstFetch/ConvertStaticPrivateConstantToSelfRector.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,41 @@
1717
* @see \Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\ConvertStaticPrivateConstantToSelfRectorTest
1818
*
1919
* @see https://3v4l.org/8Y0ba
20+
* @see https://3v4l.org/ZIeA1
2021
* @see https://phpstan.org/r/11d4c850-1a40-4fae-b665-291f96104d11
2122
*/
2223
final class ConvertStaticPrivateConstantToSelfRector extends AbstractRector
2324
{
2425
public function getRuleDefinition(): RuleDefinition
2526
{
2627
return new RuleDefinition(
27-
'Replaces static::* access to private constants with self::*',
28+
'Replaces static::* constant access with self::* for private constants and in final classes.',
2829
[
2930
new CodeSample(
3031
<<<'CODE_SAMPLE'
31-
final class Foo
32+
class Foo
3233
{
3334
private const BAR = 'bar';
35+
public const BAZ = 'baz';
3436
3537
public function run()
3638
{
3739
$bar = static::BAR;
40+
$baz = static::BAZ;
3841
}
3942
}
4043
CODE_SAMPLE
4144
,
4245
<<<'CODE_SAMPLE'
43-
final class Foo
46+
class Foo
4447
{
4548
private const BAR = 'bar';
49+
public const BAZ = 'baz';
4650
4751
public function run()
4852
{
4953
$bar = self::BAR;
54+
$baz = static::BAZ;
5055
}
5156
}
5257
CODE_SAMPLE
@@ -68,7 +73,7 @@ public function getNodeTypes(): array
6873
*/
6974
public function refactor(Node $node): ?Class_
7075
{
71-
if ($node->getConstants() === []) {
76+
if ($node->getConstants() === [] && ! $node->isFinal()) {
7277
return null;
7378
}
7479

0 commit comments

Comments
 (0)