-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix documented return type for add_link(), edit_link(), wp_update_link() #11094
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 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8bfdfb5
Fix documented return type for add_link(), edit_link(), wp_update_link()
IanDelMar 21c0850
Update src/wp-admin/includes/bookmark.php
IanDelMar b5ed63f
Update src/wp-admin/includes/bookmark.php
IanDelMar 4af2b61
Revert "Update src/wp-admin/includes/bookmark.php"
IanDelMar 86ea883
Update bookmark.php
IanDelMar 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
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.
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.
Right, it cannot return
WP_Errorbecause the$wp_errorarg is not supplied towp_insert_link(). It can, however, returnneverif there is a permission issue.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.
nevercan not be part of a union type. - https://3v4l.org/IWMlC#v8.1.34There 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.
In PHP type hints, yes, but in phpdoc, PHPStan is fine with it, right?
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.
https://phpstan.org/r/d067b4b5-761a-4888-a8f8-c92069b24145
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.
Cc @apermo
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.
@IanDelMar while you are not wrong. And while core only has a single union type with
neveras of now. In recent PRs (#11008, #11012, #11009) they were introduced in the review cycle, in order to improve static analysis with PHPStan. @westonruter suggested to add them as I refactored union types withvoid.As of last week (#10419 / 8a82e67) PHPStan level 0 is part of the Test and Build Scripts, that being said, @westonruter is currently eying PRs that can improve the PHPDoc in order to reduce the noise in PHPStan, and thats the reason to introduce
|neverin case a function might exit inside where PHPStan does not identify that correctly.See as well:
I hope that adds some light to the why.
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.
To me it seems there is a misconception of "subtype" or "never". When you add
neverto a union, you are trying to widen the type. But you simply cannot widen a type by adding a subtype to a union. Again the more straight forward example: You cannot widenmixedby addingstringto it.mixed|stringis stillmixedjust like fruits|apples is still fruits (This is how PHPStan handles unions, see Type Normalization). What you actually want is to narrow the type to a subtype. If some condition is met, we know thatmixedisstring. When the store runs out of any other fruits, I buy apples. This is a conditional return type.In particular, the issue you were trying to solve in ticket 64703 can be addressed without introducing any union that includes
never. Instead, you can use a conditional return type to refinewp_die()fromvoidto its more specific subtypenever, conditional on$args['exit']. See https://phpstan.org/r/b1604252-60e1-4981-bee6-36506466d9ed. In that snippet, I also added a nativevoidreturn type towp_die()to demonstrate that PHPStan correctly treatsneveras a subtype ofvoid. If it did not, PHPStan would flag the@returntag as incompatible with the native return type.So doing something that is weird, like adding
neverto a union "to improve static analysis" might just mask an underlying issue with the code or the PHPDocs used to document it.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 conditional return type actually makes sense.
@westonruter your thoughts?
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.
In regards to
string|mixed, in Performance Lab we have level 8 PHPStan and we write filter callback code like this:The reason is that it is documenting the normal type as strong but acknowledging that other plugins often do bad things and return incorrect values for filters, like
nullorarray. So this is why a native PHP type hint is not used here, to avoid a fatal error.It's true that the type could just be
mixed, but the value lies in the documentation for developers to be able to see at a glance what the expected value will be.This is what I had in mind with using
neverin@returnunion types.But if you feel strongly that there could be issues with adding
never, I'm fine with just documenting thenevercase in the description (e.g. this function may exit if X). Otherwise, implementing never as part of a conditional return should be implemented correctly in #11009.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.
That only works if everyone involved is aware of (and follows) the same convention. Others might be confused. And yes, filters are a bit of a type hell 😄
In the context of your statement that PHPStan is fine with it and the ticket one of the PRs is linked to, it sounded like that it is correct because PHPStan does not flag it and that this was meant to address a concrete problem. That's why I pushed back and tried to make the point that PHPStan doesn't even check for this and demonstrated that it is not required to resolve PHPStan errors.
I am not sure what issues you are referring to exactly. From PHPStan's perspective it still normalises to
intanyway. If there is consensus among core developers that this is the preferred documentation pattern for such cases, then fair enough. Personally, I wouldn't add it. Its documentation value is limited. That's different from your filters example, where the annotation can serve as a reminder that developers must do something (i.e., ensure astringis actually consumed).All the arguments from my earlier comments still hold from my perspective, but ultimately, the decision to add or not add
neverin this specific case is pointless, if there's no broader, consistently applied convention. You’re likely more familiar with WordPress' documentation conventions, so I'll leave it up to you.