Problem/Motivation

There a number of return phpdocs that claim to return bool, but can only return false. This is usually in a union type, for example in \Drupal\Core\File\FileSystemInterface::tempnam:

 * @return string|bool
 *   The new temporary filename, or FALSE on failure.

This subtle difference is important when you narrowing types for stat analysis.

Proposed resolution

Change bool to false, e.g. @return string|false

Limit the scope to @return phpdocs only, not @param, not @var and not native return type declarations.

Issue fork drupal-3499213

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

chi created an issue. See original summary.

chi’s picture

Well, there are a few other cases like this. I suppose the issue needs to be expanded to fix all of them.

ramprassad made their first commit to this issue’s fork.

ramprassad’s picture

Assigned: Unassigned » ramprassad

ramprassad changed the visibility of the branch 3499213-wrong-return-type to hidden.

chi’s picture

Title: Wrong return type in FileSystemInterface::tempnam » Narrow boolean return types
Issue summary: View changes
quietone’s picture

Version: 11.1.x-dev » 11.x-dev
Issue tags: -Documentation, -Novice +Coding standards
Parent issue: » #2571965: [meta] Fix PHP coding standards in core, stage 1

The change being done here is not in agreement with the Drupal coding standards for @return. The standard states

The @return tag is followed by an optional data type indicator, and then a newline. The following paragraph is considered by the API module to be documentation.

So, based on that this is a won't fix. Or, needs an issue in the Coding Standards project to discuss.

I also concerned about how to find the instances that this is suggesting be changed. A simple grep will not work. Ideally, all the coding standards can be enforced with a sniff, so that regressions are not introduced. Is there a sniff for this?

chi’s picture

@quietone Why do you think this violates Drupal coding standards?

 * @return string|false
 *   The new temporary filename, or FALSE on failure.
mstrelan’s picture

I don't see this as a coding standard issue. I also don't see how this violates existing coding standards. The only confusion I can see is that we usually write FALSE, but that's when we're referring to it as a value, not as a data type. The data type indicator notes in the coding standards clarify this.

For the PHP built-in types, use the following names:

  • ...
  • bool (NOT "boolean" or "Boolean"). If only TRUE or only FALSE is a possible value, rather than either one being possible, use true or false instead of bool.
  • false (NOT "FALSE", see bool)
chi’s picture

I checked core code. Found 162 occurrences of false in PHPDoc tags. FALSE is not used at all.

chi’s picture

I also concerned about how to find the instances that this is suggesting be changed. A simple grep will not work.

Instances found with grep need to be filtered manually by comment referring to false.
Like this.

The new temporary filename, or FALSE on failure.

cmlara’s picture

#3271894: StreamWrapperInterface realpath() and dirname() return docblocs are is inconsistent with documented use, actual Core implementation, and intention as related, it discusses some of the streamWrapper returns including LocalStream::getLocalPath()

Not reviewed the MR, however a general +1 to the concept to cleanup documented return types as this hurts contrib that run at PHPStan and will eventually mask errors in Core that PHPStan can detect.

Agree this isn't a Coding Standard issue.

quietone’s picture

I think that because Boolean is the data type, not false or true. https://www.php.net/manual/en/language.types.php. And I do understand that this change helps.

There are currently 1064 instances of bool in and @return statement. What is the plan here so that we know that they are all fixed and how will we prevent future problems?

chi’s picture

I think that because Boolean is the data type, not false or true. https://www.php.net/manual/en/language.types.php.

@quietone true and false are covered there in value types section.

cmlara’s picture

and how will we prevent future problems?

For now that may just have to be the Human Review process.

Core could create a PHPCS rule that the description must contain a format like “True if . False if .” when BOOL is used however that might be a bit more disruptive. I would suggest such should be a follow-up issue as fixing the broken docs is the bigger concern.

Core adopting higher level PHPStans would also help long term but is outside the scope or this issue.

What is the plan here so that we know that they are all fixed

Some missed would be better than none fixed.

chi’s picture

PHPStan currently does not have a rule for checking less specific return types. Psalm does.
I'v just created a feature request for PHPStan project.
https://github.com/phpstan/phpstan/issues/12442

quietone’s picture

@chi, thanks

mstrelan’s picture

Issue summary: View changes
Status: Active » Needs work

Changing to NW because we have an MR. Updated the IS to expand the scope to all @return docs. It might be possible to detect and automate this, but in the meantime we might as well fix the ones we've already identified. We could open a follow up to automate it.

mstrelan’s picture

Status: Needs work » Needs review
borisson_’s picture

Status: Needs review » Reviewed & tested by the community

I think this looks great. I agree with @quietone that i'd prefer to see this go in when we already have a rule.
However since there's not a phpstan rule for this yet and it seems like it's not too many changes for me this seems like it is good to go.
It's an improvement of the current status.

I think the "controversial" point is not controversial at all and I've also verified it can only return false.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new91 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

dcam made their first commit to this issue’s fork.

dcam’s picture

Status: Needs work » Reviewed & tested by the community

Restoring status.

No need to credit me for this. All I did was hit the rebase link.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new91 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

dcam’s picture

Status: Needs work » Reviewed & tested by the community

Restoring status.

Again, no need to credit me for this. All I did was hit the rebase link. I don't know why it thinks the patch doesn't apply, but it rebases cleanly.

alexpott’s picture

Category: Bug report » Task
Status: Reviewed & tested by the community » Fixed

Committed 05d72b3 and pushed to 11.x. Thanks!

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

  • alexpott committed 05d72b35 on 11.x
    Issue #3499213 by chi, ramprassad, quietone, mstrelan, cmlara, borisson_...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.