-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Transform]Move ReservedFnFunctionRector to Php74 namespace (#1471)
Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
35ef77b
commit 602aa13
Showing
6 changed files
with
116 additions
and
134 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
8 changes: 4 additions & 4 deletions
8
...dFnFunctionRector/Fixture/fixture.php.inc → ...dFnFunctionRector/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
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
105 changes: 105 additions & 0 deletions
105
rules/Php74/Rector/Function_/ReservedFnFunctionRector.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,105 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php74\Rector\Function_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\Function_; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
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://github.com/php/php-src/pull/3941/files#diff-7e3a1a5df28a1cbd8c0fb6db68f243da | ||
* @see \Rector\Tests\Php74\Rector\Function_\ReservedFnFunctionRector\ReservedFnFunctionRectorTest | ||
*/ | ||
final class ReservedFnFunctionRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const NEW_ORIGINAL_NAME = 'f'; | ||
|
||
public function __construct( | ||
private readonly ReflectionProvider $reflectionProvider | ||
) { | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::RESERVED_FN_FUNCTION_NAME; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change fn() function name to f(), since it will be reserved keyword', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
function fn($value) | ||
{ | ||
return $value; | ||
} | ||
fn(5); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
function f($value) | ||
{ | ||
return $value; | ||
} | ||
f(5); | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Function_::class, FuncCall::class]; | ||
} | ||
|
||
/** | ||
* @param Function_|FuncCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isName($node->name, 'fn')) { | ||
return null; | ||
} | ||
|
||
$newName = self::NEW_ORIGINAL_NAME; | ||
$count = 1; | ||
|
||
while ($this->reflectionProvider->hasFunction(new Name($newName), null)) { | ||
$newName = self::NEW_ORIGINAL_NAME . $count; | ||
++$count; | ||
} | ||
|
||
$node->name = $node instanceof FuncCall ? new Name($newName) : new Identifier($newName); | ||
return $node; | ||
} | ||
} |
123 changes: 0 additions & 123 deletions
123
rules/Transform/Rector/Function_/ReservedFnFunctionRector.php
This file was deleted.
Oops, something went wrong.