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

Invalid Mockery::mock()/spy() phpdoc (= phpstan errors) #1400

Closed
LastDragon-ru opened this issue Mar 20, 2024 · 6 comments · Fixed by #1401
Closed

Invalid Mockery::mock()/spy() phpdoc (= phpstan errors) #1400

LastDragon-ru opened this issue Mar 20, 2024 · 6 comments · Fixed by #1401
Labels
Bug An error or unexpected behavior.
Milestone

Comments

@LastDragon-ru
Copy link
Contributor

LastDragon-ru commented Mar 20, 2024

Mockery Version

1.6.10

PHP Version

PHP 8.3

Issue Description

Today I've updated to the latest 1.6.10 and see a lot of phpstan errors... Seems they were introduced in #1397 where the type of $args was changed to @param array<class-string<TMock>|TMock> $args. After this change, any of

$mock = \Mockery::mock('MyClass', [$constructorArg1, $constructorArg2]);
$mock = \Mockery::mock('MyClass', 'MyInterface', [$constructorArg1, $constructorArg2]);

will generate phpstan error

 463    Parameter #2 ...$args of static method Mockery::mock() expects class-string<Directive>|Directive, array<int, StreamFactory> given.
  716    Parameter #2 ...$args of static method Mockery::mock() expects class-string<Directive>|Directive, array<int, StreamFactory> given.
  1142   Parameter #2 ...$args of static method Mockery::mock() expects class-string<Directive>|Directive, array<int, StreamFactory> given.
  1236   Parameter #2 ...$args of static method Mockery::mock() expects class-string<Directive>|Directive, array<int, StreamFactory&Mockery\MockInterface> given.    
  1273   Parameter #2 ...$args of static method Mockery::mock() expects class-string<Directive>|Directive, array<int, StreamFactory&Mockery\MockInterface> given.    

Maybe something like will be better (but seems it is a breaking change and will break phpstan/phpstan-mockery)

/**
 * @template TMock
 *
 * @param array<class-string<TMock>|TMock> $class
 *
 * @return LegacyMockInterface&MockInterface&TMock
 */
public static function mock(mixed $class, mixed ...$args)

Also, the Mockery::spy(static function () {}) now gives (not sure related to phpstan/phpstan-mockery or not):

Return type of call to static method Mockery::spy() contains unresolvable type.

PS: Original issue (#1395) probably can be solved with phpstan/phpstan-mockery without changing docblock (at least I have used it for a while and never see any issue)

Steps to Reproduce

Update to 1.6.10 & run phpstan

Expected Behavior

No new phpstan errors

Actual Behavior

A lot of new phpstan errors

Exception or Error

No response

Additional Information

No response

@LastDragon-ru LastDragon-ru added the triage needs to be triaged label Mar 20, 2024
@LastDragon-ru

This comment was marked as outdated.

@LastDragon-ru

This comment was marked as outdated.

@LastDragon-ru
Copy link
Contributor Author

LastDragon-ru commented Mar 20, 2024

<?php declare(strict_types = 1);

$a = Mockery::mock(stdClass::class);
$b = Mockery::mock(stdClass::class, Countable::class);
$c = Mockery::mock(stdClass::class, ['a']);
$d = Mockery::mock(stdClass::class, Countable::class, ['a']);
$e = Mockery::mock();

PHPStan\dumpType($a);
PHPStan\dumpType($b);
PHPStan\dumpType($c);
PHPStan\dumpType($d);
PHPStan\dumpType($e);
Previous attemps

array{<class-string<TMock>|TMock>, ?mixed, ?mixed, ?mixed, ?mixed, ?mixed, ?mixed, ?mixed, ?mixed, ?mixed, ?mixed}

Without phpstan/phpstan-mockery:

 ------ ------------------------------------ 
  Line   MockeryTest.php                     
 ------ ------------------------------------
  9      Dumped type: Mockery\MockInterface
  10     Dumped type: Mockery\MockInterface
  11     Dumped type: Mockery\MockInterface
  12     Dumped type: Mockery\MockInterface
  13     Dumped type: Mockery\MockInterface
 ------ ------------------------------------

With

 ------ ------------------------------------------------------- 
  Line   MockeryTest.php                                        
 ------ -------------------------------------------------------
  9      Dumped type: Mockery\MockInterface&stdClass
  10     Dumped type: Countable&Mockery\MockInterface&stdClass
  11     Dumped type: Mockery\MockInterface&stdClass
  12     Dumped type: Countable&Mockery\MockInterface&stdClass
  13     Dumped type: Mockery\MockInterface
 ------ -------------------------------------------------------

Or even better array{<class-string|TMock>}&array<array-key, mixed>

Without phpstan/phpstan-mockery:

------ ------------------------------------ 
  Line   MockeryTest.php                     
 ------ ------------------------------------
  9      Dumped type: Mockery\MockInterface
  10     Dumped type: Mockery\MockInterface
  11     Dumped type: Mockery\MockInterface
  12     Dumped type: Mockery\MockInterface
  13     Dumped type: Mockery\MockInterface
 ------ ------------------------------------

With:

 ------ ------------------------------------------------------- 
  Line   MockeryTest.php                                        
 ------ -------------------------------------------------------
  9      Dumped type: Mockery\MockInterface&stdClass
  10     Dumped type: Countable&Mockery\MockInterface&stdClass
  11     Dumped type: Mockery\MockInterface&stdClass
  12     Dumped type: Countable&Mockery\MockInterface&stdClass
  13     Dumped type: Mockery\MockInterface
 ------ -------------------------------------------------------

@param array<class-string<TMock>|TMock> $args

Without phpstan/phpstan-mockery:

 ------ ----------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   MockeryTest.php                                                                                                                                
 ------ -----------------------------------------------------------------------------------------------------------------------------------------------
  5      Parameter #2 ...$args of static method Mockery::mock() expects class-string<stdClass>|stdClass, array<int, string> given.
  6      Parameter #3 ...$args of static method Mockery::mock() expects class-string<Countable|stdClass>|Countable|stdClass, array<int, string> given.
  7      Unable to resolve the template type TMock in call to method static method Mockery::mock()
         💡 See: https://phpstan.org/blog/solving-phpstan-error-unable-to-resolve-template-type
  9      Dumped type: Mockery\MockInterface&stdClass
  10     Dumped type: (Countable&Mockery\MockInterface)|(Mockery\MockInterface&stdClass)
  11     Dumped type: Mockery\MockInterface&stdClass
  12     Dumped type: (Countable&Mockery\MockInterface)|(Mockery\MockInterface&stdClass)
  13     Dumped type: Mockery\MockInterface
 ------ -----------------------------------------------------------------------------------------------------------------------------------------------

With

 ------ ----------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   MockeryTest.php                                                                                                                                
 ------ -----------------------------------------------------------------------------------------------------------------------------------------------
  5      Parameter #2 ...$args of static method Mockery::mock() expects class-string<stdClass>|stdClass, array<int, string> given.
  6      Parameter #3 ...$args of static method Mockery::mock() expects class-string<Countable|stdClass>|Countable|stdClass, array<int, string> given.
  7      Unable to resolve the template type TMock in call to method static method Mockery::mock()
         💡 See: https://phpstan.org/blog/solving-phpstan-error-unable-to-resolve-template-type
  9      Dumped type: Mockery\MockInterface&stdClass
  10     Dumped type: Countable&Mockery\MockInterface&stdClass
  11     Dumped type: Mockery\MockInterface&stdClass
  12     Dumped type: Countable&Mockery\MockInterface&stdClass
  13     Dumped type: Mockery\MockInterface
 ------ -----------------------------------------------------------------------------------------------------------------------------------------------

array<class-string<TMock>|TMock|array<mixed>>

Without phpstan/phpstan-mockery:

 ------ ------------------------------------------------------------------------------------------- 
  Line   MockeryTest.php                                                                            
 ------ -------------------------------------------------------------------------------------------
  7      Unable to resolve the template type TMock in call to method static method Mockery::mock()
         💡 See: https://phpstan.org/blog/solving-phpstan-error-unable-to-resolve-template-type
  9      Dumped type: Mockery\MockInterface&stdClass
  10     Dumped type: (Countable&Mockery\MockInterface)|(Mockery\MockInterface&stdClass)
  11     Dumped type: Mockery\MockInterface&stdClass
  12     Dumped type: (Countable&Mockery\MockInterface)|(Mockery\MockInterface&stdClass)
  13     Dumped type: Mockery\MockInterface
 ------ -------------------------------------------------------------------------------------------

With:

 ------ ------------------------------------------------------------------------------------------- 
  Line   MockeryTest.php                                                                            
 ------ -------------------------------------------------------------------------------------------
  7      Unable to resolve the template type TMock in call to method static method Mockery::mock()
         💡 See: https://phpstan.org/blog/solving-phpstan-error-unable-to-resolve-template-type
  9      Dumped type: Mockery\MockInterface&stdClass
  10     Dumped type: Countable&Mockery\MockInterface&stdClass
  11     Dumped type: Mockery\MockInterface&stdClass
  12     Dumped type: Countable&Mockery\MockInterface&stdClass
  13     Dumped type: Mockery\MockInterface
 ------ -------------------------------------------------------------------------------------------

@ghostwriter ghostwriter added Bug An error or unexpected behavior. and removed triage needs to be triaged labels Mar 20, 2024
@ghostwriter ghostwriter added this to the 1.6.11 milestone Mar 20, 2024
@martinssipenko
Copy link
Contributor

@ghostwriter any chance this could be released as 1.6.11?

@ghostwriter
Copy link
Member

ghostwriter commented Mar 21, 2024

@martinssipenko yes, 1.6.11 has been tagged.

@rolandsusans
Copy link
Contributor

rolandsusans commented Mar 22, 2024

@ghostwriter looks like the case of arg type Closure was not fixed in 1.6.11. Anyways, here is the PR, that addresses that - #1410.

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.

4 participants