-
Notifications
You must be signed in to change notification settings - Fork 560
Fix phpstan/phpstan#9349: Throw point are not properly recognized #5166
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug9349; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public function test(): void | ||
| { | ||
| global $pdo; | ||
|
|
||
| try { | ||
| $this->maybeThrows(); | ||
| $sql = "SELECT * FROM foo"; | ||
| $rs = $pdo->query($sql); | ||
| if ($result = $rs->fetch(\PDO::FETCH_ASSOC)) { | ||
| // do something | ||
| } | ||
| } catch (\PDOException $e) { | ||
| var_dump($sql); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @throws \RuntimeException | ||
| */ | ||
| public function maybeThrows(): void | ||
| { | ||
| if (random_int(0, 1) === 1) { | ||
| throw new \RuntimeException(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class HelloWorld2 | ||
| { | ||
| public function test2(): void | ||
| { | ||
| global $pdo; | ||
|
|
||
| try { | ||
| $this->maybeThrows2(); | ||
| $sql = "SELECT * FROM foo"; | ||
| $rs = $pdo->query($sql); | ||
| if ($result = $rs->fetch(\PDO::FETCH_ASSOC)) { | ||
| // do something | ||
| } | ||
| } catch (\PDOException $e) { | ||
| var_dump($sql); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @throws \LogicException | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be good to have a 3rd test-case in which the
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already retrieved the results - all 11,578 tests passed. Nothing more to do. |
||
| */ | ||
| public function maybeThrows2(): void | ||
| { | ||
| if (random_int(0, 1) === 1) { | ||
| throw new \LogicException(); | ||
| } | ||
| } | ||
|
Comment on lines
+37
to
+61
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would separate this second test-case into a second class, e.g.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, why ? Different method should be enough no ?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. VincentLanglet's current review comment is a discussion question directed at staabm ("Out of curiosity, why? Different method should be enough no?"), not a code change request. VincentLanglet is agreeing that the current structure (separate methods in the same class) is fine and questioning staabm's suggestion to create a separate There are no actionable code changes requested in this review. The two earlier review points from staabm were:
No code changes are needed for VincentLanglet's comment. Would you like me to respond to the PR review comment on GitHub instead, or would you like me to address staabm's suggestions?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already handled — all tests passed and the commit has been pushed.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
just because its easier to see that method1,2 do not have an effect on method 3,4 - and vice versa
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The working tree is clean and up to date with the remote. All of staabm's review comments have already been addressed in previous commits:
No further changes are needed. The branch is already pushed to the remote. |
||
| } | ||
|
|
||
| class HelloWorld3 | ||
| { | ||
| public function test3(): void | ||
| { | ||
| global $pdo; | ||
|
|
||
| try { | ||
| $this->maybeThrows3(); | ||
| $sql = "SELECT * FROM foo"; | ||
| $rs = $pdo->query($sql); | ||
| if ($result = $rs->fetch(\PDO::FETCH_ASSOC)) { | ||
| // do something | ||
| } | ||
| } catch (\PDOException $e) { | ||
| var_dump($sql); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @throws \LogicException|\RuntimeException | ||
| */ | ||
| public function maybeThrows3(): void | ||
| { | ||
| if (random_int(0, 1) === 1) { | ||
| throw new \RuntimeException(); | ||
| } | ||
| if (random_int(0, 1) === 1) { | ||
| throw new \LogicException(); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
I think that PHPStan should know that
$sqlwill always exist at this point.atm the test asserts
Uh oh!
There was an error while loading. Please reload this page.
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.
ohh I now see that
PDOException extends RuntimeException- its fine than.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.
No code changes are needed here. The reviewer (staabm) is retracting their earlier concern about
$sqlalways being defined at line 19. They now understand that sincePDOException extends RuntimeException, the@throws \RuntimeExceptionannotation onmaybeThrows()means aPDOExceptioncould come from that call (before$sqlis assigned), so "Variable $sql might not be defined" is the correct behavior.All review comments on this PR are resolved — no actionable changes remain.
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.
All background tasks are now complete. The final
make testsrun passed (11,578 tests, 0 failures, 92 skipped) and bothmake phpstanruns passed with no errors. The branch is clean and up to date with the remote.