Skip to content
Merged
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
3 changes: 0 additions & 3 deletions config/sets/phpunit-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@
NarrowSingleWillReturnCallbackRector::class,
SingleWithConsecutiveToWithRector::class,

// enable once better tested
// WillReturnCallbackFallbackToThrowRector::class,

// type declarations
TypeWillReturnCallableArrowFunctionRector::class,
VoidMethodWithCallbackToWillReturnCallbackRector::class,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,18 @@
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\ClosureUse;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use Rector\PHPUnit\Enum\ConsecutiveVariable;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\NodeFactory\MatcherInvocationCountMethodCallNodeFactory;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WillReturnCallbackFallbackToThrowRector\WillReturnCallbackFallbackToThrowRectorTest
* @deprecated This rule is deprecated as the case is very rare, hardly automatable and it is not part of any set. Handle it in a custom way or keep the code untouched.
*/
final class WillReturnCallbackFallbackToThrowRector extends AbstractRector
final class WillReturnCallbackFallbackToThrowRector extends AbstractRector implements DeprecatedInterface
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly MatcherInvocationCountMethodCallNodeFactory $matcherInvocationCountMethodCallNodeFactory,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -99,93 +82,9 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?MethodCall
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

if (! $this->isName($node->name, 'willReturnCallback')) {
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

if (count($node->getArgs()) !== 1) {
return null;
}

$closure = $node->getArgs()[0]
->value;
if (! $closure instanceof Closure) {
return null;
}

if (! $this->usesMatcher($closure)) {
return null;
}

// the closure must branch on the matcher invocation count first
if (! $this->hasConsecutiveIf($closure)) {
return null;
}

$lastStmt = $closure->stmts[array_key_last($closure->stmts)] ?? null;

// the last statement must be an if branch; an explicit fallback "return <expr>;" is left untouched
if (! $lastStmt instanceof If_) {
return null;
}

$closure->stmts[] = $this->createThrow();

return $node;
}

private function usesMatcher(Closure $closure): bool
{
return array_any(
$closure->uses,
fn (ClosureUse $use): bool => $this->isName($use->var, ConsecutiveVariable::MATCHER)
);
}

private function hasConsecutiveIf(Closure $closure): bool
{
foreach ($closure->stmts as $stmt) {
if (! $stmt instanceof If_) {
continue;
}

$hasMatcherCall = false;
$this->traverseNodesWithCallable($stmt->cond, function (Node $node) use (&$hasMatcherCall): null {
if ($node instanceof MethodCall
&& $node->var instanceof Variable
&& $this->isName($node->var, ConsecutiveVariable::MATCHER)
) {
$hasMatcherCall = true;
}

return null;
});

if ($hasMatcherCall) {
return true;
}
}

return false;
}

private function createThrow(): Expression
{
$sprintfFuncCall = $this->nodeFactory->createFuncCall('sprintf', [
new String_('Method should not be called for the %dth time'),
$this->matcherInvocationCountMethodCallNodeFactory->create(),
]);

$new = new New_(new FullyQualified('PHPUnit\Framework\Exception'), [new Arg($sprintfFuncCall)]);

return new Expression(new Throw_($new));
throw new ShouldNotHappenException(sprintf(
'"%s" is deprecated and should not be used anymore. Remove it from your config files.',
self::class,
));
}
}
Loading