Updated Rector to commit cf1d5b0a492598b893c3fe0ac1ec1d2fcd5018c6

cf1d5b0a49 Remove SwapMethodCallArgumentsRector as could lead to infinite swapping, use custom rule with type/value check instead (#4766)
This commit is contained in:
Tomas Votruba 2023-08-11 10:13:56 +00:00
parent 9c230e80b6
commit 7abf8380e8
8 changed files with 14 additions and 236 deletions

View File

@ -1,10 +1,10 @@
# 356 Rules Overview
# 355 Rules Overview
<br>
## Categories
- [Arguments](#arguments) (6)
- [Arguments](#arguments) (5)
- [CodeQuality](#codequality) (70)
@ -254,49 +254,6 @@ 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

View File

@ -1,125 +0,0 @@
<?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 RectorPrefix202308\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;
}
}

View File

@ -1,50 +0,0 @@
<?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;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '575c8d86349fae3144987da1ee705495b31335e7';
public const PACKAGE_VERSION = 'cf1d5b0a492598b893c3fe0ac1ec1d2fcd5018c6';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-08-11 09:32:07';
public const RELEASE_DATE = '2023-08-11 11:10:32';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit40acdc2d042f21bbee32f3edf8d89ae1::getLoader();
return ComposerAutoloaderInit16be4751e7661197313674642b820328::getLoader();

View File

@ -1260,13 +1260,11 @@ 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',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit40acdc2d042f21bbee32f3edf8d89ae1
class ComposerAutoloaderInit16be4751e7661197313674642b820328
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit40acdc2d042f21bbee32f3edf8d89ae1
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit40acdc2d042f21bbee32f3edf8d89ae1', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit16be4751e7661197313674642b820328', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit40acdc2d042f21bbee32f3edf8d89ae1', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit16be4751e7661197313674642b820328', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit40acdc2d042f21bbee32f3edf8d89ae1::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit16be4751e7661197313674642b820328::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit40acdc2d042f21bbee32f3edf8d89ae1::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit16be4751e7661197313674642b820328::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit40acdc2d042f21bbee32f3edf8d89ae1
class ComposerStaticInit16be4751e7661197313674642b820328
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -1513,13 +1513,11 @@ class ComposerStaticInit40acdc2d042f21bbee32f3edf8d89ae1
'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',
@ -2982,9 +2980,9 @@ class ComposerStaticInit40acdc2d042f21bbee32f3edf8d89ae1
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit40acdc2d042f21bbee32f3edf8d89ae1::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit40acdc2d042f21bbee32f3edf8d89ae1::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit40acdc2d042f21bbee32f3edf8d89ae1::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit16be4751e7661197313674642b820328::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit16be4751e7661197313674642b820328::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit16be4751e7661197313674642b820328::$classMap;
}, null, ClassLoader::class);
}