Repository navigation
Skip the synthetic argument-less call's throw points for dynamic $class::method() and new $class() when the callee resolves - #7186
Open
phpstan-bot wants to merge 1 commit into
Conversation
…ass::method()` and `new $class()` when the callee resolves - StaticCallHandler: for an expression receiver (`$class::run(false)`, `$obj::run(false)`), a synthetic `Name::run()` call with no arguments was walked and its throw points were added, on top of the throw point computed from the real arguments. With defaults filled in, a conditional `@throws` or a DynamicStaticMethodThrowTypeExtension reported exceptions the real call rules out. The synthetic call is now walked only when no method reflection was resolved. - NewHandler: same problem with `new $class(false)`: a synthetic `new X()` without arguments added constructor throw points. The constructor reflection is now resolved first, and the synthetic instantiation is only walked when there is no constructor. - Ported both changes to the turbo-ext mirrors (StaticCallHandler.cpp, NewHandler.cpp). - Probed instance method calls (`$obj->m(false)`), which do not use a synthetic call and were already correct. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
A static call with an expression as the receiver (
$class::run(false)with$class = Example::class, aclass-string<Example>, or$example::run(false)) reported exceptions that the same direct callExample::run(false)rules out. This affected conditional@throwsandDynamicStaticMethodThrowTypeExtensions. Dynamic instantiationnew $class(false)had the same bug. Now the throw points for these calls come only from the actual arguments when the method or constructor resolves.Changes
src/Analyser/ExprHandler/StaticCallHandler.php: walk the synthetic argument-lessStaticCall(new Name($class), $name, [])for its throw points only when$methodReflection === null. When the method resolves,MethodThrowPointHelper::getThrowPoint()already computes the throw point from the real, normalized arguments.src/Analyser/ExprHandler/NewHandler.php: in the dynamicnew $expr(...)branch, resolve the constructor reflection first. Walk the synthetic argument-lessnew X()only when the class has no constructor (or doesn't exist). OtherwisegetConstructorThrowPoint()covers it using the real arguments.turbo-ext/src/StaticCallHandler.cpp,turbo-ext/src/NewHandler.cpp: same changes in the native mirrors. The extension builds,smoke.php,side-by-side.phpandsignature-parity.phppass, the full test suite passes with the extension loaded, and the analysis output is identical with it on and off.new $class(false): had the same bug, fixed.class-string<A>|class-string<B>): fixed by the same StaticCallHandler change.$obj->m(false): no synthetic call is involved, so they were already correct.Root cause
For an expression receiver, both handlers walked a synthetic call to the resolved class name without arguments and merged its throw points. Without arguments, the parameter defaults apply (
$fail = true), so the conditional throw type or the dynamic throw type extension returned the exception. That throw point was merged with the correct one from the real arguments, so the exception showed up as possible. The synthetic walk is only useful when the callee can't be resolved, so it is now limited to that case.Test
tests/PHPStan/Analyser/nsrt/bug-15837.php: the reproducer from the issue. It covers the class-string variable, the object receiver, aclass-string<Example>parameter,new $class(false)andnew ExampleConstructor(false), plus "may throw" controls ($class::run(),new $class()) that should still yieldbool.tests/PHPStan/Analyser/data/dynamic-method-throw-type-extension.php:$class::staticThrowOrNot(false|true)and$this::staticThrowOrNot(false)through the dynamic static method throw type extension, and a newConstructorThrowOrNotclass coveringnew self(false),new $class(false)andnew $class(true)through the extension.All new assertions about the bug fail without the fix and pass with it.
Fixes phpstan/phpstan#15837
🤖 Generated with Claude Code