Skip to content

Commit

Permalink
[Php55] Add StaticToSelfOnFinalClassRector (#3629)
Browse files Browse the repository at this point in the history
* [Php55] Add StaticToSelfRector

* skip dynamic

* skip on trait

* skip in abstract class

* [ci-review] Rector Rectify

* skip on constant call

* generate docs

* register to config set php 5.5

* skip already self

* rename to StaticToSelfOnFinalClassRector

* [ci-review] Rector Rectify

* fix namespace

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 19, 2023
1 parent 14cc7d4 commit 581fba0
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 3 deletions.
41 changes: 38 additions & 3 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 416 Rules Overview
# 418 Rules Overview

<br>

Expand Down Expand Up @@ -30,7 +30,7 @@

- [Php54](#php54) (3)

- [Php55](#php55) (5)
- [Php55](#php55) (6)

- [Php56](#php56) (2)

Expand Down Expand Up @@ -64,7 +64,7 @@

- [Transform](#transform) (34)

- [TypeDeclaration](#typedeclaration) (37)
- [TypeDeclaration](#typedeclaration) (38)

- [Visibility](#visibility) (3)

Expand Down Expand Up @@ -4703,6 +4703,25 @@ The /e modifier is no longer supported, use preg_replace_callback instead

<br>

### StaticToSelfOnFinalClassRector

Change `static::class` to `self::class` on final class

- class: [`Rector\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector`](../rules/Php55/Rector/ClassConstFetch/StaticToSelfOnFinalClassRector.php)

```diff
final class SomeClass
{
public function callOnMe()
{
- var_dump(static::class);
+ var_dump(self::class);
}
}
```

<br>

### StringClassNameToClassConstantRector

Replace string class names by <class>::class constant
Expand Down Expand Up @@ -9514,6 +9533,22 @@ Add array shape exact types based on constant keys of array

<br>

### DeclareStrictTypesRector

Add declare(strict_types=1) if missing

- class: [`Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector`](../rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php)

```diff
+declare(strict_types=1);
+
function someFunction()
{
}
```

<br>

### EmptyOnNullableObjectToInstanceOfRector

Change `empty()` on nullable object to instanceof check
Expand Down
2 changes: 2 additions & 0 deletions config/set/php55.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rector\Config\RectorConfig;
use Rector\Php55\Rector\Class_\ClassConstantToSelfClassRector;
use Rector\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector;
use Rector\Php55\Rector\FuncCall\GetCalledClassToSelfClassRector;
use Rector\Php55\Rector\FuncCall\GetCalledClassToStaticClassRector;
use Rector\Php55\Rector\FuncCall\PregReplaceEModifierRector;
Expand All @@ -15,4 +16,5 @@
$rectorConfig->rule(PregReplaceEModifierRector::class);
$rectorConfig->rule(GetCalledClassToSelfClassRector::class);
$rectorConfig->rule(GetCalledClassToStaticClassRector::class);
$rectorConfig->rule(StaticToSelfOnFinalClassRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture;

final class Fixture
{
public function callOnMe()
{
var_dump(static::class);
}
}

?>
-----
<?php

namespace Rector\Tests\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture;

final class Fixture
{
public function callOnMe()
{
var_dump(self::class);
}
}

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

namespace Rector\Tests\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture;

final class SkipAlreadySelf
{
public function callOnMe()
{
var_dump(self::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture;

final class SkipCallConstant
{
private const BAR = 'test';

public function callOnMe()
{
var_dump(static::BAR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture;

final class SkipDynamic
{
public function callOnMe($variable)
{
var_dump($variable::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture;

final class SkipDynamic2
{
public function callOnMe($variable)
{
var_dump(static::$variable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture;

abstract class SkipInAbstractClass
{
public function callOnMe()
{
var_dump(static::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture;

trait SkipInTrait
{
public function callOnMe()
{
var_dump(static::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class StaticToSelfOnFinalClassRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(StaticToSelfOnFinalClassRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace Rector\Php55\Rector\ClassConstFetch;

use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://wiki.php.net/rfc/class_name_scalars
* @changelog https://3v4l.org/AHr9C#v5.5.0
* @see \Rector\Tests\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\StaticToSelfOnFinalClassRectorTest
*/
final class StaticToSelfOnFinalClassRector extends AbstractRector implements MinPhpVersionInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change `static::class` to `self::class` on final class', [
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function callOnMe()
{
var_dump(static::class);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function callOnMe()
{
var_dump(self::class);
}
}
CODE_SAMPLE
),
]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassConstFetch::class];
}

/**
* @param ClassConstFetch $node
*/
public function refactor(Node $node): ?ClassConstFetch
{
if (! $node->class instanceof Name) {
return null;
}

if (! $node->name instanceof Identifier) {
return null;
}

if ($node->class->toString() !== ObjectReference::STATIC) {
return null;
}

if ($node->name->toString() !== 'class') {
return null;
}

$class = $this->betterNodeFinder->findParentType($node, Class_::class);
if (! $class instanceof Class_) {
return null;
}

if (! $class->isFinal()) {
return null;
}

return $this->nodeFactory->createSelfFetchConstant('class');
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::CLASSNAME_CONSTANT;
}
}

0 comments on commit 581fba0

Please sign in to comment.