phpstan / phpstan-phpunit

PHPUnit extensions and rules for PHPStan
MIT License
468 stars 46 forks source link

False positive of unused use inside willReturnCallback() and match() #190

Closed cheldt closed 1 year ago

cheldt commented 1 year ago

Example:

public function testBaz(): void
{
    $barMock = $this->createMock(Bar::class);

    $matcher = self::exactly(2);
    $barMock->expects($matcher)->method('bar')
        ->willReturnCallback(
            static function (string $test) use ($matcher): string {
                match ($matcher->numberOfInvocations()) {
                    1 => self::assertEquals('test1', $test),
                    2 => self::assertEquals('test2', $test),
                    default => new \LogicException()
                };

                return $test;
            }
        );

    $sut = new Foo($barMock);

    $sut->baz('test1');
    $sut->baz('test2');
}
 ------ ------------------------------------------------ 
  Line   tests/FoobarTest.php                            
 ------ ------------------------------------------------ 
  18     Anonymous function has an unused use $matcher.  
 ------ ------------------------------------------------ 

Context

I slapped a tiny test-project together, which demonstrates the issue: https://github.com/cheldt/unused

Thanks for any help :)

ondrejmirtes commented 1 year ago

Fixed in PHPStan 1.10.21 https://github.com/phpstan/phpstan/releases/tag/1.10.21

cheldt commented 1 year ago

Thanks!

github-actions[bot] commented 1 year ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.