Skip to content

Commit

Permalink
Skip all but exactly one return (#2565)
Browse files Browse the repository at this point in the history
* add failing fixture

* skip all but exactly one return
  • Loading branch information
TomasVotruba committed Jun 25, 2022
1 parent 7391b85 commit 237f603
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector\Fixture;

final class SkipRootReturnWithNestedReturn
{
public function run(array $values)
{
foreach ($values as $value) {
return $value;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersion;
use Rector\TypeDeclaration\TypeAnalyzer\AlwaysStrictBoolExprAnalyzer;
Expand Down Expand Up @@ -80,12 +80,26 @@ public function refactor(Node $node): ?Node
return $node;
}

public function provideMinPhpVersion(): int
{
return PhpVersion::PHP_74;
}

private function hasSingleStrictReturn(ClassMethod $classMethod): bool
{
if ($classMethod->stmts === null) {
return false;
}

if ($this->betterNodeFinder->hasInstancesOf($classMethod->stmts, [Yield_::class])) {
return false;
}

$returns = $this->betterNodeFinder->findInstanceOf($classMethod->stmts, Return_::class);
if (count($returns) !== 1) {
return false;
}

foreach ($classMethod->stmts as $stmt) {
if (! $stmt instanceof Return_) {
continue;
Expand All @@ -103,9 +117,4 @@ private function hasSingleStrictReturn(ClassMethod $classMethod): bool

return false;
}

public function provideMinPhpVersion(): int
{
return PhpVersion::PHP_74;
}
}

0 comments on commit 237f603

Please sign in to comment.