-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rectorphp/rector-src@743fef0 [CodingStyle] Add StaticArrowFunctionRector (#2657)
- Loading branch information
1 parent
4ddedc1
commit 2747e03
Showing
14 changed files
with
114 additions
and
47 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
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
<?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 function (Node $subNode) : bool { | ||
return $subNode instanceof Variable && $subNode->name === 'this'; | ||
}); | ||
} | ||
} |
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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Binary file not shown.
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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
-----BEGIN PGP SIGNATURE----- | ||
|
||
iQIzBAABCgAdFiEE0yaA1ZV9xxFr4pwUzxoQjQ565yAFAmK8Ev8ACgkQzxoQjQ56 | ||
5y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N9Kwq6tulGD3V1j | ||
oJJh/8liFQrj3DFUz6yIczZcuTtbGcWmIhTQxtlJJczZyGXlK8aXIqac3/YbBIqo | ||
4tvCgpLJjGQ8WouVUURGy/AXiUJulnBpDGUAmbdWrwDE3oxx2IPJ+OrmtZVcK2Q1 | ||
30+jLvdb5NasGTRV84aB7lLKrifde6qFWaXh+s1b/A8znnjHdFMTvYn2F6/xFWdO | ||
u0GU5wIcLrWMvmQNA7mRPiV8e7KtOoCggCm923TDJ9Ov56ZL5N+iPCW+7TZWOuM9 | ||
L2JLhN26WCZzVn/ODuxpIKzz07UGCTcaKzNFYL/V/+Oly7JEub6nNI90SNHRcOt4 | ||
Iq+EoO8/jW2WZsnBHqP8o88ii9I+DEOYt60csVGVp4sZ3RDgSwXiC5eV0rDvz/0F | ||
32oPtYAe5cRIwVriKhGlLM74eozaLmENgtMLePkclpNarFtdwFg= | ||
=AySM | ||
-----END PGP SIGNATURE----- |
Oops, something went wrong.