Skip to content

Commit c3fc63b

Browse files
committed
early return test
1 parent 40edc70 commit c3fc63b

3 files changed

Lines changed: 14 additions & 36 deletions

File tree

‎rules/DeadCode/Rector/ClassMethod/RemoveUselessParamTagRector.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function getNodeTypes(): array
7474
public function refactor(Node $node): ?Node
7575
{
7676
// skip as no comments
77-
if ($node->getComments() === []) {
77+
if (! str_contains((string) $node->getDocComment(), '@param')) {
7878
return null;
7979
}
8080

‎rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnTagRector.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public function getNodeTypes(): array
7474
*/
7575
public function refactor(Node $node): ?Node
7676
{
77+
// early return if no @return tag is present
78+
if (! str_contains((string) $node->getDocComment(), '@return')) {
79+
return null;
80+
}
81+
7782
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
7883
$hasChanged = $this->returnTagRemover->removeReturnTagIfUseless($phpDocInfo, $node);
7984
if (! $hasChanged) {

‎src/PhpParser/NodeTraverser/RectorNodeTraverser.php‎

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ private function traverseNode(Node $node): void
142142
continue;
143143
}
144144

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

148147
foreach ($currentNodeVisitors as $currentNodeVisitor) {
@@ -162,29 +161,17 @@ private function traverseNode(Node $node): void
162161
// stop traversing as node type changed and visitors won't work
163162
continue 2;
164163
}
165-
166-
// } elseif ($return === NodeVisitor::DONT_TRAVERSE_CHILDREN) {
167-
// $traverseChildren = false;
168-
// } elseif ($return === NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN) {
169-
// $traverseChildren = false;
170-
// break;
171-
// } elseif ($return === NodeVisitor::STOP_TRAVERSAL) {
172-
// $this->stopTraversal = true;
173-
// break 2;
174-
// } elseif ($return === NodeVisitor::REPLACE_WITH_NULL) {
175-
// $node->{$name} = null;
176-
// continue 2;
177164
} else {
178165
throw new LogicException('RectorRunner::run() returned invalid value of type ' . gettype($return));
179166
}
180167
}
181168

182-
if ($traverseChildren) {
183-
$this->traverseNode($subNode);
184-
if ($this->stopTraversal) {
185-
break;
186-
}
169+
// if ($traverseChildren) {
170+
$this->traverseNode($subNode);
171+
if ($this->stopTraversal) {
172+
break;
187173
}
174+
// }
188175
}
189176
}
190177

@@ -225,29 +212,15 @@ private function traverseArray(array $nodes): array
225212
} elseif ($return === NodeVisitor::REMOVE_NODE) {
226213
$doNodes[] = [$i, []];
227214
continue 2;
228-
} elseif ($return === NodeVisitor::DONT_TRAVERSE_CHILDREN) {
229-
$traverseChildren = false;
230-
} elseif ($return === NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN) {
231-
$traverseChildren = false;
232-
break;
233-
} elseif ($return === NodeVisitor::STOP_TRAVERSAL) {
234-
$this->stopTraversal = true;
235-
break 2;
236-
} elseif ($return === NodeVisitor::REPLACE_WITH_NULL) {
237-
throw new LogicException(
238-
'REPLACE_WITH_NULL can not be used if the parent structure is an array'
239-
);
240215
} else {
241216
throw new LogicException('RectorRunner::run() returned invalid value of type ' . gettype($return));
242217
}
243218
}
244219
}
245220

246-
if ($traverseChildren) {
247-
$this->traverseNode($node);
248-
if ($this->stopTraversal) {
249-
break;
250-
}
221+
$this->traverseNode($node);
222+
if ($this->stopTraversal) {
223+
break;
251224
}
252225
}
253226

0 commit comments

Comments
 (0)