mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
[CodingStyle] Add UnSpreadOperatorRector (#5015)
Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
parent
a7544e85e1
commit
bd1bd30762
@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\CodingStyle\Tests\Rector\ClassMethod\UnSpreadOperatorRector\UnSpreadOperatorRectorTest
|
||||
*/
|
||||
final class UnSpreadOperatorRector extends AbstractRector
|
||||
{
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Remove spread operator', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run(...$array)
|
||||
{
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
$this->run(...$data);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run(array $array)
|
||||
{
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
$this->run($data);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassMethod::class, MethodCall::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|MethodCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node instanceof ClassMethod) {
|
||||
return $this->processUnspreadOperatorClassMethodParams($node);
|
||||
}
|
||||
|
||||
return $this->processUnspreadOperatorMethodCallArgs($node);
|
||||
}
|
||||
|
||||
private function processUnspreadOperatorClassMethodParams(ClassMethod $classMethod): ?ClassMethod
|
||||
{
|
||||
$params = $classMethod->params;
|
||||
if ($params === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$spreadVariables = $this->getSpreadVariables($params);
|
||||
if ($spreadVariables === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (array_keys($spreadVariables) as $key) {
|
||||
$classMethod->params[$key]->variadic = false;
|
||||
$classMethod->params[$key]->type = new Identifier('array');
|
||||
}
|
||||
|
||||
return $classMethod;
|
||||
}
|
||||
|
||||
private function processUnspreadOperatorMethodCallArgs(MethodCall $methodCall): ?MethodCall
|
||||
{
|
||||
$args = $methodCall->args;
|
||||
if ($args === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$spreadVariables = $this->getSpreadVariables($args);
|
||||
if ($spreadVariables === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (array_keys($spreadVariables) as $key) {
|
||||
$methodCall->args[$key]->unpack = false;
|
||||
}
|
||||
|
||||
return $methodCall;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Param[]|Arg[] $array
|
||||
* @return Param[]|Arg[]
|
||||
*/
|
||||
private function getSpreadVariables(array $array): array
|
||||
{
|
||||
$spreadVariables = [];
|
||||
foreach ($array as $key => $paramOrArg) {
|
||||
if ($paramOrArg instanceof Param) {
|
||||
if (! $paramOrArg->variadic) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($paramOrArg->type !== null) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if ($paramOrArg instanceof Arg && ! $paramOrArg->unpack) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$spreadVariables[$key] = $paramOrArg;
|
||||
}
|
||||
|
||||
return $spreadVariables;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\UnSpreadOperatorRector\Fixture;
|
||||
|
||||
final class Fixture
|
||||
{
|
||||
public function run(...$args)
|
||||
{
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
$this->run(...$data);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\UnSpreadOperatorRector\Fixture;
|
||||
|
||||
final class Fixture
|
||||
{
|
||||
public function run(array $args)
|
||||
{
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
$this->run($data);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\UnSpreadOperatorRector\Fixture;
|
||||
|
||||
final class SkipNoParamsOrArgs
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$this->execute();
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\UnSpreadOperatorRector\Fixture;
|
||||
|
||||
final class SkipNoSpreadVariables
|
||||
{
|
||||
public function run(array $data)
|
||||
{
|
||||
$this->execute($data);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\UnSpreadOperatorRector\Fixture;
|
||||
|
||||
final class SkipTypedParamVariadic
|
||||
{
|
||||
public function run(array ...$var)
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\UnSpreadOperatorRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\CodingStyle\Rector\ClassMethod\UnSpreadOperatorRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class UnSpreadOperatorRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return UnSpreadOperatorRector::class;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user