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
Add sniff for 'void' in @return tags #1240
base: develop
Are you sure you want to change the base?
Conversation
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 the PR! ✨ This is looking good. There are just a few tweaks I'd like to see before this is merged. The other committers may also have some feedback.
It also looks like the Travis build is failing due to the fact that we apparently use @return void
in WPCS. 😮
I think we'll probably want to go ahead and fix those as part of this PR.
foreach ( $returnTypes as $type ) { | ||
if ( 'void' === $type ) { | ||
$this->phpcsFile->addError( | ||
sprintf( '`@return void` should not be used outside of the default bundled themes', $type ), |
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.
sprintf()
is unnecessary here, since the message doesn't contain any placeholders.
Also, I think that perhaps the message should be simplified to just be something like @return void should not be used
. I don't think it is necessary to mention the exception for the bundled themes.
Perhaps the message should include something like "null
should be used instead, if needed, or the @return
tag should be omitted".
/** | ||
* Return types should not be void. | ||
* | ||
* @return void |
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.
For thoroughness, it would be good to add some tests that also include a comment:
* @return void Nothing.
* | ||
* @package WPCS\WordPressCodingStandards | ||
* | ||
* @since X.Y.Z |
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 version number will end up being 0.16.0 (0.15.0 is going to be dedicated to just renaming some things).
WordPress-Docs/ruleset.xml
Outdated
@@ -116,4 +116,6 @@ | |||
<!-- WP allows @todo's in comments --> | |||
<exclude name="Generic.Commenting.Todo.TaskFound"/> | |||
</rule> | |||
|
|||
<rule ref="WordPress.Commenting.NoReturnVoid"/> |
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.
A comment could be added here referencing the handbook rule that this sniff relates to.
Thanks for the feedback. I've pushed some changes to address your comments. On this point:
I used a slightly different set of messages because, as I understand it, |
I can't think of case where |
@@ -86,8 +86,8 @@ public function register() { | |||
* | |||
* @param int $stackPtr The position of the current token in the stack. | |||
* | |||
* @return int|void Integer stack pointer to skip forward or void to continue | |||
* normal file processing. | |||
* @return int Integer stack pointer to skip forward or void to continue |
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.
My understanding is, that when a method could return a something or void, that @return int|void
is allowed.
What's not allowed is the case of simply @return void
.
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.
Maybe @DrewAPicture could weigh in on this.
I would agree with Gary. There are certainly functions that have echo vs return arguments where I would hesitate to document that it only returns. So if it's not too hard to allow void only when there are multiple return types (outside of bundled themes) that would be preferable. |
Seeing that next commit, I'm reminded that we've had this discussion before, and I guess the question is, should it be something like: /**
* @return int|null ...
*/ ...rather than: /**
* @return int|void ...
*/ ...when at least one path returns something, since the other implicitly returns See https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#examples-14 which seems to support this. That way, There could be a separate message which checks for any use of To summarise:
|
My two pennies: Regarding multiple types: always document
Regarding types: these are the allowed types as per the handbook/inventory I made previously:
Regarding
When the return does not have an explicit type and the possible types can not be easily determined, I'd think As a side-note: As there are quite some more rules regarding the On a personal note: I will disable this rule in all my projects as I think it's plain bad practice. On that note, I'd be in favour of excluding the sniff in the WPCS native ruleset in |
This reverts commit f8467b2.
Thank you to everyone for your comments. I've pushed some updates that I think bring the PR in line with the requested changes:
Please let me know if I've missed or misunderstood any of the feedback. |
Extended with https://3v4l.org/AgenG (no change). |
@dlh01 Thanks for making those changes. I'll have a more detailed look at the PR next week. |
Also needed in interface definitions, because there is no method body to look at. |
This sniff attempts to generate an error when
void
appears in a PHPDoc@return
tag, per the core documentation standard that "@return void should not be used outside of the default bundled themes." (The default themes would need to disable this sniff, of course.)The code is largely based on the various
FunctionCommentSniff
instances in PHPCS. I welcome everyone's comments and suggestions for improvement.