Skip to content

Commit f08e956

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

4 files changed

Lines changed: 20 additions & 43 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) {

‎rules/Php53/Rector/Variable/ReplaceHttpServerVarsByServerRector.php‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,16 @@ public function getNodeTypes(): array
5656
*/
5757
public function refactor(Node $node): ?Node
5858
{
59-
foreach (self::VARIABLE_RENAME_MAP as $oldName => $newName) {
60-
if (! $this->isName($node, $oldName)) {
61-
continue;
62-
}
59+
$variableName = $this->getName($node);
60+
if ($variableName === null) {
61+
return null;
62+
}
6363

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

68-
return null;
68+
$node->name = self::VARIABLE_RENAME_MAP[$variableName];
69+
return $node;
6970
}
7071
}

‎src/PhpParser/NodeTraverser/RectorNodeTraverser.php‎

Lines changed: 6 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,28 +161,14 @@ 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+
$this->traverseNode($subNode);
170+
if ($this->stopTraversal) {
171+
break;
187172
}
188173
}
189174
}
@@ -225,29 +210,15 @@ private function traverseArray(array $nodes): array
225210
} elseif ($return === NodeVisitor::REMOVE_NODE) {
226211
$doNodes[] = [$i, []];
227212
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-
);
240213
} else {
241214
throw new LogicException('RectorRunner::run() returned invalid value of type ' . gettype($return));
242215
}
243216
}
244217
}
245218

246-
if ($traverseChildren) {
247-
$this->traverseNode($node);
248-
if ($this->stopTraversal) {
249-
break;
250-
}
219+
$this->traverseNode($node);
220+
if ($this->stopTraversal) {
221+
break;
251222
}
252223
}
253224

0 commit comments

Comments
 (0)