Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;

final class SkipRecursiveDimFetchClosure
{
public function setFormTheme($form, $formView): void
{
$findThemes = function ($form, $formView) use (&$findThemes): void {
foreach ($form as $name => $field) {
$fieldView = $formView[$name];

if ($field->count()) {
$findThemes($field, $fieldView);
}
}
};

$findThemes($form, $formView);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -138,6 +139,10 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function

$paramName = $this->getName($param);

if ($this->isDimFetchResultFedToSelfCall($functionLike, $paramName)) {
return false;
}

$isParamAccessedArrayDimFetch = false;
$this->traverseNodesWithCallable($functionLike->stmts, function (Node $node) use (
$paramName,
Expand Down Expand Up @@ -201,6 +206,111 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function
return $isParamAccessedArrayDimFetch;
}

/**
* Skip when a value read from $param[dim] is fed back as an argument into the same
* closure/function (recursion). That element-as-container use signals an ArrayAccess
* tree (e.g. Symfony FormView), not a plain array.
*/
private function isDimFetchResultFedToSelfCall(
ClassMethod|Function_|Closure $functionLike,
string $paramName
): bool {
if ($functionLike->stmts === null) {
return false;
}

$selfCallNames = $this->resolveSelfCallNames($functionLike);
if ($selfCallNames === []) {
return false;
}

// collect variables assigned from $param[dim]
$dimFetchedVariableNames = [];
$this->traverseNodesWithCallable($functionLike->stmts, function (Node $node) use (
$paramName,
&$dimFetchedVariableNames
): null {
if ($node instanceof Assign
&& $node->var instanceof Variable
&& $node->expr instanceof ArrayDimFetch
&& $node->expr->var instanceof Variable
&& $this->isName($node->expr->var, $paramName)) {
$dimFetchedVariableNames[] = $this->getName($node->var);
}

return null;
});

$isFed = false;
$this->traverseNodesWithCallable($functionLike->stmts, function (Node $node) use (
$selfCallNames,
$dimFetchedVariableNames,
$paramName,
&$isFed
): null {
if ($isFed) {
return null;
}

if (! $node instanceof FuncCall || $node->isFirstClassCallable()) {
return null;
}

if (! $node->name instanceof Variable && ! $node->name instanceof Name) {
return null;
}

if (! in_array($this->getName($node->name), $selfCallNames, true)) {
return null;
}

foreach ($node->getArgs() as $arg) {
if ($arg->value instanceof Variable
&& in_array($this->getName($arg->value), $dimFetchedVariableNames, true)) {
$isFed = true;
return null;
}

if ($arg->value instanceof ArrayDimFetch
&& $arg->value->var instanceof Variable
&& $this->isName($arg->value->var, $paramName)) {
$isFed = true;
return null;
}
}

return null;
});

return $isFed;
}

/**
* @return string[]
*/
private function resolveSelfCallNames(ClassMethod|Function_|Closure $functionLike): array
{
if ($functionLike instanceof Closure) {
$selfCallNames = [];
foreach ($functionLike->uses as $use) {
if ($use->byRef) {
$useName = $this->getName($use->var);
if ($useName !== null) {
$selfCallNames[] = $useName;
}
}
}

return $selfCallNames;
}

if ($functionLike instanceof Function_) {
return [$functionLike->name->toString()];
}

return [];
}

private function isEchoed(Node $node, string $paramName): bool
{
if (! $node instanceof Echo_) {
Expand Down
Loading