-
Notifications
You must be signed in to change notification settings - Fork 808
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
Added parsing of PHPDoc blocks based on nikic/php-parser and PHPDoc parser by phpstan. #41306
Conversation
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available. Follow this PR Review Process:
Still unsure? Reach out in #jetpack-developers for guidance! |
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 can confirm it generates both a phpdoc.json
and a markdown.json
. I'm not familiar enough with AST parsing to be confident as to whether it's doing what it should at a low level, though.
I'll note that there are two deprecation notices from the scribu
library, and several instances of this:
Warning: DOMDocument::loadHTML(): Tag summary invalid in Entity, line: 376 in /some/path/jetpack/tools/cli/helpers/doc-parser/runner.php on line 72
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 confident in my understanding of the actual parsing, but it gave no errors or warnings when run as indicated. Various spot-checked hooks and filters seem to match, including those in jetpack_vendor
.
I did get a PHP fatal when I didn't pass a path, so it'd probably be good to catch that with a simple check (see inline comment).
Non-blocking thoughts:
- It dumps phpdoc.json in the monorepo root rather than in the current directory, which was unexpected for me.
- It'd be nice to be able to specify the name of the output file in case we want to generate and compare two files easily.
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.
Most of the comments (e.g. anything talking about adding diagnostics for a potential CI run) can be left for a followup as far as I'm concerned. 🙂
composer.lock
Outdated
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.
Looks like this lock file accidentally got updated? Doesn't matter much if it does, it's just unexpected.
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.
Yes, I think I did it by running composer update
in root instead of the doc parser.
|| $argument->value instanceof Node\Expr\ConstFetch | ||
|| $argument->value instanceof Node\Expr\ClassConstFetch |
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.
These cases probably aren't too useful as-is for the documentation. We want the doc to say "stats_array" or "earn_premium_content_subscription_service", not "self::STATS_ARRAY_TO_STRING_FILTER" or "PAYWALL_FILTER".
Looks like those are the only two we currently have in jetpack-production, BTW. In both cases the constant is defined in the same file, so (probably in a followup) we could probably have some code check for definitions.
Anyway, leaving all that for a followup seems fine to me.
} | ||
return $result; | ||
|
||
} elseif ( $argument->value instanceof Node\Expr\BinaryOp\Concat ) { |
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 whole case seems like it won't be terribly useful for documentation either.
Most of the existing examples could be turned into interpolated strings easily enough.
'jetpack_module_loaded_' . $module
'jetpack_module_configuration_url_' . $module
'jetpack_learn_more_button_' . $module
'jetpack_module_more_info_' . $module
'jetpack_search_terms_' . $module
'jetpack_module_configurable_' . $module
'jetpack_module_more_info_' . $module_slug
'site_settings_endpoint_update_' . $key
'jetpack_feature_' . $feature . '_enabled'
'jetpack_sync_before_send_' . $item[0]
'jetpack_sync_before_send_queue_' . $queue->id
'jitm_' . $envelope->CTA->hook
'jitm_' . $envelope->content->hook
'pre_update_jetpack_option_' . $name
'jetpack_learn_more_button_' . $item['module']
'jetpack_module_more_info_' . $item['module']
'jetpack_module_configurable_' . $plugin['module']
A few would need assigning to a temporary variable for the interpolation, which would probably be worthwhile.
'jetpack_comment_form_enabled_for_' . get_post_type()
'jetpack_comment_form_enabled_for_' . get_post_type()
'jetpack_comment_form_enabled_for_' . get_post_type($post_array['comment_post_ID'])
In this case, I'd personally probably make a separate PR to fix all those to interpolated-strings so we can drop this case.
} | ||
|
||
return $output; | ||
throw new \Exception( 'Unexpected function call argument of type ' . get_class( $argument->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.
When we do the diagnostics, this should probably be turned into one instead of killing the whole process. 😉
Co-authored-by: tbradsha <[email protected]>
Co-authored-by: Brad Jorsch <[email protected]>
Co-authored-by: Brad Jorsch <[email protected]>
…-based-on-ast' into add/docblock-parser-based-on-ast
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.
Works for me to merge this and iterate.
$name_string = $this->pretty_print_hook_name( $hook_name ); | ||
|
||
if ( false === $name_string ) { | ||
continue; | ||
} | ||
|
||
// Purging any comments that could have been attributed to this argument. | ||
$hook_name->setAttribute( 'comments', null ); |
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.
$name_string = $this->pretty_print_hook_name( $hook_name ); | |
if ( false === $name_string ) { | |
continue; | |
} | |
// Purging any comments that could have been attributed to this argument. | |
$hook_name->setAttribute( 'comments', null ); | |
// Purging any comments that could have been attributed to this argument. | |
$hook_name->setAttribute( 'comments', null ); | |
$name_string = $this->pretty_print_hook_name( $hook_name ); | |
if ( false === $name_string ) { | |
continue; | |
} |
$n = $n->getAttribute( 'parent' ); | ||
$docComment = $n->getDocComment(); | ||
} | ||
|
||
$new_block = array( | ||
'type' => $node->name->name === 'apply_filters' ? 'filter' : 'action', | ||
'line' => $node->getLine(), | ||
'end_line' => $node->getEndLine() > 0 ? $node->getEndLine() : $node->getLine(), | ||
'name' => $this->pretty_print_hook_name( $hook_name ), |
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.
'name' => $this->pretty_print_hook_name( $hook_name ), | |
'name' => $name_string, |
} | ||
} | ||
return $result; | ||
} elseif ( $argument->value instanceof Node\Expr\Variable ) { | ||
|
||
// We don't care about variable names, we can't really document them. |
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.
Alternatively, we should avoid using variables so we can document the hooks correctly?
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.
Looks good to merge. I wasn't actually expecting you to incorporate my suggestions in this PR. :^)
Please do merge, and consider the following non-blocking feedback for future iterations:
- When I run
jetpack docs
(no optional params), it stays silent for quite some time, so it's unclear it's working. It'd be good to give some kind of immediate feedback to the user. - I'd probably still require the path-to-project.
- For the destination path, I was expecting to be able to specify the output filename, not just the path. In a perfect world, I'd be able to specify a path (and it uses that path + default filename) or a path + filename.
- If I provide an invalid path I'd expect an immediate error, but it does something weird:
$ jetpack docs asdf
Can't find source directory at: asdf
Preparing doc-parser package...
Can't find source directory at: asdf
Proposed changes:
nikic/php-parser
with help fromphpstan/phpdoc-parser
.apply_filters
anddo_action
calls.docs
command to require a Jetpack production checkout path instead.Other information:
Jetpack product discussion
Does this pull request change what data or activity we track or use?
No.
Testing instructions:
jetpack docs /path/to/jetpack-production/checkout
phpdoc.json
file generated in the folder where you're running the command from.