[rector] add form default data rule: handle case when there are method call on builder without add

This commit is contained in:
Julien Fastré 2023-06-18 21:37:52 +02:00
parent 21f11fb034
commit 9073f118db
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB

View File

@ -138,7 +138,7 @@ PHP */
}
['build_form_method' => $buildFormMethod, 'empty_to_replace' => $emptyToReplace]
= $this->filterBuildFormMethod($node->stmts[$buildFormStmtIndex]);
= $this->filterBuildFormMethod($node->stmts[$buildFormStmtIndex], $node);
$node->stmts = [
...$stmtBefore,
@ -172,7 +172,7 @@ PHP */
* @param Node\Stmt\ClassMethod $buildFormMethod
* @return array{"build_form_method": Node\Stmt\ClassMethod, "empty_to_replace": array<string, mixed>}
*/
private function filterBuildFormMethod(Node\Stmt\ClassMethod $buildFormMethod): array
private function filterBuildFormMethod(Node\Stmt\ClassMethod $buildFormMethod, Node\Stmt\Class_ $node): array
{
$builderName = $buildFormMethod->params[0]->var->name;
@ -183,7 +183,7 @@ PHP */
if ($stmt instanceof Node\Stmt\Expression
// it must be a method call
&& $stmt->expr instanceof Node\Expr\MethodCall
&& false !== ($results = $this->handleMethodCallBuilderAdd($stmt->expr, $builderName))
&& false !== ($results = $this->handleMethodCallBuilderAdd($stmt->expr, $builderName, $node))
) {
['stmt' => $newMethodCAll, 'emptyDataToReplace' => $newEmptyDataToReplace] = $results;
$newStmts[] = new Node\Stmt\Expression($newMethodCAll);
@ -198,7 +198,7 @@ PHP */
return ['build_form_method' => $buildFormMethod, "empty_to_replace" => $emptyDataToReplace];
}
private function handleMethodCallBuilderAdd(Node\Expr\MethodCall $methodCall, string $builderName): array|false
private function handleMethodCallBuilderAdd(Node\Expr\MethodCall $methodCall, string $builderName, Node\Stmt\Class_ $node): array|false
{
$emptyDataToReplace = [];
// check for chained method call
@ -208,7 +208,7 @@ PHP */
) {
// as this is chained, we make a recursion on this method
$resultFormDeepMethodCall = $this->handleMethodCallBuilderAdd($methodCall->var, $builderName);
$resultFormDeepMethodCall = $this->handleMethodCallBuilderAdd($methodCall->var, $builderName, $node);
if (false === $resultFormDeepMethodCall) {
return false;
@ -268,6 +268,6 @@ PHP */
return ['stmt' => $methodCall, 'emptyDataToReplace' => $emptyDataToReplace];
}
throw new \RuntimeException("Not supported situation");
return ['stmt' => $methodCall, 'emptyDataToReplace' => $emptyDataToReplace];
}
}