Skip to content
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

ltrim(class-string, '\\') is always a class-string #7483

Closed
greg0ire opened this issue Jun 15, 2022 · 7 comments · Fixed by phpstan/phpstan-src#1597
Closed

ltrim(class-string, '\\') is always a class-string #7483

greg0ire opened this issue Jun 15, 2022 · 7 comments · Fixed by phpstan/phpstan-src#1597

Comments

@greg0ire
Copy link

greg0ire commented Jun 15, 2022

Feature request

If you search for ltrim in all Doctrine packages, you'll find many calls to ltrim for normalization purposes, in particular normalizing a class-string prefixed with a backslash into one without. I think the rule expressed in the title is always true, but PHPStan does not seem to have that built-in (I must admit it's quite specific): https://phpstan.org/r/be10b00c-b052-4f86-805b-cc783dd21cd4

Did PHPStan help you today? Did it make you happy in any way?

I'm working on improving phpdoc on doctrine/orm before using Rector to migrate that to PHP8 native type declarations, and it's very reassuring to have PHPStan check my work 🙂

@staabm
Copy link
Contributor

staabm commented Jul 7, 2022

In psalm this one was fixed with a conditional return type: vimeo/psalm#8219

@mad-briller
Copy link
Contributor

looked into adding a stub for this with conditional return types, but i can't get phpdoc-parser to recognise \\ in a literal string:
https://phpstan.org/r/41f1530f-eaee-4e7d-8bfc-e7ccc7cfc7a7

using any other sting there makes it work just fine:
https://phpstan.org/r/a884b37d-682b-45a4-b891-1725ffd1c428

am i writing or escaping the literal wrong?

@ondrejmirtes
Copy link
Member

Just write a return type extension 😊

@staabm
Copy link
Contributor

staabm commented Jul 31, 2022

looked into adding a stub for this with conditional return types, but i can't get phpdoc-parser to recognise \ in a literal string:

IMO a separate issue should be opened for that bug, so someone might fix it.

Still we can implement the feature request using a extension like ondrey suggested.

@staabm
Copy link
Contributor

staabm commented Aug 6, 2022

@greg0ire to make use of this change you need to declare your parameters as a class-string, and then the type will be preserved when passed thru ltrim($class, , '\\');

@greg0ire
Copy link
Author

greg0ire commented Aug 6, 2022

Awesome! Thanks! Cc @derrabus

@github-actions
Copy link

github-actions bot commented Sep 7, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants