Skip to content

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
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-tdiqnzc
Open

phpstan-bot wants to merge 1 commit into
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-tdiqnzc

Conversation

@phpstan-bot

Copy link
Copy Markdown
Collaborator

Summary

A static call with an expression as the receiver ($class::run(false) with $class = Example::class, a class-string<Example>, or $example::run(false)) reported exceptions that the same direct call Example::run(false) rules out. This affected conditional @throws and DynamicStaticMethodThrowTypeExtensions. Dynamic instantiation new $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-less StaticCall(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 dynamic new $expr(...) branch, resolve the constructor reflection first. Walk the synthetic argument-less new X() only when the class has no constructor (or doesn't exist). Otherwise getConstructorThrowPoint() 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.php and signature-parity.php pass, the full test suite passes with the extension loaded, and the analysis output is identical with it on and off.
  • Analogous cases checked:
    • new $class(false): had the same bug, fixed.
    • Union class-strings (class-string<A>|class-string<B>): fixed by the same StaticCallHandler change.
    • Instance method calls $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, a class-string<Example> parameter, new $class(false) and new ExampleConstructor(false), plus "may throw" controls ($class::run(), new $class()) that should still yield bool.
  • 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 new ConstructorThrowOrNot class covering new self(false), new $class(false) and new $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

…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False checked exceptions on static calls through a known class-string

1 participant