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 @@ -74,7 +74,7 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
// skip as no comments
if ($node->getComments() === []) {
if (! str_contains((string) $node->getDocComment(), '@param')) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
// early return if no @return tag is present
if (! str_contains((string) $node->getDocComment(), '@return')) {
return null;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$hasChanged = $this->returnTagRemover->removeReturnTagIfUseless($phpDocInfo, $node);
if (! $hasChanged) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
foreach (self::VARIABLE_RENAME_MAP as $oldName => $newName) {
if (! $this->isName($node, $oldName)) {
continue;
}
$variableName = $this->getName($node);
if ($variableName === null) {
return null;
}

$node->name = $newName;
return $node;
if (! isset(self::VARIABLE_RENAME_MAP[$variableName])) {
return null;
}

return null;
$node->name = self::VARIABLE_RENAME_MAP[$variableName];
return $node;
}
}
63 changes: 18 additions & 45 deletions src/PhpParser/NodeTraverser/RectorNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,46 +142,33 @@ private function traverseNode(Node $node): void
continue;
}

$traverseChildren = true;
$currentNodeVisitors = $this->getVisitorsForNode($subNode);

foreach ($currentNodeVisitors as $currentNodeVisitor) {
$return = $this->rectorRunner->run($currentNodeVisitor, $subNode);
if ($return !== null) {
if ($return instanceof Node) {
$originalSubNodeClass = $subNode::class;
if ($return === null) {
continue;
}

$this->ensureReplacementReasonable($subNode, $return);
$subNode = $return;
$node->{$name} = $return;
if ($return instanceof Node) {
$originalSubNodeClass = $subNode::class;

if ($originalSubNodeClass !== $subNode::class) {
// stop traversing as node type changed and visitors won't work
continue 2;
}
$this->ensureReplacementReasonable($subNode, $return);
$subNode = $return;
$node->{$name} = $return;

} elseif ($return === NodeVisitor::DONT_TRAVERSE_CHILDREN) {
$traverseChildren = false;
} elseif ($return === NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN) {
$traverseChildren = false;
break;
} elseif ($return === NodeVisitor::STOP_TRAVERSAL) {
$this->stopTraversal = true;
break 2;
} elseif ($return === NodeVisitor::REPLACE_WITH_NULL) {
$node->{$name} = null;
if ($originalSubNodeClass !== $subNode::class) {
// stop traversing as node type changed and visitors won't work
continue 2;
} else {
throw new LogicException('RectorRunner::run() returned invalid value of type ' . gettype($return));
}
} else {
throw new LogicException('RectorRunner::run() returned invalid value of type ' . gettype($return));
}
}

if ($traverseChildren) {
$this->traverseNode($subNode);
if ($this->stopTraversal) {
break;
}
$this->traverseNode($subNode);
if ($this->stopTraversal) {
break;
}
}
}
Expand Down Expand Up @@ -223,29 +210,15 @@ private function traverseArray(array $nodes): array
} elseif ($return === NodeVisitor::REMOVE_NODE) {
$doNodes[] = [$i, []];
continue 2;
} elseif ($return === NodeVisitor::DONT_TRAVERSE_CHILDREN) {
$traverseChildren = false;
} elseif ($return === NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN) {
$traverseChildren = false;
break;
} elseif ($return === NodeVisitor::STOP_TRAVERSAL) {
$this->stopTraversal = true;
break 2;
} elseif ($return === NodeVisitor::REPLACE_WITH_NULL) {
throw new LogicException(
'REPLACE_WITH_NULL can not be used if the parent structure is an array'
);
} else {
throw new LogicException('RectorRunner::run() returned invalid value of type ' . gettype($return));
}
}
}

if ($traverseChildren) {
$this->traverseNode($node);
if ($this->stopTraversal) {
break;
}
$this->traverseNode($node);
if ($this->stopTraversal) {
break;
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/Rector/RectorRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(
}

/**
* @return int|Node|Node[]|null
* @return NodeVisitor::REMOVE_NODE|Node|Node[]|null
*/
public function run(RectorInterface $rector, Node $node): int|Node|null|array
{
Expand Down Expand Up @@ -100,8 +100,7 @@ public function run(RectorInterface $rector, Node $node): int|Node|null|array

// only remove node is supported
if ($refactoredNodeOrState !== NodeVisitor::REMOVE_NODE) {
// @todo warn about unsupported state in the future
return null;
throw new ShouldNotHappenException(sprintf('Unsupported state "%d" returned from "%s".', $refactoredNodeOrState, $rector::class));
}

// notify this rule changed code
Expand Down
Loading