PHPStan includes phpstan/phpstan-phpunit, but it doesn't seem to recognize mocks.

Open
#285 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
php
Domain
testing

Research direction

Start with vendor/phpstan/phpstan-phpunit/stubs/MockObject.stub and compare it with vendor/phpunit/phpunit/src/Framework/MockObject/Runtime/Interface/MockObject.php. Check how PHPStan uses the stub, then update the stub so the MockObject interface exposes the expects() method shown in the PHPUnit interface. Done means PHPStan recognizes expects() on PHPUnit mock objects.

Written by the indexing model from the issue text.

Description

Problem

PHPStan includes phpstan/phpstan-phpunit, but it doesn't seem to recognize mocks.

  1. phpstan-phpunit 2.0.16 ships with a stub file, MockObject.stub, which contains an empty MockObject interface without an expects() method.
  2. When PHPStan analyzes your code, it uses the stub file instead of the real PHPUnit interface.
  3. This causes PHPStan to not see the expects() method on mock objects.
    This is a bug in phpstan-phpunit 2.0.16. The stub file, MockObject.stub, must contain a definition of the expects() method.

Reason

Bug in phpstan/phpstan-phpunit version 2.0.16
The stub file /home/mihail/proj/task/vendor/phpstan/phpstan-phpunit/stubs/MockObject.stub contains an empty MockObject interface without an expects() method:
// vendor/phpstan/phpstan-phpunit/stubs/MockObject.stub

namespace PHPUnit\Framework\MockObject;
interface MockObject
{
// EMPTY - no expects() method!
}

However, the real MockObject interface in PHPUnit 11.x has an expects() method:
// vendor/phpunit/phpunit/src/Framework/MockObject/Runtime/Interface/MockObject.php

interface MockObject extends Stub
{
public function expects(InvocationOrder $invocationRule): InvocationMocker;
}

PHPStan uses the stub file instead of the real class, so it doesn't see the expects() method.

Solution

Correct the stub file in phpstan-phpunit. You need to add the expects() method to MockObject.stub.

Dominant language
PHP
Stars
544
Forks
58
Avg merge
2d 1h
Merged PRs (30d)
5

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from phpstan/phpstan-phpunit

All issues in phpstan/phpstan-phpunit

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.