Missing NULL in hook_file_download() return documentation

Created on 22 January 2025, 6 days ago

Problem/Motivation

 * @return string[]|int
 *   If the user does not have permission to access the file, return -1. If the
 *   user has permission, return an array with the appropriate headers. If the
 *   file is not controlled by the current module, the return value should be
 *   NULL.

The documentation for hook_file_download() contradicts itself because NULL is a valid return type that is not listed in @return.

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

documentation

Created by

🇷🇺Russia niklan Russia, Perm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @niklan
  • 🇷🇺Russia niklan Russia, Perm
  • Pipeline finished with Failed
    6 days ago
    Total: 401s
    #402688
  • The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. 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.

  • 🇷🇺Russia niklan Russia, Perm
  • The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. 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.

  • 🇷🇺Russia Chi

    I wonder if we can explicitly document -1 in return type. PHPStan allows specifying scalar values as types in PHPDocs.

    https://phpstan.org/writing-php-code/phpdoc-types#literals-and-constants

  • 🇷🇺Russia zniki.ru

    I don't understand why review bot fails on phpstan. MR check looks good.
    Add tag to not ignore bot.

    @chi this looks like a good suggestion. I believe there are more places where this can be done.

  • 🇬🇧United Kingdom oily Greater London

    @niklan In the issue summary

    The documentation for hook_file_download() contradicts itself because NULL is a valid return type that is not listed in @return.

    I think you mean the documentation is 'contradictory'. I dont understand the rest of the sentence. How is the documentation contradictory? I can see it has something to do with a NULL return type. But you dont explain what. Can you please edit it.

  • 🇬🇧United Kingdom oily Greater London
  • 🇬🇧United Kingdom oily Greater London
  • 🇬🇧United Kingdom oily Greater London
  • 🇷🇺Russia Chi

    Re #9.
    The documentation for the @return tag consists of two parts: the type hint and a free-form description. I believe the issue is that they contradict each other.

  • 🇵🇱Poland hubertinio

    I'm checking
    #ContributionWeekend2025 #WarsawContributionWeekend2025

  • 🇵🇱Poland hubertinio

    once more with tags

  • 🇵🇱Poland piotr pakulski Poland 🇪🇺

    mentoring on this

  • 🇵🇱Poland hubertinio

    Tested, looks good for me

  • 🇬🇧United Kingdom oily Greater London

    @chi RE: #12 Yes, I groped through the darkness as far as i could but #29 shines a light on the issue. The issue summary looks good to go. The MR code is hard to disagree with, too. Move to RTBTC?

    I mean the only problem i could envisage is if it turns out that any of the 3x return types is wrong. But that would seem outside the scope.

  • 🇵🇱Poland Graber
    1. Where does the function actually return int?
    2. It doesn't return anything if the if condition is not met (void).

    I think we should add a :?array return type hint and actually return NULL if the if condition is not met.

  • 🇵🇱Poland Graber

    Ok, actually see file_file_download(), it returns void in all the cases instead of NULL. I think that should be included in the doc comment.

  • 🇬🇧United Kingdom oily Greater London

    I think #19 needs test coverage so would be in a follow-up.

  • 🇷🇺Russia Chi

    It doesn't return anything if the if condition is not met (void).

    Right. Needs to be fixed.

    I think we should add a :?array|int

    Union types and the nullable type notation cannot be mixed. Anyway, as this issue is about @return tag I propose we don't add the typehint here. This is managed in other issue. 📌 [META] Add return types to hook implementations Active .

  • 🇷🇺Russia niklan Russia, Perm

    Ok, actually see file_file_download(), it returns void in all the cases instead of NULL.

    But we can't use void as the return type and combine it with other types. Does that mean all hook implementations must explicitly return NULL and void return should be deprecated somehow?

  • 🇷🇺Russia Chi

    Re #20. Turns out all implementations in Drupal core use void instead of null. Given, can only be used as a standalone type we have to update them. Otherwise we will never be able to add native return type hint to this hook. I agree, that needs to be done in a follow-up.

  • 🇵🇱Poland Graber

    graber changed the visibility of the branch drupal-3501398-3501398-missing-null-in to hidden.

  • Pipeline finished with Failed
    3 days ago
    Total: 111s
    #405516
  • Pipeline finished with Failed
    3 days ago
    Total: 144s
    #405526
  • Pipeline finished with Success
    3 days ago
    Total: 366s
    #405532
  • 🇵🇱Poland Graber

    This will not break any contrib modules implementing this hook and still returning void, because we don't have a type hint and it is still a hook so even if we had a type hint in file.api.php file, hook implementations wouldn't necessarily add those and we don't check the return type after invoking this hook.
    However, we need a change record so contrib module maintainers will now return NULL in their implementations for consistency.

  • 🇵🇱Poland Graber

    Looks good, thanks!

Production build 0.71.5 2024