Repository navigation
Docs: Add refined string types to PHPStan return and parameter types #13892
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
Closed
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7fa3597
Docs: Add refined string types to PHPStan return and parameter types.
swissspidy d83c939
Merge remote-tracking branch 'upstream/trunk' into fix/phpstan-strings
claude 073ece9
Docs: Address review feedback on string types.
claude 961506e
Merge remote-tracking branch 'upstream/trunk' into fix/phpstan-strings
claude 8ccf26c
Merge branch 'trunk' into fix/phpstan-strings
westonruter 31fa5b2
Assert the type of `$wp_version` in `wp_get_wp_version()`.
westonruter 8290702
Type the static `$regex` in `get_html_split_regex()`.
westonruter 5a8cf6b
Assert the lowercase return type of `sanitize_title_with_dashes()`.
westonruter 4ccdf6a
Return a list of lowercase hosts from `wp_internal_hosts()`.
westonruter 172abeb
Assert the lowercase return type of `wp_generate_uuid4()`.
westonruter b969408
Assert a non-falsy path when `path_is_absolute()` returns true.
westonruter c7ab136
Narrow the taxonomy key type for `register_taxonomy()`.
westonruter f4474a9
Narrow the post status name type for `register_post_status()`.
westonruter cba30d8
Narrow the ability and ability category name types.
westonruter c8cd212
Narrow the block bindings source name type.
westonruter a51d0e1
Narrow the icon and icon collection name types.
westonruter 660115f
Narrow the connector ID type.
westonruter b36cc6e
Narrow the block type name type in the block type registry.
westonruter b21d89e
Narrow the block template name type.
westonruter 4eb2a8d
Narrow the menu slug type for admin menu page functions.
westonruter 1b05091
Use `@phpstan-var` for the icon manifest array shape.
westonruter a88ab20
Use `@var` again for the icon manifest array shape.
westonruter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
it seems like we have been adding a lot of aspirational, but wrong type annotations lately, and I’m really concerned about the choices we’re making in response to PHPStan warnings.
is it not obvious by inspection that this can return a false string? maybe I’m wrong, but it seems like
get_comment()can returnnullandget_avatar()can returnfalse, which leads tostring(1) " "which itself is falsy.if this is the case, these more-frequent invalid type annotations only hide type errors and make them harder to find, and they taint the type checking even further by asserting that the type is not what the code says, but that there is some reason to trust us rather than the code.
at least this won’t case crashes, being the docblock annotation, but it can still cause headaches when debugging why a
falseappears where the type system claims it cannotThere 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.
Actually,
" "is truthy: https://3v4l.org/Ft5fq#veolOtherwise, if it had indeed been falsy, then an aspirational type would be wrong.
If there is any other example which is aspirational you see, I'm happy to revisit!
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.
Also, most of these narrowed types here are not to address any warnings. They are intended to improve correctness so that callers and callees have as specific type information available.
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.
thanks for refuting me on that @westonruter
mostly it’s with these dependent-type annotations like
non-empty-stringornon-empty-array— these seem frail, and by aspirational I mean it’s what we expect, but not necessarily what can occur.I tend to skip over these without too much thought, and maybe every time I see them I’m wrong, so I’ll try and pay more attention, though I definitely cannot keep up with the pace of PHPStan changes.
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.
@dmsnell The hope is that you won't have to think about these. We can offload the thinking to PHPStan. If the method here is ever modified to do something like this instead:
Then PHPStan would raise an error here:
In this way, I don't think they are frail. They are being checked for their truthfulness (and truthiness!)
The hope is that having these narrowed types will allow users of the method to avoid dead code. For example, if someone did this:
Then PHPStan can flag the
if()condition as anif.alwaysTrueerror.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.
if we’re overly conservative that seems fine, but we are seeming to be overly-optimistic in these situations.
it seems that either way, in this case with the comment and avatar, things are not changed, but some strings are falsey, like
"0". and I think theabs_int()example is illustrative.PHP and WP code generally has done a poor job of indicating the real possible types. I understand PRs like this exist to improve that situation.
in the example, I think the conflation of types inside
floated_admin_avatar()is more of the problem than theif ( $avatar )(though I would agree it’s also a problem). it would seem more robust to perform the checks withinfloated_admin_avatar()and return a positive signal of failure rather than an implicit behavior, e.g.string|null.in this particular case I mis-identified the type as being wrong, so it’s not important to focus on it. that was my mistake.