Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPStan error with mock method in 1.6.8 #1395

Closed
sakarikl opened this issue Mar 12, 2024 · 1 comment · Fixed by #1397
Closed

PHPStan error with mock method in 1.6.8 #1395

sakarikl opened this issue Mar 12, 2024 · 1 comment · Fixed by #1397
Assignees
Labels
Bug An error or unexpected behavior.
Milestone

Comments

@sakarikl
Copy link
Contributor

Mockery Version

1.6.8

PHP Version

PHP 8.3

Issue Description

phpstan does not detect return value anymore properly

Steps to Reproduce

class Test
{
    public function test() : string
    {
        return 'Test';
    }
}

$test = \Mockery::mock(Test::class);

$test->shouldReceive('test')->andReturn('Mocked');

echo $test->test();

Expected Behavior

No PHPStan errors

Actual Behavior

phpstan reports now following error: phpstan: Return type of call to static method Mockery::mock() contains unresolvable type.

Exception or Error

No response

Additional Information

No response

@sakarikl sakarikl added the triage needs to be triaged label Mar 12, 2024
@ghostwriter ghostwriter self-assigned this Mar 12, 2024
@sakarikl
Copy link
Contributor Author

sakarikl commented Mar 13, 2024

    /**
     * Static shortcut to Container::mock().
     *
     * @template TMock
     *
     * @param array<class-string<TMock>|TMock> $args
     *
     * @return LegacyMockInterface&MockInterface&TMock
     */
    public static function mock(...$args)
    {
        return self::getContainer()->mock(...$args);
    }

@ghostwriter I played around with phpdoc and with this I got PHPStan to dump correct type.

class Test
{
    public function asd() : string
    {
        return 'Original';
    }
}

$class = new Test();

$mock = \Mockery::mock(Test::class);
$mock2 = \Mockery::mock($class);

\PHPStan\dumpType($mock);
\PHPStan\dumpType($mock2);

$mock->shouldReceive('test')->andReturn('Mocked');

echo $mock->asd();
    
 ------ -------------------------------------------------- 
  Line   test.php                                          
 ------ -------------------------------------------------- 
  48     Dumped type: Mockery\MockInterface&TEST_PHP\Test  
  49     Dumped type: Mockery\MockInterface&TEST_PHP\Test  
 ------ -------------------------------------------------- 

@ghostwriter ghostwriter linked a pull request Mar 13, 2024 that will close this issue
@ghostwriter ghostwriter added Bug An error or unexpected behavior. and removed triage needs to be triaged labels Mar 13, 2024
@ghostwriter ghostwriter added this to the 1.6.10 milestone Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug An error or unexpected behavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants