mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 17:00:51 +00:00
f3d9b9c31c
5493b6dc13
[DX] Re-use configure() in docs + remove prefixing of ValueObjectInliner (#1410)
102 lines
3.3 KiB
PHP
102 lines
3.3 KiB
PHP
<?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 Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
use RectorPrefix20211206\Webmozart\Assert\Assert;
|
|
/**
|
|
* @changelog https://github.com/php/php-src/pull/3941/files#diff-7e3a1a5df28a1cbd8c0fb6db68f243da
|
|
* @see \Rector\Tests\Php74\Rector\Function_\ReservedFnFunctionRector\ReservedFnFunctionRectorTest
|
|
*/
|
|
final class ReservedFnFunctionRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface, \Rector\VersionBonding\Contract\MinPhpVersionInterface
|
|
{
|
|
/**
|
|
* @api
|
|
* @var string
|
|
*/
|
|
public const RESERVED_NAMES_TO_NEW_ONES = '$reservedNamesToNewOnes';
|
|
/**
|
|
* @var string[]
|
|
*/
|
|
private $reservedNamesToNewOnes = [];
|
|
public function provideMinPhpVersion() : int
|
|
{
|
|
return \Rector\Core\ValueObject\PhpVersionFeature::RESERVED_FN_FUNCTION_NAME;
|
|
}
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Change fn() function name, since it will be reserved keyword', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'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
|
|
, ['fn' => 'someFunctionName'])]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Stmt\Function_::class, \PhpParser\Node\Expr\FuncCall::class];
|
|
}
|
|
/**
|
|
* @param Function_|FuncCall $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
foreach ($this->reservedNamesToNewOnes as $reservedName => $newName) {
|
|
if (!$this->isName($node->name, $reservedName)) {
|
|
continue;
|
|
}
|
|
$node->name = $node instanceof \PhpParser\Node\Expr\FuncCall ? new \PhpParser\Node\Name($newName) : new \PhpParser\Node\Identifier($newName);
|
|
return $node;
|
|
}
|
|
return null;
|
|
}
|
|
/**
|
|
* @param mixed[] $configuration
|
|
*/
|
|
public function configure(array $configuration) : void
|
|
{
|
|
$reservedNamesToNewOnes = $configuration[self::RESERVED_NAMES_TO_NEW_ONES] ?? $configuration;
|
|
\RectorPrefix20211206\Webmozart\Assert\Assert::allString(\array_keys($reservedNamesToNewOnes));
|
|
\RectorPrefix20211206\Webmozart\Assert\Assert::allString($reservedNamesToNewOnes);
|
|
/** @var array<string, string> $reservedNamesToNewOnes */
|
|
$this->reservedNamesToNewOnes = $reservedNamesToNewOnes;
|
|
}
|
|
}
|