Skip to content

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

@userator

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.

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