2021-02-25 08:38:30 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Naming\Rector\Foreach_;
|
2021-02-25 08:38:30 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Stmt\Foreach_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Naming\ExpectedNameResolver\InflectorSingularResolver;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
|
|
|
|
use Rector\NodeManipulator\StmtsManipulator;
|
|
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-02-25 08:38:30 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector\RenameForeachValueVariableToMatchExprVariableRectorTest
|
2021-02-25 08:38:30 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RenameForeachValueVariableToMatchExprVariableRector extends AbstractRector
|
2021-02-25 08:38:30 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Naming\ExpectedNameResolver\InflectorSingularResolver
|
2021-02-25 08:38:30 +00:00
|
|
|
*/
|
2021-03-05 10:55:40 +00:00
|
|
|
private $inflectorSingularResolver;
|
2021-11-14 08:50:26 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeAnalyzer\PropertyFetchAnalyzer
|
2021-11-14 08:50:26 +00:00
|
|
|
*/
|
|
|
|
private $propertyFetchAnalyzer;
|
2023-07-10 10:56:25 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeManipulator\StmtsManipulator
|
2023-07-10 10:56:25 +00:00
|
|
|
*/
|
|
|
|
private $stmtsManipulator;
|
2023-09-20 12:55:35 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
2023-09-20 12:55:35 +00:00
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
|
|
|
public function __construct(InflectorSingularResolver $inflectorSingularResolver, PropertyFetchAnalyzer $propertyFetchAnalyzer, StmtsManipulator $stmtsManipulator, BetterNodeFinder $betterNodeFinder)
|
2021-02-25 08:38:30 +00:00
|
|
|
{
|
2021-03-05 10:55:40 +00:00
|
|
|
$this->inflectorSingularResolver = $inflectorSingularResolver;
|
2021-11-14 08:50:26 +00:00
|
|
|
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
|
2023-07-10 10:56:25 +00:00
|
|
|
$this->stmtsManipulator = $stmtsManipulator;
|
2023-09-20 12:55:35 +00:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2021-02-25 08:38:30 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-02-25 08:38:30 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Renames value variable name in foreach loop to match expression variable', [new CodeSample(<<<'CODE_SAMPLE'
|
2021-02-25 08:38:30 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
2022-07-19 21:15:00 +00:00
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$array = [];
|
|
|
|
foreach ($variables as $property) {
|
|
|
|
$array[] = $property;
|
|
|
|
}
|
2021-02-25 08:38:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2021-02-25 08:38:30 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
2022-07-19 21:15:00 +00:00
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$array = [];
|
|
|
|
foreach ($variables as $variable) {
|
|
|
|
$array[] = $variable;
|
|
|
|
}
|
2021-02-25 08:38:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2021-02-25 08:38:30 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2021-02-25 08:38:30 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2021-02-25 08:38:30 +00:00
|
|
|
{
|
2023-07-10 10:56:25 +00:00
|
|
|
return [StmtsAwareInterface::class];
|
2021-02-25 08:38:30 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-07-10 10:56:25 +00:00
|
|
|
* @param StmtsAwareInterface $node
|
2021-02-25 08:38:30 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2021-02-25 08:38:30 +00:00
|
|
|
{
|
2023-07-10 10:56:25 +00:00
|
|
|
if ($node->stmts === null) {
|
2021-02-25 08:38:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-07-10 10:56:25 +00:00
|
|
|
$hasChanged = \false;
|
|
|
|
foreach ($node->stmts as $key => $stmt) {
|
|
|
|
if (!$stmt instanceof Foreach_) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$isPropertyFetch = $this->propertyFetchAnalyzer->isLocalPropertyFetch($stmt->expr);
|
|
|
|
if (!$stmt->expr instanceof Variable && !$isPropertyFetch) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$exprName = $this->getName($stmt->expr);
|
|
|
|
if ($exprName === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($stmt->keyVar instanceof Node) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$valueVarName = $this->getName($stmt->valueVar);
|
|
|
|
if ($valueVarName === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$singularValueVarName = $this->inflectorSingularResolver->resolve($exprName);
|
|
|
|
if ($singularValueVarName === $exprName) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($singularValueVarName === $valueVarName) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$alreadyUsedVariable = $this->betterNodeFinder->findVariableOfName($stmt->stmts, $singularValueVarName);
|
|
|
|
if ($alreadyUsedVariable instanceof Variable) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, $singularValueVarName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, $valueVarName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->processRename($stmt, $valueVarName, $singularValueVarName);
|
|
|
|
$hasChanged = \true;
|
2021-04-20 12:21:02 +00:00
|
|
|
}
|
2023-07-10 10:56:25 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
2021-02-25 08:38:30 +00:00
|
|
|
}
|
2023-07-10 10:56:25 +00:00
|
|
|
return null;
|
2021-02-27 11:32:42 +00:00
|
|
|
}
|
2023-07-10 10:56:25 +00:00
|
|
|
private function processRename(Foreach_ $foreach, string $valueVarName, string $singularValueVarName) : void
|
2021-02-27 11:32:42 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$foreach->valueVar = new Variable($singularValueVarName);
|
|
|
|
$this->traverseNodesWithCallable($foreach->stmts, function (Node $node) use($singularValueVarName, $valueVarName) : ?Variable {
|
|
|
|
if (!$node instanceof Variable) {
|
2021-02-25 08:38:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isName($node, $valueVarName)) {
|
2021-02-25 08:38:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Variable($singularValueVarName);
|
2021-02-25 08:38:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|