-
Notifications
You must be signed in to change notification settings - Fork 79
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
Generate hook names using parsed Node instead of pretty-printed name. #242
base: master
Are you sure you want to change the base?
Conversation
b8c5063
to
e860d73
Compare
lib/class-hook-reflector.php
Outdated
* | ||
* @var \PhpParser\Node | ||
*/ | ||
$node = $node ?? $this->node->args[0]->value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 23 in 7fc2227
"php" : ">=5.4", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jrfnl can you provide some context or clarification for your comment?
are you suggesting we update the minimum supported version in this parser to what WordPress supports, to 7.4?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not suggesting anything. I'm only pointing out that as things are at the moment, this change cannot go in.
Either the minimum PHP version needs to change or the syntax/functionality used needs to be made compatible with the minimum supported PHP version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated the minimum version required in d57e2ed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know where you are getting your information from, but WP Core still has a minimum supported PHP version of PHP 7.0....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a side-note: I believe that whether or not it is acceptable to change the minimum PHP version of this package should be a separate discussion and should not be mixed in with a proposal for a (functional) change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know where you are getting your information from
that's why I left the link in the original message 😄
but thanks for correcting me: I was reviewing the recommended setup, not the minimum.
As a side-note: I believe that whether or it is acceptable to change the minimum PHP version of this package should be a separate discussion and should not be mixed in with a proposal for a (functional) change.
sounds like a good suggestion, which is why I asked if you meant something when you called out the line. I've updated this to avoid the ??
in 80ded80. thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code introduces a bug for do_action( 'woocommerce_admin_field_' . $value['type'], $value );
which will be returned as woocommerce_admin_field_$value['type']
instead of woocommerce_admin_field_{$value['type']}
as it did previously, but that can be easily fixed by just adding a condition for array.
I would suggest to keep the original "cleanupName" as a fallback for the return $name;
case to avoid accidentally breaking stuff for lots of people.
A variety of other common cases that should get handled in elseif in this PR can be found in the function here
https://github.com/psalm/psalm-plugin-wordpress/blob/master/Plugin.php#L378 since those cases are things that we or people reporting an issue there encountered in the wild. You can basically just copy paste the conditions from there I guess.
d57e2ed
to
558c182
Compare
Previously, the hook-name printer was reyling on the parser's pretty printer to generate names, and then post-processing the name to extract string literal values and concatenations. Unfortunately this left some cases unhandled, specifically the case where concatenations are joining function calls with string literals. Any number of other unexpected situations might arise, and there can be defects in the redundant code attempting to parse PHP syntax. In this patch the pretty-printed version of the expression is used only as a fallback in unrecognized situations. Primarily, a direct encoding from the parsed syntax tree to string is used to rely on the parser's own handling of syntax, and making it clearer how to add additional support for other syntaxes.
558c182
to
80ded80
Compare
Description
Previously, the hook-name printer was reyling on the parser's pretty printer to generate names, and then post-processing the name to extract string literal values and concatenations.
Unfortunately this left some cases unhandled, specifically the case where concatenations are joining function calls with string literals. Any number of other unexpected situations might arise, and there can be defects in the redundant code attempting to parse PHP syntax.
In this patch the pretty-printed version of the expression is used only as a fallback in unrecognized situations. Primarily, a direct encoding from the parsed syntax tree to string is used to rely on the parser's own handling of syntax, and making it clearer how to add additional support for other syntaxes.
Testing
In order to compare the output against WordPress Core I wrote a report that stores every hook name as it generated. The following version of
Hook_Reflector::getName()
will write such a report.After running the docs generation with and without this patch, I found four differences by comparing the output reports with
diff -u hook-names.txt hook-names-patch.txt
.As is evident, the impact of this patch on Core is small, but might make for a more obvious path to adding further support for other ways that the hook functions may be called. In this case, I have arbitrarily chosen to embed the call to
get_current_screen()->id
as if it were interpolated into the string in the same way that a variable would be, less the$
.