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

[Feature request] Report willReturn values incompatible with method signature

Open
#48 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
php

Research direction

Start with the PHP example using createMock(), method('foo'), and willReturn(123), then trace the existing PHPStan PHPUnit rules that inspect mock configurations. Confirm how the declared string return type and the configured int value should be compared; done means PHPStan reports the incompatible willReturn value with an actionable diagnostic.

Written by the indexing model from the issue text.

Description

Given

class MyClass
{
    public function foo(): string {}
}

class MyOtherClassTest extends \PHPUnit\Framework\TestCase
{
    public function testFooMock()
    {
        $class = $this->createMock(MyClass::class);
        $class
            ->expects(static::once())
            ->method('foo')
            ->willReturn(123)
        ;
    }
}

PHPUnit reports:

PHPUnit 8.2.0 by Sebastian Bergmann and contributors.

W                                                                  1 /  1 (100%)

Time: 173 ms, Memory: 16.00 MB

There was 1 warning:

1) MyOtherClassTest::testFooMock
Method foo may not return value of type int

WARNINGS!
Tests:  1, Assertions:    0, Warnings: 1.

It would be awesome if PHPStan could report this errors too.

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.