Skip to content

[CodeQuality] Add WillReturnCallbackFallbackToReturnFalseRector - #784

Merged
TomasVotruba merged 1 commit into
mainfrom
add-will-return-callback-return-false-fallback
Sep 17, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
add-will-return-callback-return-false-fallback

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Adds a code quality rule that appends a return false; fallback to a willReturnCallback() closure that only returns booleans but can fall through without a return.

Before:

$eventMock->expects($matcher)->method('checkContext')->willReturnCallback(function (...$parameters) use ($matcher) {
    if (1 === $matcher->numberOfInvocations()) {
        return true;
    }
    if (2 === $matcher->numberOfInvocations()) {
        return false;
    }
});

After:

$eventMock->expects($matcher)->method('checkContext')->willReturnCallback(function (...$parameters) use ($matcher) {
    if (1 === $matcher->numberOfInvocations()) {
        return true;
    }
    if (2 === $matcher->numberOfInvocations()) {
        return false;
    }

    return false;
});

Rule triggers only when every explicit return in the closure returns true/false and the body does not already end with a return. Registered in the phpunit-code-quality set.

Note: sibling WillReturnCallbackFallbackToThrowRector covers the same missing-fallback shape but throws; it is deprecated. This one targets the common bool-returning mock case with a concrete default.

…d return false fallback on bool willReturnCallback()
@TomasVotruba
TomasVotruba merged commit b640d62 into main Sep 17, 2026
7 checks passed
@TomasVotruba
TomasVotruba deleted the add-will-return-callback-return-false-fallback branch September 17, 2026 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant