-
Notifications
You must be signed in to change notification settings - Fork 574
Fix phpstan/phpstan#5952: Allow throwing exceptions from __toString() not detected since level 4 #5405
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
Merged
VincentLanglet
merged 7 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-6rkqe4h
Apr 7, 2026
Merged
Fix phpstan/phpstan#5952: Allow throwing exceptions from __toString() not detected since level 4 #5405
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
01f336d
Fix phpstan/phpstan#5952: Track __toString() throws in echo statements
VincentLanglet 546a487
Simulate MethodCall instead of calling MethodThrowPointHelper directly
phpstan-bot a39518e
Remove redundant phpVersion check and add union type tests
phpstan-bot a85d75b
Generalize __toString() throw point tracking to all implicit call sites
phpstan-bot 879b943
Rename ToStringThrowPointHelper to ImplicitToStringCallHelper and ret…
phpstan-bot 281d039
Use import statement for ImplicitToStringCallHelper instead of fully …
phpstan-bot 0e0de1c
Collect impure points from implicit __toString() in echo handler and …
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/Analyser/ExprHandler/Helper/ImplicitToStringCallHelper.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Analyser\ExprHandler\Helper; | ||
|
|
||
| use PhpParser\Node\Expr; | ||
| use PhpParser\Node\Identifier; | ||
| use PHPStan\Analyser\ExpressionResult; | ||
| use PHPStan\Analyser\ImpurePoint; | ||
| use PHPStan\Analyser\MutatingScope; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Php\PhpVersion; | ||
| use function sprintf; | ||
|
|
||
| #[AutowiredService] | ||
| final class ImplicitToStringCallHelper | ||
| { | ||
|
|
||
| public function __construct( | ||
| private PhpVersion $phpVersion, | ||
| private MethodThrowPointHelper $methodThrowPointHelper, | ||
| ) | ||
| { | ||
| } | ||
|
|
||
| public function processImplicitToStringCall(Expr $expr, MutatingScope $scope): ExpressionResult | ||
| { | ||
| $throwPoints = []; | ||
| $impurePoints = []; | ||
|
|
||
| $exprType = $scope->getType($expr); | ||
| $toStringMethod = $scope->getMethodReflection($exprType, '__toString'); | ||
| if ($toStringMethod === null) { | ||
| return new ExpressionResult( | ||
| $scope, | ||
| hasYield: false, | ||
| isAlwaysTerminating: false, | ||
| throwPoints: [], | ||
| impurePoints: [], | ||
| ); | ||
| } | ||
|
|
||
| if (!$toStringMethod->hasSideEffects()->no()) { | ||
| $impurePoints[] = new ImpurePoint( | ||
| $scope, | ||
| $expr, | ||
| 'methodCall', | ||
| sprintf('call to method %s::%s()', $toStringMethod->getDeclaringClass()->getDisplayName(), $toStringMethod->getName()), | ||
| $toStringMethod->isPure()->no(), | ||
| ); | ||
| } | ||
|
|
||
| if ($this->phpVersion->throwsOnStringCast()) { | ||
| $throwPoint = $this->methodThrowPointHelper->getThrowPoint( | ||
| $toStringMethod, | ||
| $toStringMethod->getOnlyVariant(), | ||
| new Expr\MethodCall($expr, new Identifier('__toString')), | ||
| $scope, | ||
| ); | ||
| if ($throwPoint !== null) { | ||
| $throwPoints[] = $throwPoint; | ||
| } | ||
| } | ||
|
|
||
| return new ExpressionResult( | ||
| $scope, | ||
| hasYield: false, | ||
| isAlwaysTerminating: false, | ||
| throwPoints: $throwPoints, | ||
| impurePoints: $impurePoints, | ||
| ); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 PR is not only about throw points, but also impure points.
it seems the impurity portion does not have tests
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.
Both background tasks confirmed complete. Everything was already pushed — nothing more needed.