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

array_filter callback throwing exception not reflected by phpstan, giving error about useless throws annotation #5001

Closed
janedbal opened this issue May 13, 2021 · 11 comments
Labels
Milestone

Comments

@janedbal
Copy link
Contributor

Bug report

Following snippet gives following error when using new tooWideThrowType check

Method HelloWorld::filter() has Exception in PHPDoc @throws tag but it's not thrown.

Code snippet that reproduces the problem

https://phpstan.org/r/df0c05f1-1f50-4361-bca3-e261fd5db47e

Expected output

No error

@VincentLanglet
Copy link
Contributor

I have another example: #5043

You flag this as easy fixes @ondrejmirtes, any recommendation about how to fix it ? :)

@ondrejmirtes
Copy link
Member

This needs a dynamic throw type extension for those functions that execute callbacks immediately. The extension needs to look like this: #4819 (comment)

@VincentLanglet
Copy link
Contributor

This needs a dynamic throw type extension for those functions that execute callbacks immediately.

With something like this https://github.com/phpstan/phpstan-src/blob/master/src/Type/Php/JsonThrowTypeExtension.php#L22-L25, I assume ?

So we have to list all the php function which take a callback and use it immediately, there is a lot I think.

@ondrejmirtes
Copy link
Member

Yeah, configurable like that.

I think we can only cover the most common ones. Throwing an exception from callbacks like that isn't a good reason anyway and you should be using foreach, that's why I think it's an edgecase.

@phpstan-bot
Copy link
Contributor

@janedbal After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-No errors
+-1: Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40
+Run PHPStan with --debug option and post the stack trace to:
+https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
Full report
Line Error
-1 Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40Run PHPStan with --debug option and post the stack trace to:https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md

@phpstan-bot
Copy link
Contributor

@janedbal After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-No errors
+10: Method HelloWorld::filter() should return array<string> but return statement is missing.
Full report
Line Error
10 Method HelloWorld::filter() should return array<string> but return statement is missing.

@etienneroudeix
Copy link

This change is very tricky, every closure is now a black hole ending the exception chain without any check.

@janedbal
Copy link
Contributor Author

janedbal commented Oct 9, 2023

This change is very tricky, every closure is now a black hole ending the exception chain without any check.

Whole this issue is solved in forbidCheckedExceptionInCallable which even allows you setup more methods/functions that are immediatelly called (like EntityManager::transactional)

@etienneroudeix
Copy link

This change is very tricky, every closure is now a black hole ending the exception chain without any check.

Whole this issue is solved in forbidCheckedExceptionInCallable which even allows you setup more methods/functions that are immediatelly called (like EntityManager::transactional)

Thank you for that.

Copy link

github-actions bot commented Feb 6, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 6, 2024
@ondrejmirtes
Copy link
Member

Addressed by phpstan/phpstan-src@74c02de.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants