mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
Updated Rector to commit 3ff17513c47ace5c6a71ef4f918f33a8fa183a7b
3ff17513c4
[Arguments] Add the missing default value and type for the added arguments (#837)
This commit is contained in:
parent
c4be8405d9
commit
300188a298
@ -6,11 +6,14 @@ namespace Rector\Arguments;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Arguments\Contract\ReplaceArgumentDefaultValueInterface;
|
||||
use Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue;
|
||||
use Rector\Core\PhpParser\Node\NodeFactory;
|
||||
use Rector\Core\PhpParser\Node\Value\ValueResolver;
|
||||
final class ArgumentDefaultValueReplacer
|
||||
@ -37,15 +40,44 @@ final class ArgumentDefaultValueReplacer
|
||||
if (!isset($node->params[$replaceArgumentDefaultValue->getPosition()])) {
|
||||
return null;
|
||||
}
|
||||
} elseif (isset($node->args[$replaceArgumentDefaultValue->getPosition()])) {
|
||||
$this->processArgs($node, $replaceArgumentDefaultValue);
|
||||
return $this->processParams($node, $replaceArgumentDefaultValue);
|
||||
}
|
||||
return $node;
|
||||
if (!isset($node->args[$replaceArgumentDefaultValue->getPosition()])) {
|
||||
return null;
|
||||
}
|
||||
return $this->processArgs($node, $replaceArgumentDefaultValue);
|
||||
}
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function isDefaultValueMatched(?\PhpParser\Node\Expr $expr, $value) : bool
|
||||
{
|
||||
// allow any values before, also allow param without default value
|
||||
if ($value === \Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue::ANY_VALUE_BEFORE) {
|
||||
return \true;
|
||||
}
|
||||
if ($expr === null) {
|
||||
return \false;
|
||||
}
|
||||
if ($this->valueResolver->isValue($expr, $value)) {
|
||||
return \true;
|
||||
}
|
||||
// ValueResolver::isValue returns false when default value is `null`
|
||||
return $value === null && $this->valueResolver->isNull($expr);
|
||||
}
|
||||
private function processParams(\PhpParser\Node\Stmt\ClassMethod $classMethod, \Rector\Arguments\Contract\ReplaceArgumentDefaultValueInterface $replaceArgumentDefaultValue) : ?\PhpParser\Node\Stmt\ClassMethod
|
||||
{
|
||||
$position = $replaceArgumentDefaultValue->getPosition();
|
||||
if (!$this->isDefaultValueMatched($classMethod->params[$position]->default, $replaceArgumentDefaultValue->getValueBefore())) {
|
||||
return null;
|
||||
}
|
||||
$classMethod->params[$position]->default = $this->normalizeValue($replaceArgumentDefaultValue->getValueAfter());
|
||||
return $classMethod;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\FuncCall $expr
|
||||
*/
|
||||
private function processArgs($expr, \Rector\Arguments\Contract\ReplaceArgumentDefaultValueInterface $replaceArgumentDefaultValue) : void
|
||||
private function processArgs($expr, \Rector\Arguments\Contract\ReplaceArgumentDefaultValueInterface $replaceArgumentDefaultValue) : \PhpParser\Node\Expr
|
||||
{
|
||||
$position = $replaceArgumentDefaultValue->getPosition();
|
||||
$argValue = $this->valueResolver->getValue($expr->args[$position]->value);
|
||||
@ -57,19 +89,27 @@ final class ArgumentDefaultValueReplacer
|
||||
$expr->args = $newArgs;
|
||||
}
|
||||
}
|
||||
return $expr;
|
||||
}
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
private function normalizeValueToArgument($value) : \PhpParser\Node\Arg
|
||||
{
|
||||
return new \PhpParser\Node\Arg($this->normalizeValue($value));
|
||||
}
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return \PhpParser\Node\Expr\ClassConstFetch|\PhpParser\Node\Expr
|
||||
*/
|
||||
private function normalizeValue($value)
|
||||
{
|
||||
// class constants → turn string to composite
|
||||
if (\is_string($value) && \strpos($value, '::') !== \false) {
|
||||
[$class, $constant] = \explode('::', $value);
|
||||
$classConstFetch = $this->nodeFactory->createClassConstFetch($class, $constant);
|
||||
return new \PhpParser\Node\Arg($classConstFetch);
|
||||
return $this->nodeFactory->createClassConstFetch($class, $constant);
|
||||
}
|
||||
return new \PhpParser\Node\Arg(\PhpParser\BuilderHelpers::normalizeValue($value));
|
||||
return \PhpParser\BuilderHelpers::normalizeValue($value);
|
||||
}
|
||||
/**
|
||||
* @param Arg[] $argumentNodes
|
||||
|
44
rules/Arguments/NodeAnalyzer/ChangedArgumentsDetector.php
Normal file
44
rules/Arguments/NodeAnalyzer/ChangedArgumentsDetector.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Arguments\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Param;
|
||||
use Rector\Core\PhpParser\Node\Value\ValueResolver;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
final class ChangedArgumentsDetector
|
||||
{
|
||||
/**
|
||||
* @var \Rector\Core\PhpParser\Node\Value\ValueResolver
|
||||
*/
|
||||
private $valueResolver;
|
||||
/**
|
||||
* @var \Rector\NodeNameResolver\NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
public function __construct(\Rector\Core\PhpParser\Node\Value\ValueResolver $valueResolver, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver)
|
||||
{
|
||||
$this->valueResolver = $valueResolver;
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
}
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function isDefaultValueChanged(\PhpParser\Node\Param $param, $value) : bool
|
||||
{
|
||||
if ($param->default === null) {
|
||||
return \false;
|
||||
}
|
||||
return !$this->valueResolver->isValue($param->default, $value);
|
||||
}
|
||||
public function isTypeChanged(\PhpParser\Node\Param $param, ?string $type) : bool
|
||||
{
|
||||
if ($param->type === null) {
|
||||
return \false;
|
||||
}
|
||||
if ($type === null) {
|
||||
return \true;
|
||||
}
|
||||
return !$this->nodeNameResolver->isName($param->type, $type);
|
||||
}
|
||||
}
|
@ -17,6 +17,7 @@ use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Arguments\NodeAnalyzer\ArgumentAddingScope;
|
||||
use Rector\Arguments\NodeAnalyzer\ChangedArgumentsDetector;
|
||||
use Rector\Arguments\ValueObject\ArgumentAdder;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
@ -46,9 +47,14 @@ final class ArgumentAdderRector extends \Rector\Core\Rector\AbstractRector imple
|
||||
* @var \Rector\Arguments\NodeAnalyzer\ArgumentAddingScope
|
||||
*/
|
||||
private $argumentAddingScope;
|
||||
public function __construct(\Rector\Arguments\NodeAnalyzer\ArgumentAddingScope $argumentAddingScope)
|
||||
/**
|
||||
* @var \Rector\Arguments\NodeAnalyzer\ChangedArgumentsDetector
|
||||
*/
|
||||
private $changedArgumentsDetector;
|
||||
public function __construct(\Rector\Arguments\NodeAnalyzer\ArgumentAddingScope $argumentAddingScope, \Rector\Arguments\NodeAnalyzer\ChangedArgumentsDetector $changedArgumentsDetector)
|
||||
{
|
||||
$this->argumentAddingScope = $argumentAddingScope;
|
||||
$this->changedArgumentsDetector = $changedArgumentsDetector;
|
||||
}
|
||||
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
||||
{
|
||||
@ -173,7 +179,17 @@ CODE_SAMPLE
|
||||
if (!isset($node->params[$position])) {
|
||||
return \false;
|
||||
}
|
||||
return $this->isName($node->params[$position], $argumentName);
|
||||
$param = $node->params[$position];
|
||||
// argument added and name has been changed
|
||||
if (!$this->isName($param, $argumentName)) {
|
||||
return \true;
|
||||
}
|
||||
// argument added and default has been changed
|
||||
if ($this->changedArgumentsDetector->isDefaultValueChanged($param, $argumentAdder->getArgumentDefaultValue())) {
|
||||
return \true;
|
||||
}
|
||||
// argument added and type has been changed
|
||||
return $this->changedArgumentsDetector->isTypeChanged($param, $argumentAdder->getArgumentType());
|
||||
}
|
||||
if (isset($node->args[$position])) {
|
||||
return \true;
|
||||
|
@ -7,6 +7,10 @@ use PHPStan\Type\ObjectType;
|
||||
use Rector\Arguments\Contract\ReplaceArgumentDefaultValueInterface;
|
||||
final class ReplaceArgumentDefaultValue implements \Rector\Arguments\Contract\ReplaceArgumentDefaultValueInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const ANY_VALUE_BEFORE = '*ANY_VALUE_BEFORE*';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
@ -16,11 +16,11 @@ final class VersionResolver
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '327929f6206170799c609fb4ef062aa31ac338b9';
|
||||
public const PACKAGE_VERSION = '3ff17513c47ace5c6a71ef4f918f33a8fa183a7b';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2021-09-19 00:16:36';
|
||||
public const RELEASE_DATE = '2021-09-19 11:02:33';
|
||||
public static function resolvePackageVersion() : string
|
||||
{
|
||||
$process = new \RectorPrefix20210919\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);
|
||||
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -4,4 +4,4 @@
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit2ecd80168824b0ba5abef5ededdbc492::getLoader();
|
||||
return ComposerAutoloaderInit34f2d773ddf7d761551322a8fa07b86e::getLoader();
|
||||
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
@ -1490,6 +1490,7 @@ return array(
|
||||
'Rector\\Arguments\\ArgumentDefaultValueReplacer' => $baseDir . '/rules/Arguments/ArgumentDefaultValueReplacer.php',
|
||||
'Rector\\Arguments\\Contract\\ReplaceArgumentDefaultValueInterface' => $baseDir . '/rules/Arguments/Contract/ReplaceArgumentDefaultValueInterface.php',
|
||||
'Rector\\Arguments\\NodeAnalyzer\\ArgumentAddingScope' => $baseDir . '/rules/Arguments/NodeAnalyzer/ArgumentAddingScope.php',
|
||||
'Rector\\Arguments\\NodeAnalyzer\\ChangedArgumentsDetector' => $baseDir . '/rules/Arguments/NodeAnalyzer/ChangedArgumentsDetector.php',
|
||||
'Rector\\Arguments\\Rector\\ClassMethod\\ArgumentAdderRector' => $baseDir . '/rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php',
|
||||
'Rector\\Arguments\\Rector\\ClassMethod\\ReplaceArgumentDefaultValueRector' => $baseDir . '/rules/Arguments/Rector/ClassMethod/ReplaceArgumentDefaultValueRector.php',
|
||||
'Rector\\Arguments\\Rector\\FuncCall\\FunctionArgumentDefaultValueReplacerRector' => $baseDir . '/rules/Arguments/Rector/FuncCall/FunctionArgumentDefaultValueReplacerRector.php',
|
||||
|
14
vendor/composer/autoload_real.php
vendored
14
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit2ecd80168824b0ba5abef5ededdbc492
|
||||
class ComposerAutoloaderInit34f2d773ddf7d761551322a8fa07b86e
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,15 +22,15 @@ class ComposerAutoloaderInit2ecd80168824b0ba5abef5ededdbc492
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit2ecd80168824b0ba5abef5ededdbc492', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit34f2d773ddf7d761551322a8fa07b86e', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit2ecd80168824b0ba5abef5ededdbc492', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit34f2d773ddf7d761551322a8fa07b86e', 'loadClassLoader'));
|
||||
|
||||
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
|
||||
if ($useStaticLoader) {
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit2ecd80168824b0ba5abef5ededdbc492::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit34f2d773ddf7d761551322a8fa07b86e::getInitializer($loader));
|
||||
} else {
|
||||
$classMap = require __DIR__ . '/autoload_classmap.php';
|
||||
if ($classMap) {
|
||||
@ -42,19 +42,19 @@ class ComposerAutoloaderInit2ecd80168824b0ba5abef5ededdbc492
|
||||
$loader->register(true);
|
||||
|
||||
if ($useStaticLoader) {
|
||||
$includeFiles = Composer\Autoload\ComposerStaticInit2ecd80168824b0ba5abef5ededdbc492::$files;
|
||||
$includeFiles = Composer\Autoload\ComposerStaticInit34f2d773ddf7d761551322a8fa07b86e::$files;
|
||||
} else {
|
||||
$includeFiles = require __DIR__ . '/autoload_files.php';
|
||||
}
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequire2ecd80168824b0ba5abef5ededdbc492($fileIdentifier, $file);
|
||||
composerRequire34f2d773ddf7d761551322a8fa07b86e($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
}
|
||||
}
|
||||
|
||||
function composerRequire2ecd80168824b0ba5abef5ededdbc492($fileIdentifier, $file)
|
||||
function composerRequire34f2d773ddf7d761551322a8fa07b86e($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
require $file;
|
||||
|
9
vendor/composer/autoload_static.php
vendored
9
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit2ecd80168824b0ba5abef5ededdbc492
|
||||
class ComposerStaticInit34f2d773ddf7d761551322a8fa07b86e
|
||||
{
|
||||
public static $files = array (
|
||||
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
|
||||
@ -1850,6 +1850,7 @@ class ComposerStaticInit2ecd80168824b0ba5abef5ededdbc492
|
||||
'Rector\\Arguments\\ArgumentDefaultValueReplacer' => __DIR__ . '/../..' . '/rules/Arguments/ArgumentDefaultValueReplacer.php',
|
||||
'Rector\\Arguments\\Contract\\ReplaceArgumentDefaultValueInterface' => __DIR__ . '/../..' . '/rules/Arguments/Contract/ReplaceArgumentDefaultValueInterface.php',
|
||||
'Rector\\Arguments\\NodeAnalyzer\\ArgumentAddingScope' => __DIR__ . '/../..' . '/rules/Arguments/NodeAnalyzer/ArgumentAddingScope.php',
|
||||
'Rector\\Arguments\\NodeAnalyzer\\ChangedArgumentsDetector' => __DIR__ . '/../..' . '/rules/Arguments/NodeAnalyzer/ChangedArgumentsDetector.php',
|
||||
'Rector\\Arguments\\Rector\\ClassMethod\\ArgumentAdderRector' => __DIR__ . '/../..' . '/rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php',
|
||||
'Rector\\Arguments\\Rector\\ClassMethod\\ReplaceArgumentDefaultValueRector' => __DIR__ . '/../..' . '/rules/Arguments/Rector/ClassMethod/ReplaceArgumentDefaultValueRector.php',
|
||||
'Rector\\Arguments\\Rector\\FuncCall\\FunctionArgumentDefaultValueReplacerRector' => __DIR__ . '/../..' . '/rules/Arguments/Rector/FuncCall/FunctionArgumentDefaultValueReplacerRector.php',
|
||||
@ -3858,9 +3859,9 @@ class ComposerStaticInit2ecd80168824b0ba5abef5ededdbc492
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit2ecd80168824b0ba5abef5ededdbc492::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit2ecd80168824b0ba5abef5ededdbc492::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit2ecd80168824b0ba5abef5ededdbc492::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit34f2d773ddf7d761551322a8fa07b86e::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit34f2d773ddf7d761551322a8fa07b86e::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit34f2d773ddf7d761551322a8fa07b86e::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
10
vendor/scoper-autoload.php
vendored
10
vendor/scoper-autoload.php
vendored
@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
|
||||
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
|
||||
spl_autoload_call('RectorPrefix20210919\AutoloadIncluder');
|
||||
}
|
||||
if (!class_exists('ComposerAutoloaderInit2ecd80168824b0ba5abef5ededdbc492', false) && !interface_exists('ComposerAutoloaderInit2ecd80168824b0ba5abef5ededdbc492', false) && !trait_exists('ComposerAutoloaderInit2ecd80168824b0ba5abef5ededdbc492', false)) {
|
||||
spl_autoload_call('RectorPrefix20210919\ComposerAutoloaderInit2ecd80168824b0ba5abef5ededdbc492');
|
||||
if (!class_exists('ComposerAutoloaderInit34f2d773ddf7d761551322a8fa07b86e', false) && !interface_exists('ComposerAutoloaderInit34f2d773ddf7d761551322a8fa07b86e', false) && !trait_exists('ComposerAutoloaderInit34f2d773ddf7d761551322a8fa07b86e', false)) {
|
||||
spl_autoload_call('RectorPrefix20210919\ComposerAutoloaderInit34f2d773ddf7d761551322a8fa07b86e');
|
||||
}
|
||||
if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) {
|
||||
spl_autoload_call('RectorPrefix20210919\Helmich\TypoScriptParser\Parser\AST\Statement');
|
||||
@ -3311,9 +3311,9 @@ if (!function_exists('print_node')) {
|
||||
return \RectorPrefix20210919\print_node(...func_get_args());
|
||||
}
|
||||
}
|
||||
if (!function_exists('composerRequire2ecd80168824b0ba5abef5ededdbc492')) {
|
||||
function composerRequire2ecd80168824b0ba5abef5ededdbc492() {
|
||||
return \RectorPrefix20210919\composerRequire2ecd80168824b0ba5abef5ededdbc492(...func_get_args());
|
||||
if (!function_exists('composerRequire34f2d773ddf7d761551322a8fa07b86e')) {
|
||||
function composerRequire34f2d773ddf7d761551322a8fa07b86e() {
|
||||
return \RectorPrefix20210919\composerRequire34f2d773ddf7d761551322a8fa07b86e(...func_get_args());
|
||||
}
|
||||
}
|
||||
if (!function_exists('parseArgs')) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user