-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace static with self for private constants (#3178)
- Loading branch information
Showing
11 changed files
with
282 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...ConvertStaticPrivateConstantToSelfRector/ConvertStaticPrivateConstantToSelfRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ConvertStaticPrivateConstantToSelfRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
/** | ||
* @return Iterator<array<string>> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/config.php'; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...tantToSelfRector/Fixture/const-in-class-with-final-annotation-will-not-be-changed.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Utils\Rector\Tests\Rector\UseDateTimeImmutableRector\Fixture; | ||
|
||
/** | ||
* @final | ||
*/ | ||
class Foo | ||
{ | ||
private const BAR = 1; | ||
public function baz(): void | ||
{ | ||
echo static::BAR; | ||
} | ||
} | ||
?> |
13 changes: 13 additions & 0 deletions
13
...cPrivateConstantToSelfRector/Fixture/const-in-non-final-class-will-not-be-changed.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Utils\Rector\Tests\Rector\UseDateTimeImmutableRector\Fixture; | ||
|
||
class Foo | ||
{ | ||
private const BAR = 1; | ||
public function baz(): void | ||
{ | ||
echo static::BAR; | ||
} | ||
} | ||
?> |
12 changes: 12 additions & 0 deletions
12
...cPrivateConstantToSelfRector/Fixture/constants-from-other-classes-are-not-changed.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Utils\Rector\Tests\Rector\UseDateTimeImmutableRector\Fixture; | ||
|
||
final class Foo | ||
{ | ||
public function run(): void | ||
{ | ||
echo \DateTimeInterface::ATOM; | ||
} | ||
} | ||
?> |
15 changes: 15 additions & 0 deletions
15
...nvertStaticPrivateConstantToSelfRector/Fixture/other-visibilities-are-not-changed.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Utils\Rector\Tests\Rector\UseDateTimeImmutableRector\Fixture; | ||
|
||
final class Foo | ||
{ | ||
protected const BAR = 1; | ||
public const BAZ = 1; | ||
public function run(): void | ||
{ | ||
echo static::BAR; | ||
echo static::BAZ; | ||
} | ||
} | ||
?> |
13 changes: 13 additions & 0 deletions
13
...rivateConstantToSelfRector/Fixture/private-constant-with-self-will-not-be-changed.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Utils\Rector\Tests\Rector\UseDateTimeImmutableRector\Fixture; | ||
|
||
final class Foo | ||
{ | ||
private const BAR = 1; | ||
public function baz(): void | ||
{ | ||
echo self::BAR; | ||
} | ||
} | ||
?> |
27 changes: 27 additions & 0 deletions
27
...cPrivateConstantToSelfRector/Fixture/private-constant-with-static-should-use-self.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Utils\Rector\Tests\Rector\UseDateTimeImmutableRector\Fixture; | ||
|
||
final class Foo | ||
{ | ||
private const BAR = 1; | ||
public function baz(): void | ||
{ | ||
echo static::BAR; | ||
} | ||
} | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace Utils\Rector\Tests\Rector\UseDateTimeImmutableRector\Fixture; | ||
|
||
final class Foo | ||
{ | ||
private const BAR = 1; | ||
public function baz(): void | ||
{ | ||
echo self::BAR; | ||
} | ||
} | ||
?> |
10 changes: 10 additions & 0 deletions
10
...Quality/Rector/ClassConstFetch/ConvertStaticPrivateConstantToSelfRector/config/config.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(Rector\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector::class); | ||
}; |
107 changes: 107 additions & 0 deletions
107
rules/CodeQuality/Rector/ClassConstFetch/ConvertStaticPrivateConstantToSelfRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\ClassConstFetch; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ClassConstFetch; | ||
use PhpParser\Node\Name; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\ConvertStaticPrivateConstantToSelfRectorTest | ||
* @see https://3v4l.org/8Y0ba | ||
* @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::* on final classes', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
final class Foo { | ||
private const BAR = 'bar'; | ||
public function run() | ||
{ | ||
$bar = static::BAR; | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
final class Foo { | ||
private const BAR = 'bar'; | ||
public function run() | ||
{ | ||
$bar = self::BAR; | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
), | ||
], | ||
); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [ClassConstFetch::class]; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node\Expr\ClassConstFetch $node | ||
*/ | ||
public function refactor(Node $node): ?ClassConstFetch | ||
{ | ||
if (! $this->isUsingStatic($node)) { | ||
return null; | ||
} | ||
|
||
if (! $this->isPrivateConstant($node)) { | ||
return null; | ||
} | ||
|
||
$node->class = new Name('self'); | ||
|
||
return $node; | ||
} | ||
|
||
private function isUsingStatic(ClassConstFetch $node): bool | ||
{ | ||
if (! $node->class instanceof Name) { | ||
return false; | ||
} | ||
|
||
return $node->class->toString() === 'static'; | ||
} | ||
|
||
private function isPrivateConstant(ClassConstFetch $node): bool | ||
{ | ||
$class = $this->betterNodeFinder->findParentType($node, Node\Stmt\Class_::class); | ||
if (! $class instanceof Node\Stmt\Class_) { | ||
return false; | ||
} | ||
if (! $class->isFinal()) { | ||
return false; | ||
} | ||
$constantName = $node->name; | ||
if (! $constantName instanceof Node\Identifier) { | ||
return false; | ||
} | ||
foreach ($class->getConstants() as $classConst) { | ||
if (! $this->nodeNameResolver->isName($classConst, $constantName->toString())) { | ||
continue; | ||
} | ||
|
||
return $classConst->isPrivate(); | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters