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
Expand Up @@ -142,7 +142,7 @@ public function refactor(Node $node): ?Node
return null;
}

/** @var Expression<Assign|AssignOp> $previousStmt */
/** @var Assign|AssignOp $assign */
$assign = $previousStmt->expr;

return $this->processSimplifyUselessVariable($node, $stmt, $assign, $key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function getNodeTypes(): array
/**
* @param If_ $node
*/
public function refactor(Node $node): ?Node
public function refactor(Node $node): ?Expression
{
if (! $node->else instanceof Else_) {
return null;
Expand Down
38 changes: 18 additions & 20 deletions src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,6 @@

abstract class AbstractImmutableNodeTraverser implements NodeTraverserInterface
{
/**
* @deprecated Use NodeVisitor::DONT_TRAVERSE_CHILDREN instead.
*/
public const DONT_TRAVERSE_CHILDREN = NodeVisitor::DONT_TRAVERSE_CHILDREN;

/**
* @deprecated Use NodeVisitor::STOP_TRAVERSAL instead.
*/
public const STOP_TRAVERSAL = NodeVisitor::STOP_TRAVERSAL;

/**
* @deprecated Use NodeVisitor::REMOVE_NODE instead.
*/
public const REMOVE_NODE = NodeVisitor::REMOVE_NODE;

/**
* @deprecated Use NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN instead.
*/
public const DONT_TRAVERSE_CURRENT_AND_CHILDREN = NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;

/**
* @var list<NodeVisitor> Visitors
*/
Expand Down Expand Up @@ -131,13 +111,22 @@ protected function traverseNode(Node $node): void
$traverseChildren = true;
$visitorIndex = -1;
$currentNodeVisitors = $this->getVisitorsForNode($subNode);

foreach ($currentNodeVisitors as $visitorIndex => $visitor) {
$return = $visitor->enterNode($subNode);
if ($return !== null) {
if ($return instanceof Node) {

$originalNodeClass = $subNode::class;

$this->ensureReplacementReasonable($subNode, $return);
$subNode = $return;
$node->{$name} = $return;

if ($originalNodeClass !== $return::class) {
$traverseChildren = false;
break;
}
} elseif ($return === NodeVisitor::DONT_TRAVERSE_CHILDREN) {
$traverseChildren = false;
} elseif ($return === NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN) {
Expand Down Expand Up @@ -210,12 +199,21 @@ protected function traverseArray(array $nodes): array
$traverseChildren = true;
$visitorIndex = -1;
$currentNodeVisitors = $this->getVisitorsForNode($node);

foreach ($currentNodeVisitors as $visitorIndex => $visitor) {
$return = $visitor->enterNode($node);
if ($return !== null) {
if ($return instanceof Node) {
$this->ensureReplacementReasonable($node, $return);

// hos node type changed?
$originalNodeCLass = $node::class;
$nodes[$i] = $node = $return;

if ($originalNodeCLass !== $return::class) {
$traverseChildren = false;
break;
}
} elseif (\is_array($return)) {
$doNodes[] = [$i, $return];
continue 2;
Expand Down
20 changes: 5 additions & 15 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ public function beforeTraverse(array $nodes): ?array
*/
final public function enterNode(Node $node): int|Node|null
{
if (! $this->isMatchingNodeType($node)) {
return null;
}
// if previous Rector rule changed the node type, we have to skip it here
// as here we expect still the original type
// if (! $this->isMatchingNodeType($node)) {
// return null;
//}

if (is_a($this, HTMLAverseRectorInterface::class, true) && $this->file->containsHTML()) {
return null;
Expand Down Expand Up @@ -319,16 +321,4 @@ private function refreshScopeNodes(array | Node $node, string $filePath, ?Mutati
$this->changedNodeScopeRefresher->refresh($node, $filePath, $mutatingScope);
}
}

private function isMatchingNodeType(Node $node): bool
{
$nodeClass = $node::class;
foreach ($this->getNodeTypes() as $nodeType) {
if (is_a($nodeClass, $nodeType, true)) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withRules(
[
SimplifyIfElseToTernaryRector::class,
SimplifyUselessVariableRector::class,
CompleteDynamicPropertiesRector::class,
]
);
->withRules([
SimplifyIfElseToTernaryRector::class,
SimplifyUselessVariableRector::class,
CompleteDynamicPropertiesRector::class,
]);
Loading