2020-09-09 08:52:43 +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_;
|
2020-09-09 08:52:43 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\Foreach_;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Naming\Guard\BreakingVariableRenameGuard;
|
|
|
|
use Rector\Naming\Matcher\ForeachMatcher;
|
|
|
|
use Rector\Naming\Naming\ExpectedNameResolver;
|
|
|
|
use Rector\Naming\NamingConvention\NamingConventionAnalyzer;
|
|
|
|
use Rector\Naming\ValueObject\VariableAndCallForeach;
|
|
|
|
use Rector\Naming\VariableRenamer;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-09-09 08:52:43 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchMethodCallReturnTypeRector\RenameForeachValueVariableToMatchMethodCallReturnTypeRectorTest
|
2020-09-09 08:52:43 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RenameForeachValueVariableToMatchMethodCallReturnTypeRector extends AbstractRector
|
2020-09-09 08:52:43 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Naming\Guard\BreakingVariableRenameGuard
|
2020-09-09 08:52:43 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $breakingVariableRenameGuard;
|
2020-09-09 08:52:43 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Naming\Naming\ExpectedNameResolver
|
2020-09-09 08:52:43 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $expectedNameResolver;
|
2020-09-09 08:52:43 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Naming\NamingConvention\NamingConventionAnalyzer
|
2020-09-09 08:52:43 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $namingConventionAnalyzer;
|
2020-09-09 08:52:43 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Naming\VariableRenamer
|
2020-09-09 08:52:43 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $variableRenamer;
|
2020-09-09 08:52:43 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Naming\Matcher\ForeachMatcher
|
2020-09-09 08:52:43 +00:00
|
|
|
*/
|
2021-06-18 19:29:49 +00:00
|
|
|
private $foreachMatcher;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(BreakingVariableRenameGuard $breakingVariableRenameGuard, ExpectedNameResolver $expectedNameResolver, NamingConventionAnalyzer $namingConventionAnalyzer, VariableRenamer $variableRenamer, ForeachMatcher $foreachMatcher)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2020-09-09 08:52:43 +00:00
|
|
|
$this->breakingVariableRenameGuard = $breakingVariableRenameGuard;
|
2021-05-10 23:39:21 +00:00
|
|
|
$this->expectedNameResolver = $expectedNameResolver;
|
2020-09-09 08:52:43 +00:00
|
|
|
$this->namingConventionAnalyzer = $namingConventionAnalyzer;
|
2021-05-10 23:39:21 +00:00
|
|
|
$this->variableRenamer = $variableRenamer;
|
2021-06-18 19:29:49 +00:00
|
|
|
$this->foreachMatcher = $foreachMatcher;
|
2020-09-09 08:52:43 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-09-09 08:52:43 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Renames value variable name in foreach loop to match method type', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-09-09 08:52:43 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$array = [];
|
|
|
|
foreach ($object->getMethods() as $property) {
|
|
|
|
$array[] = $property;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-09-09 08:52:43 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$array = [];
|
|
|
|
foreach ($object->getMethods() as $method) {
|
|
|
|
$array[] = $method;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-09-09 08:52:43 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-09-09 08:52:43 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-09-09 08:52:43 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Foreach_::class];
|
2020-09-09 08:52:43 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Foreach_ $node
|
2020-09-09 08:52:43 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-09-09 08:52:43 +00:00
|
|
|
{
|
2021-06-18 19:29:49 +00:00
|
|
|
$variableAndCallForeach = $this->foreachMatcher->match($node);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$variableAndCallForeach instanceof VariableAndCallForeach) {
|
2020-09-09 08:52:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-03-01 01:28:35 +00:00
|
|
|
$expectedName = $this->expectedNameResolver->resolveForForeach($variableAndCallForeach->getCall());
|
2020-12-24 16:28:56 +00:00
|
|
|
if ($expectedName === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-03-01 01:28:35 +00:00
|
|
|
if ($this->isName($variableAndCallForeach->getVariable(), $expectedName)) {
|
2020-09-09 08:52:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-03-01 01:28:35 +00:00
|
|
|
if ($this->shouldSkip($variableAndCallForeach, $expectedName)) {
|
2020-09-09 08:52:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-03-01 01:28:35 +00:00
|
|
|
$this->renameVariable($variableAndCallForeach, $expectedName);
|
2020-09-09 08:52:43 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkip(VariableAndCallForeach $variableAndCallForeach, string $expectedName) : bool
|
2020-09-09 08:52:43 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if ($this->namingConventionAnalyzer->isCallMatchingVariableName($variableAndCallForeach->getCall(), $variableAndCallForeach->getVariableName(), $expectedName)) {
|
|
|
|
return \true;
|
2020-09-09 08:52:43 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return $this->breakingVariableRenameGuard->shouldSkipVariable($variableAndCallForeach->getVariableName(), $expectedName, $variableAndCallForeach->getFunctionLike(), $variableAndCallForeach->getVariable());
|
2020-09-09 08:52:43 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function renameVariable(VariableAndCallForeach $variableAndCallForeach, string $expectedName) : void
|
2020-09-09 08:52:43 +00:00
|
|
|
{
|
2021-05-29 22:10:59 +00:00
|
|
|
$this->variableRenamer->renameVariableInFunctionLike($variableAndCallForeach->getFunctionLike(), $variableAndCallForeach->getVariableName(), $expectedName, null);
|
2020-09-09 08:52:43 +00:00
|
|
|
}
|
|
|
|
}
|