Skip to content

Conversation

@mikadamczyk
Copy link
Contributor

@mikadamczyk mikadamczyk commented Jan 30, 2026

🎫 Issue N/A

Description:

This PR adds two opt‑in PHPStan rules that enforce correct typing for PHPUnit mocks:

  • RequireMockObjectInPropertyTypeRule - catches properties typed only via @var …&MockObject and requires an intersection type on the property itself.
  • RequireConcreteTypeForMockReturnRule - Flags methods that return a mock but declare only MockObject as the return type. (Returning a concrete type, even when the value is a mock, is allowed.)

How to enable in a package
The rules are opt‑in via a dedicated include file:

phpstan.neon in your package:

  includes:
    - vendor/ibexa/phpstan/extension.neon
    - vendor/ibexa/phpstan/extension-mocks.neon

Examples

  • private ContentService&MockObject $contentService;
  • private function createContentInfo(): ContentInfo&MockObject
  • private function createContentInfo(): ContentInfo (allowed if you don’t need MockObject typing)
  • /** @var ContentService&MockObject */ private ContentService $contentService;
  • private function createContentInfo(): ContentInfo (when it returns $this->createMock())

Additional notes

  • Test fixtures for the rules live under tests/rules/data/ and are excluded from the phpstan package analysis via excludePaths.
  • Added .gitignore to keep local caches and vendor/ out of version control.

For QA:

Documentation:

@mikadamczyk mikadamczyk self-assigned this Jan 30, 2026
@mikadamczyk mikadamczyk requested review from a team and konradoboza January 30, 2026 15:24
@konradoboza konradoboza requested a review from a team February 2, 2026 07:55
Base automatically changed from fix-require-closure to main February 2, 2026 10:58
@konradoboza konradoboza requested a review from a team February 10, 2026 08:59
@mikadamczyk mikadamczyk merged commit 805cfed into main Feb 10, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants