Skip to content

[PHPUnit120] Skip stub-only ->method() calls in AllowMockObjectsForDataProviderRector - #790

Merged
TomasVotruba merged 1 commit into
rectorphp:mainfrom
orcnd:fix/stub-only-data-provider
Oct 3, 2026
Merged

TomasVotruba merged 1 commit into
rectorphp:mainfrom
orcnd:fix/stub-only-data-provider

Conversation

@orcnd

@orcnd orcnd commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Fixes rectorphp/rector#9920

What

AllowMockObjectsForDataProviderRector added #[AllowMockObjectsWithoutExpectations] to test classes whose data-provided tests only use createStub().

Why

MockObjectExprDetector::hasMethodCallWithoutExpects() matched any $var->method(...) call without checking the receiver's type. Stubs (PHPUnit\Framework\MockObject\Stub) never require expectations, so they should not trigger the attribute.

How

The detector now resolves the receiver's type. It skips ->method() calls on expressions that are definitely a Stub but not a MockObject. Unknown or mixed types keep the previous behaviour, so the change stays conservative.

Tests

  • New fixture skip_stub_only.php.inc. It failed before the fix and passes now.
  • The full phpunit suite (676 tests), ecs and rector pass.
  • phpstan reports the same 6 pre-existing errors as main, none of them in the touched file.

AI disclosure: this change was prepared with the help of Claude Code and reviewed by me.

🤖 Generated with Claude Code

…taProviderRector

createStub() returns a Stub, which never requires expectations, so a
data-provided test using only stubs must not get the
#[AllowMockObjectsWithoutExpectations] attribute.

Fixes rectorphp/rector#9920

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TomasVotruba
TomasVotruba merged commit 722063f into rectorphp:main Oct 3, 2026
8 checks passed
@TomasVotruba

Copy link
Copy Markdown
Member

LGTM, thanks

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.

False positive AllowMockObjectsForDataProviderRector

2 participants