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

Add ThrowableReturnTypeExtension #795

Merged
merged 2 commits into from
Nov 20, 2021
Merged

Add ThrowableReturnTypeExtension #795

merged 2 commits into from
Nov 20, 2021

Conversation

herndlm
Copy link
Contributor

@herndlm herndlm commented Nov 20, 2021

Closes phpstan/phpstan#6001

Based on feedback of the first approach in #767 (comment)

@herndlm herndlm marked this pull request as draft November 20, 2021 21:17
@herndlm
Copy link
Contributor Author

herndlm commented Nov 20, 2021

I'm still unsure when it comes to tests in phpstan - is this assert test enough?

@herndlm herndlm marked this pull request as ready for review November 20, 2021 21:46
@ondrejmirtes ondrejmirtes merged commit 89c16a6 into phpstan:master Nov 20, 2021
@ondrejmirtes
Copy link
Member

Yes, thank you!

@herndlm herndlm deleted the feature/add-throwable-return-type-extension branch November 20, 2021 22:18
MidnightDesign pushed a commit to MidnightDesign/phpstan-src that referenced this pull request Nov 30, 2021
Seldaek added a commit to composer/composer that referenced this pull request Dec 30, 2021
@Seldaek
Copy link
Contributor

Seldaek commented Dec 30, 2021

Are you sure this is the best way to handle this? And if it is only PDOException could we not return string only for that class, and leave getCode as an int for other classes?

I am asking because as per https://phpstan.org/r/610c7193-7cba-46eb-be15-b9f09c91728f it is a bit weird IMO that a clear int case is seen as string|int, which forced me to do composer/composer@84f8fda to fix these errors https://github.com/composer/composer/actions/runs/1638602292

@ondrejmirtes
Copy link
Member

@Seldaek We've had Exception::getCode() and Throwable::getCode() as only int in the past but it was wrong in many cases.

We haven't tried Exception::getCode()and Throwable::getCode() to be int|string, PDOException::getCode() as string and any other subclass to be int but I think it's certain there's going to be userland exceptions that use string as well...

@Seldaek
Copy link
Contributor

Seldaek commented Jan 2, 2022

I tried redefining getCode to add an int return type but it's final so I couldn't, so I'm not sure what those userland exceptions would be? Exceptions in extensions perhaps..

@ondrejmirtes
Copy link
Member

@Seldaek You can do this crazy thing: https://3v4l.org/vcWJg

@Seldaek
Copy link
Contributor

Seldaek commented Jan 6, 2022

OK, although I would argue that is totally voiding the warranty and shouldn't mean PHPStan covers your ass with a mixed return type. I suspect many things can be broken in similar ways or with reflection or who knows what..

@Seldaek
Copy link
Contributor

Seldaek commented Jan 6, 2022

https://www.php.net/manual/en/exception.getcode.php clearly documents it as returning an int, so IMO an exception for PDO as it's insane but part of core makes sense, but everything else should be int, and those people doing insane shit can ignore their errors instead of forcing everyone else to do so.

ondrejmirtes added a commit that referenced this pull request Jan 6, 2022
@ondrejmirtes
Copy link
Member

Alright, I'm eager to try my suggestion from #795 (comment) too: 1181717

Let's see what the integration tests and the feedback will say :)

@ondrejmirtes
Copy link
Member

@Seldaek
Copy link
Contributor

Seldaek commented Jan 7, 2022

Thanks, looks good to me now, and sorry for the rant :D

Seldaek added a commit to composer/composer that referenced this pull request Jan 7, 2022
@Seldaek
Copy link
Contributor

Seldaek commented Jan 7, 2022

composer/composer@508ac05 🎉

@Seldaek
Copy link
Contributor

Seldaek commented Jan 7, 2022

Ahh well now I'm left with https://github.com/composer/composer/runs/4736711738?check_suite_focus=true which makes sense of course.. There's no way to tell PHPStan to narrow down the return type for getCode is there? Redefining with @method did not seem to help.

@ondrejmirtes
Copy link
Member

Yeah, this might be actually solved for good, until someone notices that some exception from a popular framework like Symfony or Doctrine also misuses this :D

The fact that @method doesn't work here surprised me, I'm gonna look into it.

@Seldaek
Copy link
Contributor

Seldaek commented Jan 7, 2022

At least adding this to SolverProblemsException's class docblock did not help, maybe I did it wrong tho:

 * @method self::ERROR_DEPENDENCY_RESOLUTION_FAILED getCode()

@Seldaek
Copy link
Contributor

Seldaek commented Jan 7, 2022

Maybe its effect is negated by the ThrowableReturnTypeExtension?

@ondrejmirtes
Copy link
Member

Good guess, that was it: e04cc8d

@Seldaek
Copy link
Contributor

Seldaek commented Jan 7, 2022

Nice :) Thanks a bunch

@ondrejmirtes
Copy link
Member

Enjoy 1.3.3 :) https://github.com/phpstan/phpstan/releases/tag/1.3.3

@Seldaek
Copy link
Contributor

Seldaek commented Jan 7, 2022

It builds 👍🏻 composer/composer@64d39a9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants