mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-22 00:12:29 +02:00
Updated Rector to commit 575e799aa59bc375fae671f469cb5b1574a96381
575e799aa5
Add SwapMethodCallArgumentsRector (#3726)
This commit is contained in:
parent
69f023883a
commit
67b4d9bb93
@ -1,10 +1,10 @@
|
||||
# 420 Rules Overview
|
||||
# 421 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
## Categories
|
||||
|
||||
- [Arguments](#arguments) (5)
|
||||
- [Arguments](#arguments) (6)
|
||||
|
||||
- [CodeQuality](#codequality) (79)
|
||||
|
||||
@ -264,6 +264,49 @@ return static function (RectorConfig $rectorConfig): void {
|
||||
|
||||
<br>
|
||||
|
||||
### SwapMethodCallArgumentsRector
|
||||
|
||||
Reorder arguments in method calls
|
||||
|
||||
:wrench: **configure it!**
|
||||
|
||||
- class: [`Rector\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector`](../rules/Arguments/Rector/MethodCall/SwapMethodCallArgumentsRector.php)
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector;
|
||||
use Rector\Arguments\ValueObject\SwapMethodCallArguments;
|
||||
use Rector\Config\RectorConfig;
|
||||
|
||||
return static function (RectorConfig $rectorConfig): void {
|
||||
$rectorConfig->ruleWithConfiguration(SwapMethodCallArgumentsRector::class, [
|
||||
new SwapMethodCallArguments('Caller', 'call', [
|
||||
2,
|
||||
1,
|
||||
0,
|
||||
]),
|
||||
]);
|
||||
};
|
||||
```
|
||||
|
||||
↓
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(Caller $caller)
|
||||
{
|
||||
- return $caller->call('one', 'two', 'three');
|
||||
+ return $caller->call('three', 'two', 'one');
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## CodeQuality
|
||||
|
||||
### AbsolutizeRequireAndIncludePathRector
|
||||
|
@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Arguments\Rector\MethodCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Arguments\ValueObject\SwapMethodCallArguments;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use RectorPrefix202305\Webmozart\Assert\Assert;
|
||||
/**
|
||||
* @see \Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\SwapMethodCallArgumentsRectorTest
|
||||
*/
|
||||
final class SwapMethodCallArgumentsRector extends AbstractRector implements ConfigurableRectorInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const JUST_SWAPPED = 'just_swapped';
|
||||
/**
|
||||
* @var SwapMethodCallArguments[]
|
||||
*/
|
||||
private $methodArgumentSwaps = [];
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Reorder arguments in method calls', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(Caller $caller)
|
||||
{
|
||||
return $caller->call('one', 'two', 'three');
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(Caller $caller)
|
||||
{
|
||||
return $caller->call('three', 'two', 'one');
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, [new SwapMethodCallArguments('Caller', 'call', [2, 1, 0])])]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [MethodCall::class, StaticCall::class];
|
||||
}
|
||||
/**
|
||||
* @param MethodCall|StaticCall $node
|
||||
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|null
|
||||
*/
|
||||
public function refactor(Node $node)
|
||||
{
|
||||
$isJustSwapped = (bool) $node->getAttribute(self::JUST_SWAPPED, \false);
|
||||
if ($isJustSwapped) {
|
||||
return null;
|
||||
}
|
||||
if ($node->isFirstClassCallable()) {
|
||||
return null;
|
||||
}
|
||||
$args = $node->getArgs();
|
||||
foreach ($this->methodArgumentSwaps as $methodArgumentSwap) {
|
||||
if (!$this->isName($node->name, $methodArgumentSwap->getMethod())) {
|
||||
continue;
|
||||
}
|
||||
if (!$this->isObjectTypeMatch($node, $methodArgumentSwap->getObjectType())) {
|
||||
continue;
|
||||
}
|
||||
$newArguments = $this->resolveNewArguments($methodArgumentSwap, $args);
|
||||
if ($newArguments === []) {
|
||||
return null;
|
||||
}
|
||||
foreach ($newArguments as $newPosition => $argument) {
|
||||
$node->args[$newPosition] = $argument;
|
||||
}
|
||||
$node->setAttribute(self::JUST_SWAPPED, \true);
|
||||
return $node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public function configure(array $configuration) : void
|
||||
{
|
||||
Assert::allIsAOf($configuration, SwapMethodCallArguments::class);
|
||||
$this->methodArgumentSwaps = $configuration;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $node
|
||||
*/
|
||||
private function isObjectTypeMatch($node, ObjectType $objectType) : bool
|
||||
{
|
||||
if ($node instanceof MethodCall) {
|
||||
return $this->isObjectType($node->var, $objectType);
|
||||
}
|
||||
return $this->isObjectType($node->class, $objectType);
|
||||
}
|
||||
/**
|
||||
* @param Arg[] $args
|
||||
* @return array<int, Arg>
|
||||
*/
|
||||
private function resolveNewArguments(SwapMethodCallArguments $swapMethodCallArguments, array $args) : array
|
||||
{
|
||||
$newArguments = [];
|
||||
foreach ($swapMethodCallArguments->getOrder() as $oldPosition => $newPosition) {
|
||||
if (!isset($args[$oldPosition])) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($args[$newPosition])) {
|
||||
continue;
|
||||
}
|
||||
$newArguments[$newPosition] = $args[$oldPosition];
|
||||
}
|
||||
return $newArguments;
|
||||
}
|
||||
}
|
50
rules/Arguments/ValueObject/SwapMethodCallArguments.php
Normal file
50
rules/Arguments/ValueObject/SwapMethodCallArguments.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Arguments\ValueObject;
|
||||
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Core\Validation\RectorAssert;
|
||||
final class SwapMethodCallArguments
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var string
|
||||
*/
|
||||
private $class;
|
||||
/**
|
||||
* @readonly
|
||||
* @var string
|
||||
*/
|
||||
private $method;
|
||||
/**
|
||||
* @var array<int, int>
|
||||
* @readonly
|
||||
*/
|
||||
private $order;
|
||||
/**
|
||||
* @param array<int, int> $order
|
||||
*/
|
||||
public function __construct(string $class, string $method, array $order)
|
||||
{
|
||||
$this->class = $class;
|
||||
$this->method = $method;
|
||||
$this->order = $order;
|
||||
RectorAssert::className($class);
|
||||
}
|
||||
public function getObjectType() : ObjectType
|
||||
{
|
||||
return new ObjectType($this->class);
|
||||
}
|
||||
public function getMethod() : string
|
||||
{
|
||||
return $this->method;
|
||||
}
|
||||
/**
|
||||
* @return array<int, int>
|
||||
*/
|
||||
public function getOrder() : array
|
||||
{
|
||||
return $this->order;
|
||||
}
|
||||
}
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'c6cb214aa518e02f9cc0a13f1bae638de114189d';
|
||||
public const PACKAGE_VERSION = '575e799aa59bc375fae671f469cb5b1574a96381';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-05-06 16:52:58';
|
||||
public const RELEASE_DATE = '2023-05-06 18:17:34';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitafa0499ecac4b22dfe5b6ff38f3b0743::getLoader();
|
||||
return ComposerAutoloaderInit48e0b453fc5b660e8ece7e7ac4d00614::getLoader();
|
||||
|
2
vendor/composer/autoload_classmap.php
vendored
2
vendor/composer/autoload_classmap.php
vendored
@ -1121,11 +1121,13 @@ return array(
|
||||
'Rector\\Arguments\\Rector\\FuncCall\\FunctionArgumentDefaultValueReplacerRector' => $baseDir . '/rules/Arguments/Rector/FuncCall/FunctionArgumentDefaultValueReplacerRector.php',
|
||||
'Rector\\Arguments\\Rector\\FuncCall\\SwapFuncCallArgumentsRector' => $baseDir . '/rules/Arguments/Rector/FuncCall/SwapFuncCallArgumentsRector.php',
|
||||
'Rector\\Arguments\\Rector\\MethodCall\\RemoveMethodCallParamRector' => $baseDir . '/rules/Arguments/Rector/MethodCall/RemoveMethodCallParamRector.php',
|
||||
'Rector\\Arguments\\Rector\\MethodCall\\SwapMethodCallArgumentsRector' => $baseDir . '/rules/Arguments/Rector/MethodCall/SwapMethodCallArgumentsRector.php',
|
||||
'Rector\\Arguments\\ValueObject\\ArgumentAdder' => $baseDir . '/rules/Arguments/ValueObject/ArgumentAdder.php',
|
||||
'Rector\\Arguments\\ValueObject\\RemoveMethodCallParam' => $baseDir . '/rules/Arguments/ValueObject/RemoveMethodCallParam.php',
|
||||
'Rector\\Arguments\\ValueObject\\ReplaceArgumentDefaultValue' => $baseDir . '/rules/Arguments/ValueObject/ReplaceArgumentDefaultValue.php',
|
||||
'Rector\\Arguments\\ValueObject\\ReplaceFuncCallArgumentDefaultValue' => $baseDir . '/rules/Arguments/ValueObject/ReplaceFuncCallArgumentDefaultValue.php',
|
||||
'Rector\\Arguments\\ValueObject\\SwapFuncCallArguments' => $baseDir . '/rules/Arguments/ValueObject/SwapFuncCallArguments.php',
|
||||
'Rector\\Arguments\\ValueObject\\SwapMethodCallArguments' => $baseDir . '/rules/Arguments/ValueObject/SwapMethodCallArguments.php',
|
||||
'Rector\\BetterPhpDocParser\\Annotation\\AnnotationNaming' => $baseDir . '/packages/BetterPhpDocParser/Annotation/AnnotationNaming.php',
|
||||
'Rector\\BetterPhpDocParser\\Attributes\\AttributeMirrorer' => $baseDir . '/packages/BetterPhpDocParser/Attributes/AttributeMirrorer.php',
|
||||
'Rector\\BetterPhpDocParser\\Comment\\CommentsMerger' => $baseDir . '/packages/BetterPhpDocParser/Comment/CommentsMerger.php',
|
||||
|
10
vendor/composer/autoload_real.php
vendored
10
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitafa0499ecac4b22dfe5b6ff38f3b0743
|
||||
class ComposerAutoloaderInit48e0b453fc5b660e8ece7e7ac4d00614
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,17 +22,17 @@ class ComposerAutoloaderInitafa0499ecac4b22dfe5b6ff38f3b0743
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitafa0499ecac4b22dfe5b6ff38f3b0743', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit48e0b453fc5b660e8ece7e7ac4d00614', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitafa0499ecac4b22dfe5b6ff38f3b0743', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit48e0b453fc5b660e8ece7e7ac4d00614', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitafa0499ecac4b22dfe5b6ff38f3b0743::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit48e0b453fc5b660e8ece7e7ac4d00614::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitafa0499ecac4b22dfe5b6ff38f3b0743::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit48e0b453fc5b660e8ece7e7ac4d00614::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
10
vendor/composer/autoload_static.php
vendored
10
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitafa0499ecac4b22dfe5b6ff38f3b0743
|
||||
class ComposerStaticInit48e0b453fc5b660e8ece7e7ac4d00614
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
@ -1363,11 +1363,13 @@ class ComposerStaticInitafa0499ecac4b22dfe5b6ff38f3b0743
|
||||
'Rector\\Arguments\\Rector\\FuncCall\\FunctionArgumentDefaultValueReplacerRector' => __DIR__ . '/../..' . '/rules/Arguments/Rector/FuncCall/FunctionArgumentDefaultValueReplacerRector.php',
|
||||
'Rector\\Arguments\\Rector\\FuncCall\\SwapFuncCallArgumentsRector' => __DIR__ . '/../..' . '/rules/Arguments/Rector/FuncCall/SwapFuncCallArgumentsRector.php',
|
||||
'Rector\\Arguments\\Rector\\MethodCall\\RemoveMethodCallParamRector' => __DIR__ . '/../..' . '/rules/Arguments/Rector/MethodCall/RemoveMethodCallParamRector.php',
|
||||
'Rector\\Arguments\\Rector\\MethodCall\\SwapMethodCallArgumentsRector' => __DIR__ . '/../..' . '/rules/Arguments/Rector/MethodCall/SwapMethodCallArgumentsRector.php',
|
||||
'Rector\\Arguments\\ValueObject\\ArgumentAdder' => __DIR__ . '/../..' . '/rules/Arguments/ValueObject/ArgumentAdder.php',
|
||||
'Rector\\Arguments\\ValueObject\\RemoveMethodCallParam' => __DIR__ . '/../..' . '/rules/Arguments/ValueObject/RemoveMethodCallParam.php',
|
||||
'Rector\\Arguments\\ValueObject\\ReplaceArgumentDefaultValue' => __DIR__ . '/../..' . '/rules/Arguments/ValueObject/ReplaceArgumentDefaultValue.php',
|
||||
'Rector\\Arguments\\ValueObject\\ReplaceFuncCallArgumentDefaultValue' => __DIR__ . '/../..' . '/rules/Arguments/ValueObject/ReplaceFuncCallArgumentDefaultValue.php',
|
||||
'Rector\\Arguments\\ValueObject\\SwapFuncCallArguments' => __DIR__ . '/../..' . '/rules/Arguments/ValueObject/SwapFuncCallArguments.php',
|
||||
'Rector\\Arguments\\ValueObject\\SwapMethodCallArguments' => __DIR__ . '/../..' . '/rules/Arguments/ValueObject/SwapMethodCallArguments.php',
|
||||
'Rector\\BetterPhpDocParser\\Annotation\\AnnotationNaming' => __DIR__ . '/../..' . '/packages/BetterPhpDocParser/Annotation/AnnotationNaming.php',
|
||||
'Rector\\BetterPhpDocParser\\Attributes\\AttributeMirrorer' => __DIR__ . '/../..' . '/packages/BetterPhpDocParser/Attributes/AttributeMirrorer.php',
|
||||
'Rector\\BetterPhpDocParser\\Comment\\CommentsMerger' => __DIR__ . '/../..' . '/packages/BetterPhpDocParser/Comment/CommentsMerger.php',
|
||||
@ -3125,9 +3127,9 @@ class ComposerStaticInitafa0499ecac4b22dfe5b6ff38f3b0743
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitafa0499ecac4b22dfe5b6ff38f3b0743::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitafa0499ecac4b22dfe5b6ff38f3b0743::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitafa0499ecac4b22dfe5b6ff38f3b0743::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit48e0b453fc5b660e8ece7e7ac4d00614::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit48e0b453fc5b660e8ece7e7ac4d00614::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit48e0b453fc5b660e8ece7e7ac4d00614::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user