Skip to content

Fix builtin class string instantiation#11860

Open
HenkPoley wants to merge 1 commit into
vimeo:6.xfrom
HenkPoley:fix-builtin-class-string-instantiation-6x
Open

Fix builtin class string instantiation#11860
HenkPoley wants to merge 1 commit into
vimeo:6.xfrom
HenkPoley:fix-builtin-class-string-instantiation-6x

Conversation

@HenkPoley

Copy link
Copy Markdown
Contributor

Fixes #11491

Replaces #11502 with a narrower fix.

Since 3.4.0 @ 99e6cd8 (2019-06-03) Psalm can crash when a string literal containing a built-in/runtime class name is used for dynamic instantiation:

$throwable_class = "\\AssertionError";
throw new $throwable_class("failed");

This PR keeps the fix at the point where the behavior is needed: dynamic instantiation analysis in NewAnalyzer.

Before validating the classlike for new $class_name, Psalm now ensures reflected storage exists when all of these are true:

  • the class name is already known to Psalm as an existing classlike,
  • storage is not already present,
  • PHP already has the class/interface/enum/trait loaded.

The runtime existence checks use *_exists(..., false), so this does not trigger autoloading while analyzing arbitrary values.

Tests

Added coverage in ClassLikeStringTest for instantiating \AssertionError through a string variable with allow_string_standin_for_class is enabled.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 26d8a6a. Configure here.

Comment thread src/Psalm/Internal/Analyzer/Statements/Expression/Call/NewAnalyzer.php Outdated
@HenkPoley HenkPoley force-pushed the fix-builtin-class-string-instantiation-6x branch from 26d8a6a to 557147c Compare May 23, 2026 02:00
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.

Fix for InvalidArgumentException due to dynamic class instantiation using a variable

1 participant