Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WillReturnCallbackFallbackToReturnFalseRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipNeverReturnCallAfterBoolReturns extends TestCase
{
public function test()
{
$matcher = $this->exactly(2);

$eventMock = $this->createMock(\stdClass::class);
$eventMock->expects($matcher)->method('checkContext')->willReturnCallback(function (string $id) use ($matcher) {
if ($id === 'service') {
return true;
}

if ($id === 'other') {
return false;
}

self::fail('Unknown ID requested: ' . $id);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WillReturnCallbackFallbackToReturnFalseRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipThrowAfterBoolReturns extends TestCase
{
public function test()
{
$matcher = $this->exactly(2);

$eventMock = $this->createMock(\stdClass::class);
$eventMock->expects($matcher)->method('checkContext')->willReturnCallback(function (string $id) use ($matcher) {
if ($id === 'service') {
return true;
}

if ($id === 'other') {
return false;
}

throw new \Exception();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\NeverType;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -132,6 +137,12 @@ public function refactor(Node $node): ?MethodCall
return null;
}

// a closure that always throws or ends in a never-returning call
// cannot fall through, so no "return false" fallback is needed
if ($lastStmt instanceof Stmt && $this->isTerminated($lastStmt)) {
return null;
}

if (! $this->hasOnlyBoolReturns($closure)) {
return null;
}
Expand All @@ -141,6 +152,23 @@ public function refactor(Node $node): ?MethodCall
return $node;
}

private function isTerminated(Stmt $stmt): bool
{
if ($stmt instanceof Expression) {
$stmt = $stmt->expr;
}

if ($stmt instanceof Throw_) {
return true;
}

if (! $stmt instanceof Expr) {
return false;
}

return $this->nodeTypeResolver->getNativeType($stmt) instanceof NeverType;
}

private function hasOnlyBoolReturns(Closure $closure): bool
{
/** @var Return_[] $returns */
Expand Down