Skip to content

Commit

Permalink
[CodeQuality] Skip do { } while always returned on ExplicitReturnNull…
Browse files Browse the repository at this point in the history
…Rector (#5760)
  • Loading branch information
samsonasik authored Mar 23, 2024
1 parent 8874f25 commit cae0691
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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 SkipDoWhileAlwaysReturned
{
public function run(int $i)
{
do {
if (rand(0,1)) {
return 1;
}

return 2;
} while (++$i < 1);
}
}
5 changes: 5 additions & 0 deletions 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\ClassMethod;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Finally_;
Expand Down Expand Up @@ -92,6 +93,10 @@ private function hasStmtsAlwaysReturnOrExit(array $stmts): bool
if ($this->isIfReturn($stmt)) {
return true;
}

if ($stmt instanceof Do_ && $this->hasStmtsAlwaysReturnOrExit($stmt->stmts)) {
return true;
}
}

return false;
Expand Down

0 comments on commit cae0691

Please sign in to comment.