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
Recognise exceptions as instances of Throwable #91
Comments
This normally works, because every concrete exception class passes
isSubclassOf('Throwable') in reflection, but I suspect that your case is
about interfaces - which can be put in catch too and which of course do not
extend Throwable.
Please check if I'm right and that your FooException class is an interface.
…On Wed, 18 Jan 2017 at 19:26, Haralan Dobrev ***@***.***> wrote:
With --level 5, I get errors such as this one:
Parameter #1 $throwable of static method Foo::processThrowable() expects Throwable, FooException given.
Of course in PHP 7, every exception is an instance of the special
Throwable interface.
It would be nice if PHPStan knows about it out of the box.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#91>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAGZuBLMJxNdTBHXS_8qeME0CojUAZCRks5rTllDgaJpZM4LnOk1>
.
|
This is what I suspect your situation is: This is what you should do :) |
Indeed this is the case, the exception I'm getting is an interface. However, it is not from my code, but from a 3rd-party library - https://github.com/thephpleague/omnipay-common/blob/b1440bd7c/src/Omnipay/Common/Exception/OmnipayException.php So I could write a new interface extending both theirs and |
I'm gonna fix that for the next version - ClassReflection in catch block
will always agree that the exception variable is a Throwable. Meanwhile uou
can add the message to ignoreErrrors.
…On Wed, 18 Jan 2017 at 20:43, Haralan Dobrev ***@***.***> wrote:
Indeed this is the case, the exception I'm getting is an interface.
However, it is not from my code, but from a 3rd-party library -
https://github.com/thephpleague/omnipay-common/blob/b1440bd7c/src/Omnipay/Common/Exception/OmnipayException.php
So I could write a new interface extending both theirs and Throwable, and
then using that in my code, but should I really *have to* do that for
every 3rd-party exception interface?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#91 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAGZuNzyAmO1lHkJGzFcQjw4q-mrx-Xbks5rTmtcgaJpZM4LnOk1>
.
|
I get the motivation to automatically add the Throwable type, but I think, this is a double edged sword, because that also means, that if something is in fact not an Throwable/Exception it may very well be a bug and the worst thing about this is that PHP will not throw an error about this. On the other hand in throws, this is checked by PHP: https://3v4l.org/OBHAW. This is exactly the kind of bugs I would expect PHPStan to notify my about - the <?php
class Foo
{
}
class FooException extends \Exception
{
}
try {
throw new \FooException();
} catch (\FooExceptio $e) {
throw new \Exception('Never called because class does not exist');
} catch (\Foo $e) {
throw new \Exception('Never called beacause class does not match exception and never will (because it is not an exception/throwable subtype)');
} So either the class can:
So I think it is still usable to check it, also level 5 should be very strict as it is, so at first I wanted to propose pushing this to a higher level, but I think it might actually be fine. So I believe this is on par with the other cases, where the type information is missing and can either be supplied in other way, or ignored. |
@VasekPurchart The class referenced inside the This issue is about what happens inside the To prevent the issue you're talking about, I'd have to check if some class that implements the caught interface actually extends |
@ondrejmirtes Thank you for your efforts! I just came here to say that I'm using a method call on the exception where a Throwable is expected, but you've posted first: class Logger
{
/**
* @param Throwable $throwable
*/
public function logException($throwable) // I don't have a typehint here so it works on 5.6 as well
{
// log exception with trace here
}
}
try {
// some code here
} catch(OmnipayException $exception) {
Logger::logException($exception);
} I hope this context helps you and others reading the issue. |
But one check is truly missing – whether the type in |
Awesome to have an issue, come to GH to open it and find it's already there, fully explained and everything. 👍 |
Any update on this @ondrejmirtes ? |
Right now, you can put this error you’re getting into ignoreErrors.
On Wed, 9 Aug 2017 at 11:57, Ema Panz ***@***.***> wrote:
Any update on this @ondrejmirtes <https://github.com/ondrejmirtes> ?
It would fix some warnigs we're getting from a GuzzleException that we're
catching: it is an interface (
https://github.com/guzzle/guzzle/blob/master/src/Exception/GuzzleException.php)
and a further use of $e->getMessage() is not recognized by PhpStan
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#91 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAGZuDBITo5Xk8RXFpD6opHw_zUzpLFDks5sWYKFgaJpZM4LnOk1>
.
--
Ondřej Mirtes
|
Fixed mainly with intersection types #410 and this 480e9c8. If you like PHPStan and use it to find bugs in your projects, I'd love if you supported it on Patreon. I have many ideas how to make PHPStan even smarter and faster but doing it over evenings and weekends is not sustainable longterm. I'm trying to make developing open-source a viable livelihood alternative. Even a $1 a month helps, but the more you contribute, the closer I am to my goal to work on it fulltime :) |
Thanks @ondrejmirtes! |
With
--level 5
, I get errors such as this one:Of course in PHP 7, every exception is an instance of the special
Throwable
interface.It would be nice if PHPStan knows about it out of the box.
The text was updated successfully, but these errors were encountered: