diff --git a/build/target-repository/docs/rector_rules_overview.md b/build/target-repository/docs/rector_rules_overview.md index 686446e2592..583438f207c 100644 --- a/build/target-repository/docs/rector_rules_overview.md +++ b/build/target-repository/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 412 Rules Overview +# 414 Rules Overview
@@ -8,7 +8,7 @@ - [CodeQuality](#codequality) (73) -- [CodingStyle](#codingstyle) (35) +- [CodingStyle](#codingstyle) (37) - [Compatibility](#compatibility) (1) @@ -2339,6 +2339,38 @@ Separate constant and properties to own lines
+### StaticArrowFunctionRector + +Changes ArrowFunction to be static when possible + +- class: [`Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector`](../rules/CodingStyle/Rector/ArrowFunction/StaticArrowFunctionRector.php) + +```diff +-fn (): string => 'test'; ++static fn (): string => 'test'; +``` + +
+ +### StaticClosureRector + +Changes Closure to be static when possible + +- class: [`Rector\CodingStyle\Rector\Closure\StaticClosureRector`](../rules/CodingStyle/Rector/Closure/StaticClosureRector.php) + +```diff +-function () { ++static function () { + if (rand(0, 1)) { + return 1; + } + + return 2; + } +``` + +
+ ### StrictArraySearchRector Makes array_search search for identical elements diff --git a/config/set/coding-style.php b/config/set/coding-style.php index 5f1ffb1e0f6..e68b6436b88 100644 --- a/config/set/coding-style.php +++ b/config/set/coding-style.php @@ -14,6 +14,7 @@ use Rector\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector; use Rector\CodingStyle\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector; use Rector\CodingStyle\Rector\ClassMethod\UnSpreadOperatorRector; +use Rector\CodingStyle\Rector\Closure\StaticClosureRector; use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector; use Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector; use Rector\CodingStyle\Rector\FuncCall\CallUserFuncArrayToVariadicRector; @@ -69,5 +70,6 @@ CallUserFuncArrayToVariadicRector::class, VersionCompareFuncCallToConstantRector::class, StaticArrowFunctionRector::class, + StaticClosureRector::class, ]); }; diff --git a/rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/Fixture/fixture.php.inc b/rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/Fixture/fixture.php.inc new file mode 100644 index 00000000000..de3d36c58b7 --- /dev/null +++ b/rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/Fixture/fixture.php.inc @@ -0,0 +1,25 @@ + +----- + diff --git a/rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/Fixture/skip_already_static.php.inc b/rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/Fixture/skip_already_static.php.inc new file mode 100644 index 00000000000..3c0e340c3ce --- /dev/null +++ b/rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/Fixture/skip_already_static.php.inc @@ -0,0 +1,11 @@ +data; + } + + return strtoupper($this->data); + }; + } +} diff --git a/rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/StaticClosureRectorTest.php b/rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/StaticClosureRectorTest.php new file mode 100644 index 00000000000..5fa1e02a0fa --- /dev/null +++ b/rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/StaticClosureRectorTest.php @@ -0,0 +1,33 @@ +doTestFileInfo($fileInfo); + } + + /** + * @return Iterator + */ + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/config/configured_rule.php b/rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/config/configured_rule.php new file mode 100644 index 00000000000..432e63bc6e3 --- /dev/null +++ b/rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(StaticClosureRector::class); +}; diff --git a/rules/CodingStyle/Rector/Closure/StaticClosureRector.php b/rules/CodingStyle/Rector/Closure/StaticClosureRector.php new file mode 100644 index 00000000000..68f5a7bb8fa --- /dev/null +++ b/rules/CodingStyle/Rector/Closure/StaticClosureRector.php @@ -0,0 +1,81 @@ +> + */ + 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' + ); + } +}