-
-
Notifications
You must be signed in to change notification settings - Fork 680
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add FuncGetArgsToVariadicParamRector (#6318)
- Loading branch information
1 parent
2e6d188
commit 21bf53f
Showing
15 changed files
with
486 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
...le/Rector/ClassMethod/FuncGetArgsToVariadicParamRector/Fixture/skip_already_param.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector\Fixture; | ||
|
||
function skip_already_param($someParam) | ||
{ | ||
$args = \func_get_args(); | ||
} |
27 changes: 27 additions & 0 deletions
27
.../Rector/ClassMethod/FuncGetArgsToVariadicParamRector/Fixture/skip_property_assign.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector\Fixture; | ||
|
||
final class SomePropertyAssign | ||
{ | ||
public function run() | ||
{ | ||
$this->someProperty = \func_get_args(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector\Fixture; | ||
|
||
final class SomePropertyAssign | ||
{ | ||
public function run(...$args) | ||
{ | ||
$this->someProperty = $args; | ||
} | ||
} | ||
|
||
?> |
20 changes: 20 additions & 0 deletions
20
...odingStyle/Rector/ClassMethod/FuncGetArgsToVariadicParamRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector\Fixture; | ||
|
||
function run() | ||
{ | ||
$args = \func_get_args(); | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector\Fixture; | ||
|
||
function run(...$args) | ||
{ | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...tor/ClassMethod/FuncGetArgsToVariadicParamRector/FuncGetArgsToVariadicParamRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class FuncGetArgsToVariadicParamRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
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'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...odingStyle/Rector/ClassMethod/FuncGetArgsToVariadicParamRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(FuncGetArgsToVariadicParamRector::class); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
rules/CodingStyle/Rector/ClassMethod/FuncGetArgsToVariadicParamRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodingStyle\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\FunctionLike; | ||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Function_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\ValueObject\PhpVersionFeature; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://3v4l.org/d4tBd | ||
* | ||
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector\FuncGetArgsToVariadicParamRectorTest | ||
*/ | ||
final class FuncGetArgsToVariadicParamRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Refactor func_get_args() in to a variadic param', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
function run() | ||
{ | ||
$args = \func_get_args(); | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
function run(...$args) | ||
{ | ||
} | ||
CODE_SAMPLE | ||
|
||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class, Function_::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod|Function_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isAtLeastPhpVersion(PhpVersionFeature::VARIADIC_PARAM)) { | ||
return null; | ||
} | ||
|
||
if ($node->params !== []) { | ||
return null; | ||
} | ||
|
||
$assign = $this->matchFuncGetArgsVariableAssign($node); | ||
if (! $assign instanceof Assign) { | ||
return null; | ||
} | ||
|
||
if ($assign->var instanceof Variable) { | ||
$variableName = $this->getName($assign->var); | ||
if ($variableName === null) { | ||
return null; | ||
} | ||
|
||
$this->removeNode($assign); | ||
} else { | ||
$variableName = 'args'; | ||
$assign->expr = new Variable('args'); | ||
} | ||
|
||
$node->params[] = $this->createVariadicParam($variableName); | ||
|
||
return $node; | ||
} | ||
|
||
/** | ||
* @param ClassMethod|Function_ $functionLike | ||
*/ | ||
private function matchFuncGetArgsVariableAssign(FunctionLike $functionLike): ?Assign | ||
{ | ||
/** @var Assign[] $assigns */ | ||
$assigns = $this->betterNodeFinder->findInstanceOf((array) $functionLike->stmts, Assign::class); | ||
|
||
foreach ($assigns as $assign) { | ||
if (! $assign->expr instanceof FuncCall) { | ||
continue; | ||
} | ||
|
||
if (! $this->isName($assign->expr, 'func_get_args')) { | ||
continue; | ||
} | ||
|
||
return $assign; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function createVariadicParam(string $variableName): Param | ||
{ | ||
$variable = new Variable($variableName); | ||
return new Param($variable, null, null, false, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters