mirror of
https://github.com/rectorphp/rector.git
synced 2025-03-14 20:39:43 +01:00
Updated Rector to commit 4951accb91a39af88d3903930112f2752e497076
4951accb91
[Renaming] New Rule: RenameFunctionLikeParamWithinCallLikeArgRector (#5554)
This commit is contained in:
parent
adbf03fcec
commit
08cec1591e
@ -1,4 +1,4 @@
|
||||
# 360 Rules Overview
|
||||
# 361 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
@ -50,7 +50,7 @@
|
||||
|
||||
- [Removing](#removing) (5)
|
||||
|
||||
- [Renaming](#renaming) (9)
|
||||
- [Renaming](#renaming) (10)
|
||||
|
||||
- [Strict](#strict) (5)
|
||||
|
||||
@ -5601,6 +5601,21 @@ Replace constant by new ones
|
||||
|
||||
<br>
|
||||
|
||||
### RenameFunctionLikeParamWithinCallLikeArgRector
|
||||
|
||||
Rename param within closures and arrow functions based on use with specified method calls
|
||||
|
||||
:wrench: **configure it!**
|
||||
|
||||
- class: [`Rector\Renaming\Rector\FunctionLike\RenameFunctionLikeParamWithinCallLikeArgRector`](../rules/Renaming/Rector/FunctionLike/RenameFunctionLikeParamWithinCallLikeArgRector.php)
|
||||
|
||||
```diff
|
||||
-(new SomeClass)->process(function ($param) {});
|
||||
+(new SomeClass)->process(function ($parameter) {});
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### RenameFunctionRector
|
||||
|
||||
Turns defined function call new one.
|
||||
|
@ -73,7 +73,7 @@ final class BreakingVariableRenameGuard
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $functionLike
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Expr\ArrowFunction $functionLike
|
||||
*/
|
||||
public function shouldSkipVariable(string $currentName, string $expectedName, $functionLike, Variable $variable) : bool
|
||||
{
|
||||
@ -85,7 +85,7 @@ final class BreakingVariableRenameGuard
|
||||
if ($this->conflictingNameResolver->hasNameIsInFunctionLike($expectedName, $functionLike)) {
|
||||
return \true;
|
||||
}
|
||||
if ($this->overridenExistingNamesResolver->hasNameInClassMethodForNew($currentName, $functionLike)) {
|
||||
if (!$functionLike instanceof ArrowFunction && $this->overridenExistingNamesResolver->hasNameInClassMethodForNew($currentName, $functionLike)) {
|
||||
return \true;
|
||||
}
|
||||
if ($this->isVariableAlreadyDefined($variable, $currentName)) {
|
||||
@ -94,7 +94,7 @@ final class BreakingVariableRenameGuard
|
||||
if ($this->hasConflictVariable($functionLike, $expectedName)) {
|
||||
return \true;
|
||||
}
|
||||
return $this->isUsedInClosureUsesName($expectedName, $functionLike);
|
||||
return $functionLike instanceof Closure && $this->isUsedInClosureUsesName($expectedName, $functionLike);
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Expr\ArrowFunction $classMethod
|
||||
@ -151,11 +151,14 @@ final class BreakingVariableRenameGuard
|
||||
return $trinaryLogic->maybe();
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $functionLike
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Expr\ArrowFunction $functionLike
|
||||
*/
|
||||
private function hasConflictVariable($functionLike, string $newName) : bool
|
||||
{
|
||||
return $this->betterNodeFinder->hasInstanceOfName((array) $functionLike->stmts, Variable::class, $newName);
|
||||
if ($functionLike instanceof ArrowFunction) {
|
||||
return $this->betterNodeFinder->hasInstanceOfName(\array_merge([$functionLike->expr], $functionLike->params), Variable::class, $newName);
|
||||
}
|
||||
return $this->betterNodeFinder->hasInstanceOfName(\array_merge((array) $functionLike->stmts, $functionLike->params), Variable::class, $newName);
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $functionLike
|
||||
|
@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Renaming\Rector\FunctionLike;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\ArrowFunction;
|
||||
use PhpParser\Node\Expr\CallLike;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Param;
|
||||
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Naming\Guard\BreakingVariableRenameGuard;
|
||||
use Rector\Naming\ParamRenamer\ParamRenamer;
|
||||
use Rector\Naming\ValueObject\ParamRename;
|
||||
use Rector\Naming\ValueObjectFactory\ParamRenameFactory;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\Renaming\ValueObject\RenameFunctionLikeParamWithinCallLikeArg;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use RectorPrefix202402\Webmozart\Assert\Assert;
|
||||
/**
|
||||
* @see \Rector\Tests\Renaming\Rector\FunctionLike\RenameFunctionLikeParamWithinCallLikeArgRector\RenameFunctionLikeParamWithinCallLikeArgRectorTest
|
||||
*/
|
||||
final class RenameFunctionLikeParamWithinCallLikeArgRector extends AbstractRector implements ConfigurableRectorInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Naming\Guard\BreakingVariableRenameGuard
|
||||
*/
|
||||
private $breakingVariableRenameGuard;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Naming\ParamRenamer\ParamRenamer
|
||||
*/
|
||||
private $paramRenamer;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Naming\ValueObjectFactory\ParamRenameFactory
|
||||
*/
|
||||
private $paramRenameFactory;
|
||||
/**
|
||||
* @var RenameFunctionLikeParamWithinCallLikeArg[]
|
||||
*/
|
||||
private $renameFunctionLikeParamWithinCallLikeArgs = [];
|
||||
public function __construct(BreakingVariableRenameGuard $breakingVariableRenameGuard, ParamRenamer $paramRenamer, ParamRenameFactory $paramRenameFactory)
|
||||
{
|
||||
$this->breakingVariableRenameGuard = $breakingVariableRenameGuard;
|
||||
$this->paramRenamer = $paramRenamer;
|
||||
$this->paramRenameFactory = $paramRenameFactory;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Rename param within closures and arrow functions based on use with specified method calls', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
||||
(new SomeClass)->process(function ($param) {});
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
(new SomeClass)->process(function ($parameter) {});
|
||||
CODE_SAMPLE
|
||||
, [new RenameFunctionLikeParamWithinCallLikeArg('SomeClass', 'process', 0, 0, 'parameter')])]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [MethodCall::class, StaticCall::class];
|
||||
}
|
||||
/**
|
||||
* @param CallLike $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
$hasChanged = \false;
|
||||
foreach ($this->renameFunctionLikeParamWithinCallLikeArgs as $renameFunctionLikeParamWithinCallLikeArg) {
|
||||
if (!$node instanceof MethodCall && !$node instanceof StaticCall) {
|
||||
continue;
|
||||
}
|
||||
switch (\true) {
|
||||
case $node instanceof MethodCall:
|
||||
$type = $node->var;
|
||||
break;
|
||||
case $node instanceof StaticCall:
|
||||
$type = $node->class;
|
||||
break;
|
||||
}
|
||||
if (!$this->isObjectType($type, $renameFunctionLikeParamWithinCallLikeArg->getObjectType())) {
|
||||
continue;
|
||||
}
|
||||
if (($node->name ?? null) === null) {
|
||||
continue;
|
||||
}
|
||||
if (!$node->name instanceof Identifier) {
|
||||
continue;
|
||||
}
|
||||
if (!$this->isName($node->name, $renameFunctionLikeParamWithinCallLikeArg->getMethodName())) {
|
||||
continue;
|
||||
}
|
||||
$arg = $this->findArgFromMethodCall($renameFunctionLikeParamWithinCallLikeArg, $node);
|
||||
$functionLike = ($nullsafeVariable1 = $arg) ? $nullsafeVariable1->value : null;
|
||||
if (!$arg instanceof Arg) {
|
||||
continue;
|
||||
}
|
||||
if (!$functionLike instanceof FunctionLike) {
|
||||
continue;
|
||||
}
|
||||
$param = $this->findParameterFromArg($arg, $renameFunctionLikeParamWithinCallLikeArg);
|
||||
if (!$param instanceof Param) {
|
||||
continue;
|
||||
}
|
||||
if (!$param->var instanceof Variable) {
|
||||
continue;
|
||||
}
|
||||
if (($functionLike instanceof Closure || $functionLike instanceof ArrowFunction) && $this->breakingVariableRenameGuard->shouldSkipVariable((string) $this->nodeNameResolver->getName($param->var), $renameFunctionLikeParamWithinCallLikeArg->getNewParamName(), $functionLike, $param->var)) {
|
||||
continue;
|
||||
}
|
||||
$paramRename = $this->paramRenameFactory->createFromResolvedExpectedName($functionLike, $param, $renameFunctionLikeParamWithinCallLikeArg->getNewParamName());
|
||||
if (!$paramRename instanceof ParamRename) {
|
||||
continue;
|
||||
}
|
||||
$this->paramRenamer->rename($paramRename);
|
||||
$hasChanged = \true;
|
||||
}
|
||||
if (!$hasChanged) {
|
||||
return null;
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
/**
|
||||
* @param RenameFunctionLikeParamWithinCallLikeArg[] $configuration
|
||||
*/
|
||||
public function configure(array $configuration) : void
|
||||
{
|
||||
Assert::allIsAOf($configuration, RenameFunctionLikeParamWithinCallLikeArg::class);
|
||||
$this->renameFunctionLikeParamWithinCallLikeArgs = $configuration;
|
||||
}
|
||||
public function findParameterFromArg(Arg $arg, RenameFunctionLikeParamWithinCallLikeArg $renameFunctionLikeParamWithinCallLikeArg) : ?Param
|
||||
{
|
||||
$functionLike = $arg->value;
|
||||
if (!$functionLike instanceof FunctionLike) {
|
||||
return null;
|
||||
}
|
||||
return $functionLike->params[$renameFunctionLikeParamWithinCallLikeArg->getFunctionLikePosition()] ?? null;
|
||||
}
|
||||
private function findArgFromMethodCall(RenameFunctionLikeParamWithinCallLikeArg $renameFunctionLikeParamWithinCallLikeArg, CallLike $callLike) : ?Arg
|
||||
{
|
||||
if (\is_int($renameFunctionLikeParamWithinCallLikeArg->getCallLikePosition())) {
|
||||
$arg = $this->processPositionalArg($callLike, $renameFunctionLikeParamWithinCallLikeArg);
|
||||
} else {
|
||||
$arg = $this->processNamedArg($callLike, $renameFunctionLikeParamWithinCallLikeArg);
|
||||
}
|
||||
return $arg;
|
||||
}
|
||||
private function processPositionalArg(CallLike $callLike, RenameFunctionLikeParamWithinCallLikeArg $renameFunctionLikeParamWithinCallLikeArg) : ?Arg
|
||||
{
|
||||
if ($callLike->isFirstClassCallable()) {
|
||||
return null;
|
||||
}
|
||||
if ($callLike->getArgs() === []) {
|
||||
return null;
|
||||
}
|
||||
$arg = $callLike->args[$renameFunctionLikeParamWithinCallLikeArg->getCallLikePosition()] ?? null;
|
||||
if (!$arg instanceof Arg) {
|
||||
return null;
|
||||
}
|
||||
// int positions shouldn't have names
|
||||
if ($arg->name !== null) {
|
||||
return null;
|
||||
}
|
||||
return $arg;
|
||||
}
|
||||
private function processNamedArg(CallLike $callLike, RenameFunctionLikeParamWithinCallLikeArg $renameFunctionLikeParamWithinCallLikeArg) : ?Arg
|
||||
{
|
||||
$args = \array_filter($callLike->getArgs(), static function (Arg $arg) use($renameFunctionLikeParamWithinCallLikeArg) : bool {
|
||||
if ($arg->name === null) {
|
||||
return \false;
|
||||
}
|
||||
return $arg->name->name === $renameFunctionLikeParamWithinCallLikeArg->getCallLikePosition();
|
||||
});
|
||||
if ($args === []) {
|
||||
return null;
|
||||
}
|
||||
return \array_values($args)[0];
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Renaming\ValueObject;
|
||||
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Validation\RectorAssert;
|
||||
final class RenameFunctionLikeParamWithinCallLikeArg
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var string
|
||||
*/
|
||||
private $className;
|
||||
/**
|
||||
* @readonly
|
||||
* @var string
|
||||
*/
|
||||
private $methodName;
|
||||
/**
|
||||
* @var int<0, max>|string
|
||||
* @readonly
|
||||
*/
|
||||
private $callLikePosition;
|
||||
/**
|
||||
* @var int<0, max>
|
||||
* @readonly
|
||||
*/
|
||||
private $functionLikePosition;
|
||||
/**
|
||||
* @readonly
|
||||
* @var string
|
||||
*/
|
||||
private $newParamName;
|
||||
/**
|
||||
* @param int<0, max>|string $callLikePosition
|
||||
* @param int<0, max> $functionLikePosition
|
||||
*/
|
||||
public function __construct(string $className, string $methodName, $callLikePosition, int $functionLikePosition, string $newParamName)
|
||||
{
|
||||
$this->className = $className;
|
||||
$this->methodName = $methodName;
|
||||
$this->callLikePosition = $callLikePosition;
|
||||
$this->functionLikePosition = $functionLikePosition;
|
||||
$this->newParamName = $newParamName;
|
||||
RectorAssert::className($className);
|
||||
}
|
||||
public function getObjectType() : ObjectType
|
||||
{
|
||||
return new ObjectType($this->className);
|
||||
}
|
||||
public function getMethodName() : string
|
||||
{
|
||||
return $this->methodName;
|
||||
}
|
||||
/**
|
||||
* @return int<0, max>|string
|
||||
*/
|
||||
public function getCallLikePosition()
|
||||
{
|
||||
return $this->callLikePosition;
|
||||
}
|
||||
/**
|
||||
* @return int<0, max>
|
||||
*/
|
||||
public function getFunctionLikePosition() : int
|
||||
{
|
||||
return $this->functionLikePosition;
|
||||
}
|
||||
public function getNewParamName() : string
|
||||
{
|
||||
return $this->newParamName;
|
||||
}
|
||||
}
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'f9b3312035a4e5baee7105b19715d057587e1f59';
|
||||
public const PACKAGE_VERSION = '4951accb91a39af88d3903930112f2752e497076';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2024-02-22 15:43:48';
|
||||
public const RELEASE_DATE = '2024-02-22 10:06:22';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
2
vendor/composer/autoload_classmap.php
vendored
2
vendor/composer/autoload_classmap.php
vendored
@ -2023,6 +2023,7 @@ return array(
|
||||
'Rector\\Renaming\\Rector\\ClassMethod\\RenameAnnotationRector' => $baseDir . '/rules/Renaming/Rector/ClassMethod/RenameAnnotationRector.php',
|
||||
'Rector\\Renaming\\Rector\\ConstFetch\\RenameConstantRector' => $baseDir . '/rules/Renaming/Rector/ConstFetch/RenameConstantRector.php',
|
||||
'Rector\\Renaming\\Rector\\FuncCall\\RenameFunctionRector' => $baseDir . '/rules/Renaming/Rector/FuncCall/RenameFunctionRector.php',
|
||||
'Rector\\Renaming\\Rector\\FunctionLike\\RenameFunctionLikeParamWithinCallLikeArgRector' => $baseDir . '/rules/Renaming/Rector/FunctionLike/RenameFunctionLikeParamWithinCallLikeArgRector.php',
|
||||
'Rector\\Renaming\\Rector\\MethodCall\\RenameMethodRector' => $baseDir . '/rules/Renaming/Rector/MethodCall/RenameMethodRector.php',
|
||||
'Rector\\Renaming\\Rector\\Name\\RenameClassRector' => $baseDir . '/rules/Renaming/Rector/Name/RenameClassRector.php',
|
||||
'Rector\\Renaming\\Rector\\PropertyFetch\\RenamePropertyRector' => $baseDir . '/rules/Renaming/Rector/PropertyFetch/RenamePropertyRector.php',
|
||||
@ -2034,6 +2035,7 @@ return array(
|
||||
'Rector\\Renaming\\ValueObject\\RenameAnnotationByType' => $baseDir . '/rules/Renaming/ValueObject/RenameAnnotationByType.php',
|
||||
'Rector\\Renaming\\ValueObject\\RenameClassAndConstFetch' => $baseDir . '/rules/Renaming/ValueObject/RenameClassAndConstFetch.php',
|
||||
'Rector\\Renaming\\ValueObject\\RenameClassConstFetch' => $baseDir . '/rules/Renaming/ValueObject/RenameClassConstFetch.php',
|
||||
'Rector\\Renaming\\ValueObject\\RenameFunctionLikeParamWithinCallLikeArg' => $baseDir . '/rules/Renaming/ValueObject/RenameFunctionLikeParamWithinCallLikeArg.php',
|
||||
'Rector\\Renaming\\ValueObject\\RenameProperty' => $baseDir . '/rules/Renaming/ValueObject/RenameProperty.php',
|
||||
'Rector\\Renaming\\ValueObject\\RenameStaticMethod' => $baseDir . '/rules/Renaming/ValueObject/RenameStaticMethod.php',
|
||||
'Rector\\Set\\Contract\\SetListInterface' => $baseDir . '/src/Set/Contract/SetListInterface.php',
|
||||
|
2
vendor/composer/autoload_static.php
vendored
2
vendor/composer/autoload_static.php
vendored
@ -2242,6 +2242,7 @@ class ComposerStaticInit2d887a2f87c676eb32b3e04612865e54
|
||||
'Rector\\Renaming\\Rector\\ClassMethod\\RenameAnnotationRector' => __DIR__ . '/../..' . '/rules/Renaming/Rector/ClassMethod/RenameAnnotationRector.php',
|
||||
'Rector\\Renaming\\Rector\\ConstFetch\\RenameConstantRector' => __DIR__ . '/../..' . '/rules/Renaming/Rector/ConstFetch/RenameConstantRector.php',
|
||||
'Rector\\Renaming\\Rector\\FuncCall\\RenameFunctionRector' => __DIR__ . '/../..' . '/rules/Renaming/Rector/FuncCall/RenameFunctionRector.php',
|
||||
'Rector\\Renaming\\Rector\\FunctionLike\\RenameFunctionLikeParamWithinCallLikeArgRector' => __DIR__ . '/../..' . '/rules/Renaming/Rector/FunctionLike/RenameFunctionLikeParamWithinCallLikeArgRector.php',
|
||||
'Rector\\Renaming\\Rector\\MethodCall\\RenameMethodRector' => __DIR__ . '/../..' . '/rules/Renaming/Rector/MethodCall/RenameMethodRector.php',
|
||||
'Rector\\Renaming\\Rector\\Name\\RenameClassRector' => __DIR__ . '/../..' . '/rules/Renaming/Rector/Name/RenameClassRector.php',
|
||||
'Rector\\Renaming\\Rector\\PropertyFetch\\RenamePropertyRector' => __DIR__ . '/../..' . '/rules/Renaming/Rector/PropertyFetch/RenamePropertyRector.php',
|
||||
@ -2253,6 +2254,7 @@ class ComposerStaticInit2d887a2f87c676eb32b3e04612865e54
|
||||
'Rector\\Renaming\\ValueObject\\RenameAnnotationByType' => __DIR__ . '/../..' . '/rules/Renaming/ValueObject/RenameAnnotationByType.php',
|
||||
'Rector\\Renaming\\ValueObject\\RenameClassAndConstFetch' => __DIR__ . '/../..' . '/rules/Renaming/ValueObject/RenameClassAndConstFetch.php',
|
||||
'Rector\\Renaming\\ValueObject\\RenameClassConstFetch' => __DIR__ . '/../..' . '/rules/Renaming/ValueObject/RenameClassConstFetch.php',
|
||||
'Rector\\Renaming\\ValueObject\\RenameFunctionLikeParamWithinCallLikeArg' => __DIR__ . '/../..' . '/rules/Renaming/ValueObject/RenameFunctionLikeParamWithinCallLikeArg.php',
|
||||
'Rector\\Renaming\\ValueObject\\RenameProperty' => __DIR__ . '/../..' . '/rules/Renaming/ValueObject/RenameProperty.php',
|
||||
'Rector\\Renaming\\ValueObject\\RenameStaticMethod' => __DIR__ . '/../..' . '/rules/Renaming/ValueObject/RenameStaticMethod.php',
|
||||
'Rector\\Set\\Contract\\SetListInterface' => __DIR__ . '/../..' . '/src/Set/Contract/SetListInterface.php',
|
||||
|
Loading…
x
Reference in New Issue
Block a user