mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-30 03:47:53 +01:00
Bugfix for SimplifyForeachToArrayFilterRector (#5859)
This commit is contained in:
parent
c1c6c9aaf2
commit
b84f76e4e4
@ -160,7 +160,7 @@ abstract class AbstractRectorTestCase extends AbstractKernelTestCase
|
||||
|
||||
$inputFileInfo = $inputFileInfoAndExpectedFileInfo->getInputFileInfo();
|
||||
|
||||
// needed for PHPStan, because the analyzed file is just create in /temp
|
||||
// needed for PHPStan, because the analyzed file is just created in /temp
|
||||
/** @var NodeScopeResolver $nodeScopeResolver */
|
||||
$nodeScopeResolver = $this->getService(NodeScopeResolver::class);
|
||||
$nodeScopeResolver->setAnalysedFiles([$inputFileInfo->getRealPath()]);
|
||||
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\Foreach_\SimplifyForeachToArrayFilterRector\Fixture;
|
||||
|
||||
$responseData = [];
|
||||
foreach ($responseData as $key => $value) {
|
||||
if (is_string($value)) {
|
||||
$responseData[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -9,6 +9,7 @@ use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
@ -73,11 +74,7 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
|
||||
if (count($funcCallNode->args) !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->nodeComparator->areNodesEqual($funcCallNode->args[0], $node->valueVar)) {
|
||||
if (! $this->isSimpleCall($funcCallNode, $node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -90,7 +87,8 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $onlyNodeInIf->var instanceof ArrayDimFetch) {
|
||||
$arrayDimFetch = $onlyNodeInIf->var;
|
||||
if (! $arrayDimFetch instanceof ArrayDimFetch) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -103,7 +101,11 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->createAssignNode($node, $name, $onlyNodeInIf->var);
|
||||
if (! $this->forLoopFillsAnotherArray($node, $arrayDimFetch)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->createAssignNode($node, $name, $arrayDimFetch);
|
||||
}
|
||||
|
||||
private function shouldSkip(Foreach_ $foreach): bool
|
||||
@ -139,4 +141,27 @@ CODE_SAMPLE
|
||||
|
||||
return new Assign($arrayDimFetch->var, $arrayFilterFuncCall);
|
||||
}
|
||||
|
||||
private function forLoopFillsAnotherArray(Foreach_ $node, ArrayDimFetch $arrayDimFetch): bool
|
||||
{
|
||||
$loopVar = $node->expr;
|
||||
if (! $loopVar instanceof Variable) {
|
||||
return false;
|
||||
}
|
||||
$varThatIsModified = $arrayDimFetch->var;
|
||||
if (! $varThatIsModified instanceof Variable) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $loopVar->name !== $varThatIsModified->name;
|
||||
}
|
||||
|
||||
private function isSimpleCall(FuncCall $funcCallNode, Foreach_ $foreach): bool
|
||||
{
|
||||
if (count($funcCallNode->args) !== 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->nodeComparator->areNodesEqual($funcCallNode->args[0], $foreach->valueVar);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user