Skip to content
Merged
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
Expand Up @@ -204,14 +204,15 @@ private function refactorClassMethod(
return null;
}

$this->refactorStmtsAwareNode(
$hasChangedNode = $this->refactorStmtsAwareNode(
$node,
$templateDoctrineAnnotationTagValueNode,
$hasThisRenderOrReturnsResponse,
$classMethod
);

$hasChanged = true;
if ($hasChangedNode) {
$hasChanged = true;
}

return null;
});
Expand Down Expand Up @@ -256,10 +257,10 @@ private function refactorReturn(
DoctrineAnnotationTagValueNode $templateDoctrineAnnotationTagValueNode,
bool $hasThisRenderOrReturnsResponse,
ClassMethod $classMethod
): void {
): bool {
// nothing we can do
if (! $return->expr instanceof Expr) {
return;
return false;
}

// create "$this->render('template.file.twig.html', ['key' => 'value']);" method call
Expand All @@ -269,7 +270,7 @@ private function refactorReturn(
$classMethod
);

$this->refactorReturnWithValue(
return $this->refactorReturnWithValue(
$return,
$hasThisRenderOrReturnsResponse,
$thisRenderMethodCall,
Expand All @@ -295,7 +296,7 @@ private function refactorReturnWithValue(
MethodCall $thisRenderMethodCall,
ClassMethod $classMethod,
DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode
): void {
): bool {
/** @var Expr $lastReturnExpr */
$lastReturnExpr = $return->expr;

Expand All @@ -309,7 +310,7 @@ private function refactorReturnWithValue(
$return->expr = $thisRenderMethodCall;
} elseif ($returnStaticType instanceof MixedType) {
// nothing we can do
return;
return false;
}

$isArrayOrResponseType = $this->arrayUnionResponseTypeAnalyzer->isArrayUnionResponseType(
Expand All @@ -319,12 +320,13 @@ private function refactorReturnWithValue(

// skip as the original class method has to change first
if ($isArrayOrResponseType) {
return;
return false;
}

// already response
$this->removeDoctrineAnnotationTagValueNode($classMethod, $doctrineAnnotationTagValueNode);
$this->returnTypeDeclarationUpdater->updateClassMethod($classMethod, SymfonyClass::RESPONSE);
return true;
}

private function removeDoctrineAnnotationTagValueNode(
Expand All @@ -342,27 +344,32 @@ private function refactorStmtsAwareNode(
DoctrineAnnotationTagValueNode $templateDoctrineAnnotationTagValueNode,
bool $hasThisRenderOrReturnsResponse,
ClassMethod $classMethod
): void {
): bool {
if ($stmtsAware->stmts === null) {
return;
return false;
}

$hasChanged = false;
foreach ($stmtsAware->stmts as $stmt) {
if (! $stmt instanceof Return_) {
continue;
}

// just created node, skip it
if ($stmt->getAttributes() === []) {
return;
return false;
}

$this->refactorReturn(
$hasChangedReturn = $this->refactorReturn(
$stmt,
$templateDoctrineAnnotationTagValueNode,
$hasThisRenderOrReturnsResponse,
$classMethod
);
if ($hasChangedReturn) {
$hasChanged = true;
}
}
return $hasChanged;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,17 @@ public function refactor(Node $node): ?Node
return null;
}

$this->refactorOptionsArray($optionsArray);
$hasChanged = $this->refactorOptionsArray($optionsArray);

if (! $hasChanged) {
return null;
}
return $node;
}

private function refactorOptionsArray(Array_ $optionsArray): void
private function refactorOptionsArray(Array_ $optionsArray): bool
{
$hasChanged = false;
foreach ($optionsArray->items as $arrayItem) {
if (! $arrayItem instanceof ArrayItem) {
continue;
Expand All @@ -135,7 +139,9 @@ private function refactorOptionsArray(Array_ $optionsArray): void
}

$arrayItem->key = new String_($newName);
$hasChanged = true;
}
}
return $hasChanged;
}
}
14 changes: 11 additions & 3 deletions rules/Symfony42/Rector/New_/StringToArrayArgumentProcessRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ private function processArgumentPosition(New_|MethodCall $node, int $argumentPos
return null;
}

$this->processStringType($node, $argumentPosition, $activeArgValue);
$hasChanged = $this->processStringType($node, $argumentPosition, $activeArgValue);

if (! $hasChanged) {
return null;
}
return $node;
}

Expand All @@ -121,25 +125,29 @@ private function shouldSkipProcessMethodCall(MethodCall $methodCall): bool
return in_array($methodName, self::EXCLUDED_PROCESS_METHOD_CALLS, true);
}

private function processStringType(New_|MethodCall $expr, int $argumentPosition, Expr $firstArgumentExpr): void
private function processStringType(New_|MethodCall $expr, int $argumentPosition, Expr $firstArgumentExpr): bool
{
if ($firstArgumentExpr instanceof Concat) {
$arrayNode = $this->nodeTransformer->transformConcatToStringArray($firstArgumentExpr);
$expr->args[$argumentPosition] = new Arg($arrayNode);
return;
return true;
}

$args = $expr->getArgs();

$hasChanged = false;
if ($firstArgumentExpr instanceof FuncCall && $this->isName($firstArgumentExpr, 'sprintf')) {
$arrayNode = $this->nodeTransformer->transformSprintfToArray($firstArgumentExpr);
if ($arrayNode instanceof Array_) {
$args[$argumentPosition]->value = $arrayNode;
$hasChanged = true;
}
} elseif ($firstArgumentExpr instanceof String_) {
$parts = $this->splitProcessCommandToItems($firstArgumentExpr->value);
$args[$argumentPosition]->value = $this->nodeFactory->createArray($parts);
$hasChanged = true;
}
return $hasChanged;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public function refactor(Node $node): ?Node
return null;
}

$this->hasChanged = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a case of shared state where the results of a previous test would affect the next tests. By adding this statement we make sure that all tests run independently


$this->refactorReturnTypeDeclaration($executeClassMethod);
$this->addReturn0ToExecuteClassMethod($executeClassMethod);

Expand Down
Loading