Skip to content

Commit

Permalink
[CodeQuality] Skip Switch_ always returned on ExplicitReturnNullRector (
Browse files Browse the repository at this point in the history
#5800)

* [CodeQuality] Skip Switch_ always returned on ExplicitReturnNullRector

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user authored Apr 3, 2024
1 parent 88648d1 commit 0348124
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture;

final class SkipSwitchAlwaysReturn
{
public function run(int $number)
{
switch ($number) {
case 1:
return 1;
case 2:
default:
return 2;
}
}
}
5 changes: 4 additions & 1 deletion rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Case_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Continue_;
use PhpParser\Node\Stmt\Do_;
Expand Down Expand Up @@ -166,8 +167,10 @@ private function isSwitchWithAlwaysReturnOrExit(Switch_ $switch): bool

$casesWithReturnOrExitCount = $this->resolveReturnOrExitCount($switch);

$cases = array_filter($switch->cases, static fn(Case_ $case): bool => $case->stmts !== []);

// has same amount of first return or exit nodes as switches
return count($switch->cases) === $casesWithReturnOrExitCount;
return count($cases) === $casesWithReturnOrExitCount;
}

private function isTryCatchAlwaysReturnOrExit(TryCatch $tryCatch): bool
Expand Down

0 comments on commit 0348124

Please sign in to comment.