-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
HTML API: Add PHP type annotations #6753
Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
a89ccd3
to
c4d2ed6
Compare
c4d2ed6
to
ce91715
Compare
These properties should not be null, their types indicate non-null. They are set in the constructor.
This property is not nullable. It should always be defined.
This is supported by PHP 7.1
2d37730
to
a2393b6
Compare
…otations' into html-api/annotate-types
bcf989a
to
63e235f
Compare
I've removed most of the type annotations from the parameters of public APIs. I did this to address concerns about the potential to break sites due to type errors from plugins that are not actionable in any way for the sites. I've maintained the type annotations on the private APIs and internal classes. @dmsnell What do you think about the low-level API like the token map? Should I remove types in the same way there as well? |
@@ -643,7 +641,6 @@ public function next_token() { | |||
return true; | |||
} | |||
|
|||
|
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 won't ask to remove this, but it was intentionally added to give a little bit more separation between the next_
methods and the query/read methods
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.
Oh, the two empty lines seemed out of place. I'm surprised PHPCS didn't complain, in other places that's a lint error.
I can restore the line or we could add an organizational comment, thoughts?
@sirreal I think all of this as is is probably fine, but I want to run this against the Gutenberg repository first to ensure that nothing basic is broken, since we're introducing new intentional-breaking behaviors. |
Backport contains these anticipated updates in the following Gutenberg PR. |
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.
The backport into Gutenberg did not immediately break everything, so I will merge this into Core.
This patch adds type annotations to internal and private methods of the HTML API and the supporting WP_Token_Map. Annotations have not been added to the public interfaces where it would likely crash a site if called wrong. These annotations should help avoid unnecessary type-related bugs (as have been uncovered in earlier work adding such annotations) and provide additional guidance to developers when interacting with these classes in an IDE. Developed in #6753 Discussed in https://core.trac.wordpress.org/ticket/61399 Props dmsnell, jonsurrell. See #61399. git-svn-id: https://develop.svn.wordpress.org/trunk@58769 602fd350-edb4-49c9-b593-d223f7449a82
Merged in [58769] |
This patch adds type annotations to internal and private methods of the HTML API and the supporting WP_Token_Map. Annotations have not been added to the public interfaces where it would likely crash a site if called wrong. These annotations should help avoid unnecessary type-related bugs (as have been uncovered in earlier work adding such annotations) and provide additional guidance to developers when interacting with these classes in an IDE. Developed in WordPress/wordpress-develop#6753 Discussed in https://core.trac.wordpress.org/ticket/61399 Props dmsnell, jonsurrell. See #61399. Built from https://develop.svn.wordpress.org/trunk@58769 git-svn-id: http://core.svn.wordpress.org/trunk@58171 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This patch adds type annotations to internal and private methods of the HTML API and the supporting WP_Token_Map. Annotations have not been added to the public interfaces where it would likely crash a site if called wrong. These annotations should help avoid unnecessary type-related bugs (as have been uncovered in earlier work adding such annotations) and provide additional guidance to developers when interacting with these classes in an IDE. Developed in WordPress/wordpress-develop#6753 Discussed in https://core.trac.wordpress.org/ticket/61399 Props dmsnell, jonsurrell. See #61399. Built from https://develop.svn.wordpress.org/trunk@58769 git-svn-id: https://core.svn.wordpress.org/trunk@58171 1a063a9b-81f0-0310-95a4-ce76da25c4cd
@dmsnell @sirreal: |
@TobiasBg some of the annotations were removed to prevent site crashes when improperly called. I think we are still gathering experience knowing how to handle this scenario. the PHDDoc comment adds the types, right? so IDE's should still type-check, autocomplete, and auto-document the parameters. I've seen plenty of well-intentioned efforts to add type annotations only to realize that something was a mistake and now WordPress crashes because the types were wrong. I'm sensitive to avoid doing this out of over-eagerness. |
@dmsnell: Yes, PHPDocs (correctly) adds the types for syntax highlighting, autocomplete, documentation, use in static analysis, etc. But these are pretty much "optional". Only the annotations let PHP raise an error message if a parameter has the wrong type. And of course that's why type annotations should be added with care (and e.g. prevent with custom type checks in the code) to prevent crashes! Thus, excluding public-facing functions from this totally makes sense! I was more referring to "inconsistencies": E.g. in public function contains( string $word, string $case_sensitivity = 'case-sensitive' ): bool { both parameters (including public function read_token( string $text, int $offset = 0, &$matched_token_byte_length = null, $case_sensitivity = 'case-sensitive' ): ?string { only the first parameter got an annotation while the same Similarly, in trunk/src/wp-includes/html-api/class-wp-html-open-elements.php, there's the line public function has_element_in_specific_scope( string $tag_name, $termination_list ): bool { where only the first parameter got an annotation. |
@TobiasBg happy to review a patch! |
Trac ticket: Core-61399
Add type annotations in the HTML API where possible. In this PR I've avoided adding annotations where the supported annotations are worse that PHPDoc annotations.
For example, I've avoided using the
array
type where the@param
or@return
annotations are more expressive, or amixed
type instead of a type union.I've extracted some bugfixes that arose as part of this work to #6754, that's a smaller patch that can be landed independently.
Includes #7031.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.