Skip to content

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

Description

@userator

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.

Activity

  1. VincentLanglet commented on Feb 26, 2026

    @VincentLanglet
    Contributor

    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 ?

  2. ondrejmirtes commented on Feb 26, 2026

    @ondrejmirtes
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions