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

Support constructor promotion with namespaced and union typehints #333

Merged
merged 8 commits into from
Nov 17, 2024

Conversation

sirbrillig
Copy link
Owner

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

@sirbrillig
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.

@sirbrillig
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
@@ -1535,62 +1535,105 @@ public static function getForLoopForIncrementVariable($stackPtr, $forLoops)
*/
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

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
Labels
None yet
Projects
None yet
2 participants