-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodingStyle] Add StaticArrowFunctionRector (#2657)
* [CodingStyle] Add StaticArrowFunctionRector * final touch: clean up
- Loading branch information
1 parent
b1ae9a1
commit 743fef0
Showing
7 changed files
with
147 additions
and
0 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
15 changes: 15 additions & 0 deletions
15
...-tests/CodingStyle/Rector/ArrowFunction/StaticArrowFunctionRector/Fixture/fixture.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 Rector\Tests\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector\Fixture; | ||
|
||
fn(): string => 'test'; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector\Fixture; | ||
|
||
static fn(): string => 'test'; | ||
|
||
?> |
5 changes: 5 additions & 0 deletions
5
...gStyle/Rector/ArrowFunction/StaticArrowFunctionRector/Fixture/skip_already_static.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,5 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector\Fixture; | ||
|
||
static fn(): string => 'test'; |
13 changes: 13 additions & 0 deletions
13
...CodingStyle/Rector/ArrowFunction/StaticArrowFunctionRector/Fixture/skip_with_this.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 Rector\Tests\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector\Fixture; | ||
|
||
class SkipWithThis | ||
{ | ||
private $data = 'data'; | ||
|
||
public function run() | ||
{ | ||
fn(): string => $this->data; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ingStyle/Rector/ArrowFunction/StaticArrowFunctionRector/StaticArrowFunctionRectorTest.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class StaticArrowFunctionRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...sts/CodingStyle/Rector/ArrowFunction/StaticArrowFunctionRector/config/configured_rule.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 Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(StaticArrowFunctionRector::class); | ||
}; |
69 changes: 69 additions & 0 deletions
69
rules/CodingStyle/Rector/ArrowFunction/StaticArrowFunctionRector.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,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodingStyle\Rector\ArrowFunction; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrowFunction; | ||
use PhpParser\Node\Expr\Variable; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector\StaticArrowFunctionRectorTest | ||
*/ | ||
final class StaticArrowFunctionRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Changes ArrowFunction to be static when possible', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
fn (): string => 'test'; | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
static fn (): string => 'test'; | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ArrowFunction::class]; | ||
} | ||
|
||
/** | ||
* @param ArrowFunction $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($this->shouldSkip($node)) { | ||
return null; | ||
} | ||
|
||
$node->static = true; | ||
return $node; | ||
} | ||
|
||
private function shouldSkip(ArrowFunction $arrowFunction): bool | ||
{ | ||
if ($arrowFunction->static) { | ||
return true; | ||
} | ||
|
||
return (bool) $this->betterNodeFinder->findFirst( | ||
$arrowFunction->expr, | ||
static fn (Node $subNode): bool => $subNode instanceof Variable && $subNode->name === 'this' | ||
); | ||
} | ||
} |