Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodingStyle] Add StaticClosureRector (#2658)
* [CodingStyle] Add StaticClosureRector * regenerate docs
- Loading branch information
1 parent
743fef0
commit ae7648e
Showing
8 changed files
with
215 additions
and
2 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
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
25 changes: 25 additions & 0 deletions
25
rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/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,25 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Fixture; | ||
|
||
function () { | ||
if (rand(0, 1)) { | ||
return 1; | ||
} | ||
return 2; | ||
}; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Fixture; | ||
|
||
static function () { | ||
if (rand(0, 1)) { | ||
return 1; | ||
} | ||
return 2; | ||
}; | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...-tests/CodingStyle/Rector/Closure/StaticClosureRector/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,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Fixture; | ||
|
||
static function () { | ||
if (rand(0, 1)) { | ||
return 1; | ||
} | ||
|
||
return 2; | ||
}; |
19 changes: 19 additions & 0 deletions
19
rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/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,19 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Fixture; | ||
|
||
class SkipWithThis | ||
{ | ||
private $data = 'data'; | ||
|
||
public function run() | ||
{ | ||
function () { | ||
if (rand(0, 1)) { | ||
return $this->data; | ||
} | ||
|
||
return strtoupper($this->data); | ||
}; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/StaticClosureRectorTest.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\Closure\StaticClosureRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class StaticClosureRectorTest 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
rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/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\Closure\StaticClosureRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(StaticClosureRector::class); | ||
}; |
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,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodingStyle\Rector\Closure; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Closure; | ||
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\Closure\StaticClosureRector\StaticClosureRectorTest | ||
*/ | ||
final class StaticClosureRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Changes Closure to be static when possible', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
function () { | ||
if (rand(0, 1)) { | ||
return 1; | ||
} | ||
return 2; | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
static function () { | ||
if (rand(0, 1)) { | ||
return 1; | ||
} | ||
return 2; | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Closure::class]; | ||
} | ||
|
||
/** | ||
* @param Closure $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($this->shouldSkip($node)) { | ||
return null; | ||
} | ||
|
||
$node->static = true; | ||
return $node; | ||
} | ||
|
||
private function shouldSkip(Closure $closure): bool | ||
{ | ||
if ($closure->static) { | ||
return true; | ||
} | ||
|
||
return (bool) $this->betterNodeFinder->findFirst( | ||
$closure->stmts, | ||
static fn (Node $subNode): bool => $subNode instanceof Variable && $subNode->name === 'this' | ||
); | ||
} | ||
} |