Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support constructor promotion with namespaced and union typehints #333

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

Merged
sirbrillig merged 8 commits into 2.x from fix-constructor-promotion-with-namespace
Nov 17, 2024

Conversation

@sirbrillig
Copy link
Owner

@sirbrillig sirbrillig commented Nov 17, 2024

Constructor property promotion supports full typehinting, but while this sniff handled a simple typehint like string or ?string, it could not handle complex typehints like false|null|\App\Models\User.

In this PR we update the handling to be able to better recognize a typehint.

Fixes #331

Copy link
Owner Author

Ah, it's just not handling union types in PHP < 8. I need to figure out what phpcs under PHP 5 and 7 see when they look at a union type.

Copy link
Owner Author

That works. psalm in the automated tests is failing because it's having trouble parsing the XML config file but I think it's unrelated to this PR (psalm passes locally).

Fatal error: Uncaught Psalm\Exception\ConfigException: Error parsing file /home/runner/work/phpcs-variable-analysis/phpcs-variable-analysis/ on line 9: Element '{https://getpsalm.org/schema/config}psalm': No matching global declaration available for the validation root.

@sirbrillig sirbrillig merged commit 779884c into 2.x Nov 17, 2024
54 of 56 checks passed
@sirbrillig sirbrillig deleted the fix-constructor-promotion-with-namespace branch November 17, 2024 19:42
*
* @return bool
*/
public static function isConstructorPromotion(File $phpcsFile, $stackPtr)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@sirbrillig Just out of curiousity: why are you not using the File::getMethodParameters() to parse the function signature ? That would allow for just checking if the parameter has a 'property_visibility' index to know whether it is a promoted property...

Copy link
Owner Author

@sirbrillig sirbrillig Nov 18, 2024

Choose a reason for hiding this comment

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

Because I didn't know about File::getMethodParameters() 😅 That does sound much easier! TIL there's helpers like this inside the source code.

Related: I really need to get around to adding phpcsutils to this package; you've done a fantastic job with its documentation!

Copy link
Collaborator

Choose a reason for hiding this comment

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

If you didn't know about this one... you might also be interested in the File::getMemberProperties() (for declared, non constructor promoted, properties in a class)...

And yes, PHPCSUtils has those too and improved on them further + much more.

Happy to talk things through with you at some point if you think that would help. Unfortunately won't have any time in the foreseeable future to actually help you with the code changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jrfnl jrfnl jrfnl left review comments

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Constructor property promotion and backslash as a namespace operator cause false positive for UndefinedVariable bug

AltStyle によって変換されたページ (->オリジナル) /