Skip to content
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

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from

Conversation

dlh01
Copy link
Contributor

@dlh01 dlh01 commented Nov 26, 2017

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.

Copy link
Contributor

@JDGrimes JDGrimes left a 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. 😮

https://travis-ci.org/WordPress-Coding-Standards/WordPress-Coding-Standards/jobs/307321215#L1316-L1977

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 ),
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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).

@@ -116,4 +116,6 @@
<!-- WP allows @todo's in comments -->
<exclude name="Generic.Commenting.Todo.TaskFound"/>
</rule>

<rule ref="WordPress.Commenting.NoReturnVoid"/>
Copy link
Contributor

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.

@dlh01
Copy link
Contributor Author

dlh01 commented Nov 29, 2017

Thanks for the feedback. I've pushed some changes to address your comments.

On this point:

Perhaps the message should include something like "null should be used instead, if needed, or the @return tag should be omitted".

I used a slightly different set of messages because, as I understand it, null and void are not always interchangeable, but let me know if I've misunderstood what you were looking for.

@westonruter
Copy link
Member

I can't think of case where null and void would not be identical: https://3v4l.org/a9stV

@@ -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
Copy link
Member

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|voidis allowed.

What's not allowed is the case of simply @return void.

Copy link
Contributor

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.

@DrewAPicture
Copy link
Member

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.

aristath added a commit to themeum/kirki that referenced this pull request Nov 29, 2017
@GaryJones
Copy link
Member

GaryJones commented Nov 29, 2017

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 null.

See https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#examples-14 which seems to support this.

That way, @return void is the only possible usage of void (when there is no return statement at all in the function/method), and in this case, and allowed by the draft PSR-5, we say that the annotation should not be present.

There could be a separate message which checks for any use of void in a @return annotation, and indicate that it should be null when combined with other types.

To summarise:

  • @return void - not allowed, even when there is no return statement.
  • @return string|null - example, when one path returns and another echoes.
  • @return null - only if the function explicitly return null;
  • ??? - when all returns have no type: return;.

@jrfnl
Copy link
Member

jrfnl commented Nov 29, 2017

My two pennies:

Regarding multiple types: always document void if there are multiple types, otherwise the documentation is plain wrong and will confuse people.
From the handbook/inventory I made previously:

Return should list all possible return types

Regarding types: these are the allowed types as per the handbook/inventory I made previously:

Valid types: bool, int, string, float, object, array, resource, callback, void or FQ classname

Regarding null versus void: According to the above list, void should be used.
Personally, I'd allow null, but only if there is an explicit return null somewhere in the code.

??? - when all returns have no type: return;.

When the return does not have an explicit type and the possible types can not be easily determined, I'd think @return mixed should be acceptable, but only as a last resort.

As a side-note: As there are quite some more rules regarding the @return tag, I'm wondering if this should be a separate sniff or if all the rules regarding @return tags should be examined in one sniff.

On a personal note: I will disable this rule in all my projects as I think it's plain bad practice.
If there is no @return tag, you don't know whether someone has been sloppy in documenting the method or if the return is void. Thar's why void IMHO should always be documented.

On that note, I'd be in favour of excluding the sniff in the WPCS native ruleset in bin, rather than to remove the existing documentation.
After all, this project will not be using the documentation generation tools which WP uses.

@dlh01
Copy link
Contributor Author

dlh01 commented Dec 10, 2017

Thank you to everyone for your comments. I've pushed some updates that I think bring the PR in line with the requested changes:

  • Rename the sniff to a FunctionCommentSniff with a process_return() method to accommodate other tests more easily. These names are intended to mimic the existing sniffs and methods in PHPCS.
  • Allow void to appear with other return types. (In other words, disallow only @return void.)
  • Exclude NoReturnVoid from the WPCS ruleset.

Please let me know if I've missed or misunderstood any of the feedback.

@GaryJones
Copy link
Member

I can't think of case where null and void would not be identical: https://3v4l.org/a9stV

Extended with https://3v4l.org/AgenG (no change).

@jrfnl
Copy link
Member

jrfnl commented Dec 13, 2017

@dlh01 Thanks for making those changes. I'll have a more detailed look at the PR next week.

@mundschenk-at
Copy link

On a personal note: I will disable this rule in all my projects as I think it's plain bad practice.
If there is no @return tag, you don't know whether someone has been sloppy in documenting the method or if the return is void. Thar's why void IMHO should always be documented.

Also needed in interface definitions, because there is no method body to look at.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants