Skip to content

More specific plugin type doc #199

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

Merged
merged 3 commits into from
Jul 7, 2020
Merged

More specific plugin type doc #199

merged 3 commits into from
Jul 7, 2020

Conversation

GrahamCampbell
Copy link
Contributor

@GrahamCampbell GrahamCampbell commented Jul 6, 2020

Q A
Bug fix? yes
New feature? yes
BC breaks? no
Deprecations? no
License MIT

Static analysers (such as Psalm) complain if someone adds this typing information to their own class which extends this interface because what they have affectively done is specialised the types, which is not allowed. It's the same as an interface saying string|int but the implementation only accepting string. So, with this in mind, we should take the opportunity to provide the most specific type information we can.

For reference, an error produced by Psalm (without this PR):

ERROR: MoreSpecificImplementedParamType - src/HttpClient/Plugin/Authentication.php:62:71 - Argument 2 of Bitbucket\HttpClient\Plugin\Authentication::handleRequest has the more specific type 'callable(Psr\Http\Message\RequestInterface):Http\Promise\Promise', expecting 'callable' as defined by Http\Client\Common\Plugin::handleRequest (see https://psalm.dev/140)
    public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise


ERROR: MoreSpecificImplementedParamType - src/HttpClient/Plugin/Authentication.php:62:87 - Argument 3 of Bitbucket\HttpClient\Plugin\Authentication::handleRequest has the more specific type 'callable(Psr\Http\Message\RequestInterface):Http\Promise\Promise', expecting 'callable' as defined by Http\Client\Common\Plugin::handleRequest (see https://psalm.dev/140)
    public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise

and by PHPStan:

 ------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   HttpClient/Plugin/Authentication.php                                                                                                                                  
 ------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  62     Parameter #2 $next (callable(Psr\Http\Message\RequestInterface): Http\Promise\Promise) of method Bitbucket\HttpClient\Plugin\Authentication::handleRequest() should   
         be contravariant with parameter $next (callable(): mixed) of method Http\Client\Common\Plugin::handleRequest()                                                        
  62     Parameter #3 $first (callable(Psr\Http\Message\RequestInterface): Http\Promise\Promise) of method Bitbucket\HttpClient\Plugin\Authentication::handleRequest() should  
         be contravariant with parameter $first (callable(): mixed) of method Http\Client\Common\Plugin::handleRequest()                                                       
 ------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

@GrahamCampbell
Copy link
Contributor Author

GrahamCampbell commented Jul 6, 2020

NB This syntax for callable typing is standard, and is agreed between both PHPStan and Psalm (and others), and understood by PHPStorm. In the absence of any universality accepted specification of phpdoc syntax, I think this is the best I can say.

@GrahamCampbell GrahamCampbell marked this pull request as draft July 6, 2020 20:45
@GrahamCampbell GrahamCampbell marked this pull request as ready for review July 6, 2020 21:05
Copy link
Contributor

@dbu dbu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. if phpstan and psalm agree, thats a good start :-)

@dbu dbu merged commit 75470e5 into php-http:master Jul 7, 2020
@GrahamCampbell GrahamCampbell deleted the plugin-doc branch July 7, 2020 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants