Skip to content

[CodeQuality] Skip mock property removal when used in a trait#723

Merged
TomasVotruba merged 1 commit into
mainfrom
never-used-mock-property-trait-check
Jul 13, 2026
Merged

[CodeQuality] Skip mock property removal when used in a trait#723
TomasVotruba merged 1 commit into
mainfrom
never-used-mock-property-trait-check

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

RemoveNeverUsedMockPropertyRector removed a mock property when it saw no local uses. But a used trait can reference that property, so removal broke the code.

Now resolves each used trait's AST and skips removal if the trait fetches the property.

Skipped case

trait UsesMockPropertyTrait
{
    public function useMock(): void
    {
        $this->mockProperty->someMethod();
    }
}

final class SkipIfUsedInTrait extends TestCase
{
    use UsesMockPropertyTrait;

    private MockObject $mockProperty;

    protected function setUp(): void
    {
        $this->mockProperty = $this->createMock(\stdClass::class);
        $this->mockProperty->expects($this->once())
            ->method('someMethod')
            ->willReturn('someValue');
    }
}

Before this change the $mockProperty (and its setUp() assignment) got removed, breaking UsesMockPropertyTrait. Now it is kept.

@TomasVotruba TomasVotruba merged commit 188445b into main Jul 13, 2026
8 checks passed
@TomasVotruba TomasVotruba deleted the never-used-mock-property-trait-check branch July 13, 2026 09:54
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