-
Notifications
You must be signed in to change notification settings - Fork 53
Fix pending PHPStan errors in PR #233 #234
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
lets ignore the cs errors here, they will be fixed in #233 |
i tagged https://github.com/php-http/promise/releases/tag/1.2.1 and restarted the phpstan build. there are still some failures 🤔 |
Taking a look. |
@dbu looking into the failures related to the PHPStan seems to have some limitations in recognizing that a
Please see the link below. |
thanks a lot for looking into this! that would be this interface, right? https://github.com/php-fig/http-client/blob/master/src/ClientExceptionInterface.php says it extends \Throwable. is it possible to narrow down to a more specific interface in this context? if even we can't make it work correctly, we should revert the annotations on the promise. otherwise everybody has to ignore the errors and everybody will complain first. have you been able to check with the phpstan authors if your example should work? they are really good at improving phpstan, if it makes sense they can maybe fix it soonish. |
I reasoned a bit on the linked playground, and I think that PHPStan is right (btw, the same error is reported by Psalm). What's happenning is that we're declaring in the interface that |
Thank you for breaking this down @ste93cry! I get it now. |
Alternatively, reverting the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed https://github.com/php-http/promise/blob/7fa2284a91241e7fa06976369e998933d510da1b/src/Promise.php#L42C1-L42C88 is a BC break (in terms of phpdoc). we can't narrow down what type of exception the callable must accept. i think the correct thing would be to have a second covariant for the exception of the promise. then we can correctly model that the promise can only ever throw ClientExceptionInterface
can you try that?
and we should adjust the HttpAsyncClient in the httplug repository to declare what promise it returns, then we don't need to change all the various async client implementations here.
@@ -122,6 +130,9 @@ public function reject(ClientExceptionInterface $exception): void | |||
} | |||
} | |||
|
|||
/** | |||
* {@inheritDoc} | |||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't do inheritdoc when there is nothing else. if there is no docblock, most tools take the docblock of the parent/interface
@@ -90,6 +97,7 @@ public function getState(): string | |||
|
|||
/** | |||
* Resolve this deferred with a Response. | |||
* @param ResponseInterface $response |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't add phpdoc when it does not add information
* @param ResponseInterface $response |
@@ -51,6 +53,11 @@ public function __construct(callable $waitCallback) | |||
$this->onRejectedCallbacks = []; | |||
} | |||
|
|||
/** | |||
* @param callable(ResponseInterface): ResponseInterface|null $onFulfilled |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should not be necessary with the @implements
in the class docblock, it should come automatically from the covariant in the interface.
@@ -23,6 +23,7 @@ abstract public function sendRequest(RequestInterface $request): ResponseInterfa | |||
|
|||
/** | |||
* @see HttpAsyncClient::sendAsyncRequest | |||
* @return \Http\Promise\Promise<ResponseInterface|\Throwable> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add in interface instead
@@ -52,6 +53,9 @@ public function addHttpClient($client): void | |||
*/ | |||
abstract protected function chooseHttpClient(): HttpClientPoolItem; | |||
|
|||
/** | |||
* @return Promise<ResponseInterface> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed when HttpAsyncClient defines the return type
@@ -89,6 +90,9 @@ public function sendRequest(RequestInterface $request): ResponseInterface | |||
return $response; | |||
} | |||
|
|||
/** | |||
* @return Promise<ResponseInterface> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed when HttpAsyncClient defines the return type
@@ -26,6 +27,9 @@ public function sendRequest(RequestInterface $request): ResponseInterface | |||
return $this->chooseHttpClient($request)->sendRequest($request); | |||
} | |||
|
|||
/** | |||
* @return Promise<ResponseInterface> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed when HttpAsyncClient defines the return type
@@ -84,6 +84,9 @@ public function sendRequest(RequestInterface $request): ResponseInterface | |||
return $pluginChain($request)->wait(); | |||
} | |||
|
|||
/** | |||
* @return Promise<ResponseInterface> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed when HttpAsyncClient defines the return type
@@ -21,6 +23,7 @@ trait HttpAsyncClientDecorator | |||
|
|||
/** | |||
* @see HttpAsyncClient::sendAsyncRequest | |||
* @return Promise<ResponseInterface> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to fix this in the HttpAsyncClient instead. then it will be inferred for all the various implementations here:
Well, technically speaking it is a breaking change. You can narrow the return type in a child class because the declaration of the method is compatible, but you cannot narrow the type of an argument in a child class, because that would mean that the implementation cannot handle something that the parent declares it can. For this reason, since adding the typehint would mean that people have to change their child classes to take it into account, the same is true when adding the typehint in the DocBlock and then using a static analysis tool.
Couldn't the plugin also throw a |
there is no ServerExceptionInterface in PSR-18, the client exception is short to mean "http client exception" and psr18 says that every exception thrown must be a client exception. the async client follows the same logic in https://github.com/php-http/httplug/blob/2.x/src/HttpAsyncClient.php (except that for BC it has https://github.com/php-http/httplug/blob/2.x/src/Exception.php) imo the correct solution is to make the exception customizable in the promise as well and in async client customize the promise to use |
I wonder whether it makes sense to insist on trying to typehint |
i think for documentation purposes its good to be clear what kind of exception may be thrown / has to be handled. |
I came up with this solution that seems to work fine 🎉Of course, the |
I played around in the phpstan playground and came up with this: https://phpstan.org/r/6ec99fad-fd67-4f64-ac0d-ba135e207700 The upside is that the async client can declare what type of exception the onRejected callback must expect, to match with the current text description in phpdoc and the implementations of adapters. any thoughts on that? i plan to implement this soonish if no objections come up. |
ef61211
to
ee440ee
Compare
What's in this PR?
To Do