Skip to content

Commit

Permalink
[DX] Remove autowrapping of expr/stmt to make return values in the sa…
Browse files Browse the repository at this point in the history
…me type and reliable (#4466)
  • Loading branch information
TomasVotruba committed Jul 9, 2023
1 parent 5c85bb6 commit a771c56
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
7 changes: 6 additions & 1 deletion rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ public function refactor(Node $node)
return null;
}

return $this->refactorFuncCall($node->expr, true);
$assignOrCast = $this->refactorFuncCall($node->expr, true);
if (! $assignOrCast instanceof Expr) {
return null;
}

return new Expression($assignOrCast);
}

return $this->refactorFuncCall($node, false);
Expand Down
4 changes: 3 additions & 1 deletion rules/Php72/Rector/Assign/ListEachRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public function refactor(Node $node)
// only key: list($key, ) = each($values);
if ($listNode->items[0] instanceof ArrayItem && ! $listNode->items[1] instanceof ArrayItem) {
$keyFuncCall = $this->nodeFactory->createFuncCall('key', $eachFuncCall->args);
return new Assign($listNode->items[0]->value, $keyFuncCall);
$keyFuncCallAssign = new Assign($listNode->items[0]->value, $keyFuncCall);

return new Expression($keyFuncCallAssign);
}

// only value: list(, $value) = each($values);
Expand Down
7 changes: 0 additions & 7 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\InlineHTML;
use PhpParser\Node\Stmt\Nop;
use PhpParser\NodeTraverser;
Expand Down Expand Up @@ -354,10 +351,6 @@ private function postRefactorProcess(Node $originalNode, Node $node, Node|array|
return $originalNode;
}

$refactoredNode = $originalNode instanceof Stmt && $refactoredNode instanceof Expr
? new Expression($refactoredNode)
: $refactoredNode;

$this->updateParentNodes($refactoredNode, $parentNode);
$this->refreshScopeNodes($refactoredNode, $filePath, $currentScope);

Expand Down

0 comments on commit a771c56

Please sign in to comment.