Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Feature request: Validate `with` and `willReturn` types on mocks

Open
#321 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
php
Domain
testing

Research direction

Start with the PHPStan/PHPUnit integration and the example in the issue, since no files or tests are named. Done means detecting mismatched parameter types passed to with and an incompatible return value supplied to willReturn, with coverage demonstrating both reports.

Written by the indexing model from the issue text.

Description

Given the following example, i'd expect PHPStan to report the following issues:

  • The parameter types in with don't match the function signature
  • The return value does not match the function signature.

This would be usefull for projects with a large test suite, where you do a refactor of a method that is used in a lot of places. It would allow to fix these issues before running the entire test suite to get the errors from PHPUnit.

class T1 {
    public function myMethod(int $a, string $b): int {
        return 3;
    }
}

class T1Test extends TestCase {
    public function testStuff(): void {
        $mock = $this->createMock(T1::class);

        $mock->expects($this->once())
            ->method('myMethod')
            ->with(
                new \stdClass(),
                new \stdClass(),
            )->willReturn(true);
    }
}
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.