Skip to content

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

Closed
@cheldt

Description

@cheldt

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

  • php 8.2.7
  • phpunit/phpunit 10.2.2
  • phpstan/phpstan-phpunit 1.3.13
  • phpstan/phpstan-deprecation-rules 1.1.3
  • phpstan/phpstan-strict-rules 1.5

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

Thanks for any help :)

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