Skip to content

Normalize the leading backslash of constant class-string values - #6438

Open
mlocati wants to merge 1 commit into
phpstan:2.2.xfrom
mlocati-forks:class-string-leading-backslash
Open

mlocati wants to merge 1 commit into
phpstan:2.2.xfrom
mlocati-forks:class-string-leading-backslash

Conversation

@mlocati

@mlocati mlocati commented Sep 14, 2026

Copy link
Copy Markdown

PHP accepts class names with a leading backslash ('\Foo\Bar'), but the object types built from constant strings kept it: the resulting types were described as \Foo\Bar and didn't match the ones built from Foo\Bar::class.
Extensions looking the class name up elsewhere were affected too: for example phpstan-doctrine couldn't find the entity metadata of $entityManager->getRepository('\Foo\Bar'), so the magic findByXxx() methods of the repository were reported as undefined.

Strip the leading backslash when turning a constant string into an object type: in ConstantStringType (class-string object type, instanceof and is_a() checks, class constants), GenericClassStringType (accepts, isSuperTypeOf, template type inference, type removal) and ImpossibleCheckTypeHelper (method_exists()).

PHP accepts class names with a leading backslash ('\Foo\Bar'), but the object types built from constant strings kept it: the resulting types were described as \Foo\Bar and didn't match the ones built from Foo\Bar::class.
Extensions looking the class name up elsewhere were affected too: for example phpstan-doctrine couldn't find the entity metadata of $entityManager->getRepository('\Foo\Bar'), so the magic findByXxx() methods of the repository were reported as undefined.

Strip the leading backslash when turning a constant string into an object type: in ConstantStringType (class-string object type, instanceof and is_a() checks, class constants), GenericClassStringType (accepts, isSuperTypeOf, template type inference, type removal) and ImpossibleCheckTypeHelper (method_exists()).
@phpstan-bot

Copy link
Copy Markdown
Collaborator

You've opened the pull request against the latest branch 2.3.x. PHPStan 2.3 is not going to be released for months. If your code is relevant on 2.2.x and you want it to be released sooner, please rebase your pull request and change its target to 2.2.x.

@mlocati
mlocati changed the base branch from 2.3.x to 2.2.x September 14, 2026 09:51
@mlocati

mlocati commented Sep 14, 2026

Copy link
Copy Markdown
Author

change its target to 2.2.x.

Done.

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.

2 participants