mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 08:20:53 +00:00
[DowngradePhp80] Add DowngradeNamedArgumentRector (#133)
Co-authored-by: kaizen-ci <info@kaizen-ci.org>
This commit is contained in:
parent
42ca1883de
commit
02f9524e34
4
.github/workflows/build_scoped_rector.yaml
vendored
4
.github/workflows/build_scoped_rector.yaml
vendored
|
@ -35,6 +35,10 @@ jobs:
|
|||
# install only prod dependencies - do not use ramsey, it uses cache including "dev", we want to avoid it here
|
||||
- run: composer install --no-dev --ansi
|
||||
|
||||
# early downgrade individual files of symfony Attribute classes
|
||||
- run: bin/rector process vendor/symfony/dependency-injection/Attribute/Autoconfigure.php -c build/config/config-downgrade-php70.php --ansi
|
||||
- run: bin/rector process vendor/symfony/dependency-injection/Attribute/AutoconfigureTag.php -c build/config/config-downgrade-php70.php --ansi
|
||||
|
||||
# 1. copy files to $NESTED_DIRECTORY directory Exclude the scoped/nested directories to prevent rsync from copying in a loop
|
||||
- run: rsync --exclude rector-build -av * rector-build --quiet
|
||||
- run: rm -rf rector-build/packages-tests rector-build/rules-tests rector-build/tests
|
||||
|
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
use Rector\Core\Configuration\Option;
|
||||
use Rector\Core\ValueObject\PhpVersion;
|
||||
use Rector\DowngradePhp80\Rector\Catch_\DowngradeNonCapturingCatchesRector;
|
||||
use Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector;
|
||||
use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector;
|
||||
use Rector\DowngradePhp80\Rector\ClassConstFetch\DowngradeClassOnObjectToGetClassRector;
|
||||
use Rector\DowngradePhp80\Rector\ClassMethod\DowngradeStaticTypeDeclarationRector;
|
||||
|
@ -13,10 +14,13 @@ use Rector\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector;
|
|||
use Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrContainsRector;
|
||||
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector;
|
||||
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector;
|
||||
use Rector\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector;
|
||||
use Rector\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector;
|
||||
use Rector\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector;
|
||||
use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation;
|
||||
use Rector\Removing\Rector\Class_\RemoveInterfacesRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
use Symplify\SymfonyPhpConfig\ValueObjectInliner;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
|
@ -31,6 +35,13 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
],
|
||||
]]);
|
||||
|
||||
$services->set(DowngradeAttributeToAnnotationRector::class)
|
||||
->call('configure', [[
|
||||
DowngradeAttributeToAnnotationRector::ATTRIBUTE_TO_ANNOTATION => ValueObjectInliner::inline([
|
||||
new DowngradeAttributeToAnnotation('Attribute', 'Attribute'),
|
||||
]),
|
||||
]]);
|
||||
|
||||
$services->set(DowngradeUnionTypeTypedPropertyRector::class);
|
||||
$services->set(DowngradeUnionTypeDeclarationRector::class);
|
||||
$services->set(DowngradeMixedTypeDeclarationRector::class);
|
||||
|
@ -44,4 +55,5 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
$services->set(DowngradeTrailingCommasInParamUseRector::class);
|
||||
$services->set(\Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrStartsWithRector::class);
|
||||
$services->set(\Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrEndsWithRector::class);
|
||||
$services->set(DowngradeNamedArgumentRector::class);
|
||||
};
|
||||
|
|
|
@ -14,6 +14,10 @@ set -u
|
|||
|
||||
composer install --no-dev --ansi
|
||||
|
||||
# early downgrade individual files of symfony Attribute classes
|
||||
bin/rector process vendor/symfony/dependency-injection/Attribute/Autoconfigure.php -c build/config/config-downgrade-php70.php --ansi
|
||||
bin/rector process vendor/symfony/dependency-injection/Attribute/AutoconfigureTag.php -c build/config/config-downgrade-php70.php --ansi
|
||||
|
||||
rsync --exclude rector-build -av * rector-build --quiet
|
||||
rm -rf rector-build/packages-tests rector-build/rules-tests rector-build/tests
|
||||
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
|
||||
class AutoconfigureTag
|
||||
{
|
||||
public function __construct(string $name = null, array $attributes = [])
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\Fixture;
|
||||
|
||||
/**
|
||||
* @Attribute
|
||||
*/
|
||||
class AutoconfigureTag
|
||||
{
|
||||
public function __construct(string $name = null, array $attributes = [])
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -18,6 +18,10 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
'Symfony\Component\Routing\Annotation\Route'
|
||||
),
|
||||
new DowngradeAttributeToAnnotation('Symfony\Contracts\Service\Attribute\Required', 'required'),
|
||||
new DowngradeAttributeToAnnotation(
|
||||
'Attribute',
|
||||
'Attribute'
|
||||
),
|
||||
]),
|
||||
]]);
|
||||
};
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class DowngradeNamedArgumentRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
* @requires PHP 8.0
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
private function execute(?array $a = null, ?array $b = null)
|
||||
{
|
||||
}
|
||||
|
||||
public function run(string $name = null, array $attributes = [])
|
||||
{
|
||||
$this->execute(a: [[$name ?? 0 => $attributes]]);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
private function execute(?array $a = null, ?array $b = null)
|
||||
{
|
||||
}
|
||||
|
||||
public function run(string $name = null, array $attributes = [])
|
||||
{
|
||||
$this->execute([[$name ?? 0 => $attributes]]);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;
|
||||
|
||||
class Fixture2
|
||||
{
|
||||
private function execute(?array $a = null, ?array $b = null)
|
||||
{
|
||||
}
|
||||
|
||||
public function run(string $name = null, array $attributes = [])
|
||||
{
|
||||
$this->execute(b: [[$name ?? 0 => $attributes]]);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;
|
||||
|
||||
class Fixture2
|
||||
{
|
||||
private function execute(?array $a = null, ?array $b = null)
|
||||
{
|
||||
}
|
||||
|
||||
public function run(string $name = null, array $attributes = [])
|
||||
{
|
||||
$this->execute(null, [[$name ?? 0 => $attributes]]);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;
|
||||
|
||||
class FlipOrder
|
||||
{
|
||||
private function execute(?array $a = null, ?array $b = null)
|
||||
{
|
||||
var_dump($b);
|
||||
}
|
||||
|
||||
public function run(string $name = null, array $attributes = [])
|
||||
{
|
||||
$this->execute(b: [[$name ?? 0 => $attributes]], a: []);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;
|
||||
|
||||
class FlipOrder
|
||||
{
|
||||
private function execute(?array $a = null, ?array $b = null)
|
||||
{
|
||||
var_dump($b);
|
||||
}
|
||||
|
||||
public function run(string $name = null, array $attributes = [])
|
||||
{
|
||||
$this->execute([], [[$name ?? 0 => $attributes]]);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;
|
||||
|
||||
use Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Source\Foo;
|
||||
|
||||
class SkipNoArg extends Foo
|
||||
{
|
||||
public function __construct(string $name = null, array $attributes = [])
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;
|
||||
|
||||
use Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Source\Foo;
|
||||
|
||||
class SkipNoNamedArg extends Foo
|
||||
{
|
||||
public function __construct(string $name = null, array $attributes = [])
|
||||
{
|
||||
parent::__construct([], []);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;
|
||||
|
||||
use Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Source\Foo;
|
||||
|
||||
class StaticCall extends Foo
|
||||
{
|
||||
public function __construct(string $name = null, array $attributes = [])
|
||||
{
|
||||
parent::__construct(a: [[$name ?? 0 => $attributes]]);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;
|
||||
|
||||
use Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Source\Foo;
|
||||
|
||||
class StaticCall extends Foo
|
||||
{
|
||||
public function __construct(string $name = null, array $attributes = [])
|
||||
{
|
||||
parent::__construct([[$name ?? 0 => $attributes]]);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Source;
|
||||
|
||||
class Foo
|
||||
{
|
||||
public function __construct(
|
||||
public ?array $a = null,
|
||||
public ?array $b = null
|
||||
) {
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(DowngradeNamedArgumentRector::class);
|
||||
};
|
|
@ -0,0 +1,224 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DowngradePhp80\Rector\MethodCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Parser;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use Symplify\SmartFileSystem\SmartFileSystem;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\DowngradeNamedArgumentRectorTest
|
||||
*/
|
||||
final class DowngradeNamedArgumentRector extends AbstractRector
|
||||
{
|
||||
public function __construct(
|
||||
private ReflectionProvider $reflectionProvider,
|
||||
private SmartFileSystem $smartFileSystem,
|
||||
private Parser $parser
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [MethodCall::class, StaticCall::class];
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
'Remove named argument',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
private function execute(?array $a = null, ?array $b = null)
|
||||
{
|
||||
}
|
||||
|
||||
public function run(string $name = null, array $attributes = [])
|
||||
{
|
||||
$this->execute(a: [[$name ?? 0 => $attributes]]);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
private function execute(?array $a = null, ?array $b = null)
|
||||
{
|
||||
}
|
||||
|
||||
public function run(string $name = null, array $attributes = [])
|
||||
{
|
||||
$this->execute([[$name ?? 0 => $attributes]]);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall|StaticCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$args = $node->args;
|
||||
if ($this->shouldSkip($args)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->applyRemoveNamedArgument($node, $args);
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall|StaticCall $node
|
||||
* @param Arg[] $args
|
||||
*/
|
||||
private function applyRemoveNamedArgument(Node $node, array $args): ?Node
|
||||
{
|
||||
$caller = $this->getCaller($node);
|
||||
if (! $caller instanceof ClassMethod) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->processRemoveNamedArgument($caller, $node, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall|StaticCall $node
|
||||
*/
|
||||
private function getCaller(Node $node): ?Node
|
||||
{
|
||||
$caller = $node instanceof StaticCall
|
||||
? $this->nodeRepository->findClassMethodByStaticCall($node)
|
||||
: $this->nodeRepository->findClassMethodByMethodCall($node);
|
||||
|
||||
if ($caller instanceof ClassMethod) {
|
||||
return $caller;
|
||||
}
|
||||
|
||||
$type = $node instanceof StaticCall
|
||||
? $this->nodeTypeResolver->resolve($node->class)
|
||||
: $this->nodeTypeResolver->resolve($node->var);
|
||||
|
||||
if (! $type instanceof ObjectType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$classReflection = $this->reflectionProvider->getClass($type->getClassName());
|
||||
$fileName = $classReflection->getFileName();
|
||||
if (! is_string($fileName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$stmts = $this->parser->parse($this->smartFileSystem->readFile($fileName));
|
||||
/** @var ClassLike[] $classLikes */
|
||||
$classLikes = $this->betterNodeFinder->findInstanceOf((array) $stmts, ClassLike::class);
|
||||
|
||||
foreach ($classLikes as $classLike) {
|
||||
$caller = $classLike->getMethod((string) $this->getName($node->name));
|
||||
if (! $caller instanceof ClassMethod) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return $caller;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall|StaticCall $node
|
||||
* @param Arg[] $args
|
||||
*/
|
||||
private function processRemoveNamedArgument(ClassMethod $classMethod, Node $node, array $args): Expr
|
||||
{
|
||||
$params = $classMethod->params;
|
||||
/** @var Arg[] $newArgs */
|
||||
$newArgs = [];
|
||||
$keyParam = 0;
|
||||
|
||||
foreach ($params as $keyParam => $param) {
|
||||
/** @var string $paramName */
|
||||
$paramName = $this->getName($param);
|
||||
|
||||
foreach ($args as $arg) {
|
||||
/** @var string|null $argName */
|
||||
$argName = $this->getName($arg);
|
||||
|
||||
if ($paramName === $argName) {
|
||||
$newArgs[$keyParam] = new Arg(
|
||||
$arg->value,
|
||||
$arg->byRef,
|
||||
$arg->unpack,
|
||||
$arg->getAttributes(),
|
||||
null
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->replacePreviousArgs($node, $params, $keyParam, $newArgs);
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall|StaticCall $node
|
||||
* @param Param[] $params
|
||||
* @param Arg[] $newArgs
|
||||
*/
|
||||
private function replacePreviousArgs(Node $node, array $params, int $keyParam, array $newArgs): void
|
||||
{
|
||||
for ($i = $keyParam - 1; $i >= 0; --$i) {
|
||||
if (! isset($newArgs[$i]) && $params[$i]->default instanceof Expr) {
|
||||
$newArgs[$i] = new Arg($params[$i]->default);
|
||||
}
|
||||
}
|
||||
|
||||
$countNewArgs = count($newArgs);
|
||||
for ($i = 0; $i < $countNewArgs; ++$i) {
|
||||
$node->args[$i] = $newArgs[$i];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Arg[] $args
|
||||
*/
|
||||
private function shouldSkip(array $args): bool
|
||||
{
|
||||
if ($args === []) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($args as $arg) {
|
||||
if ($arg->name instanceof Identifier) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user