You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PHPStan includes phpstan/phpstan-phpunit, but it doesn't seem to recognize mocks. #285
PHPStan includes phpstan/phpstan-phpunit, but it doesn't seem to recognize mocks.
phpstan-phpunit 2.0.16 ships with a stub file, MockObject.stub, which contains an empty MockObject interface without an expects() method.
When PHPStan analyzes your code, it uses the stub file instead of the real PHPUnit interface.
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
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.
The stub does not require to define all existing methods since the definition is merged with the original one.
Methods are only declared when we need to override the existing definition.
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:
Please give a real example in which you're having an issue.
Did you also install phpunit/phpunit with composer ?
My wild guess: you are seeing “Call to an undefined method” because for example you upgraded to PHPUnit 13 but you’re not running PHP 8.4, or you’ve set PHPStan phpVersion to lower than 8.4.
Activity
VincentLanglet commentedon Feb 26, 2026
The stub does not require to define all existing methods since the definition is merged with the original one.
Methods are only declared when we need to override the existing definition.
Please give a real example in which you're having an issue.
Did you also install phpunit/phpunit with composer ?
ondrejmirtes commentedon Feb 26, 2026
My wild guess: you are seeing “Call to an undefined method” because for example you upgraded to PHPUnit 13 but you’re not running PHP 8.4, or you’ve set PHPStan phpVersion to lower than 8.4.