While working on 'optional' fixes in #2608732: Some fixes for 'optional' parameter in core/modules, it became clear file module's file.module file needs some docblock love. In order to keep patch sizes reasonable, this issue was broken out and uses the work initiated there as a start. It now includes a number of other docblock changes, like proper formatting of a list of items.

Please limit any changes in this issue to docblock improvement only in this one file in the file module. Also to speed reviews, please create patches with enhanced context lines (using -U switch with git diff). Thanks.

Comments

Lars Toomre created an issue. See original summary.

lars toomre’s picture

Status: Active » Needs review
StatusFileSize
new18.12 KB

Attached is an initial patch for this issue. A second review of the file.module file (with this patch attached) would be appreciated. I am unsure yet if this catches all of the docblock changes that should be made. Thanks.

jhodgdon’s picture

Status: Needs review » Needs work

Thanks for the patch! Sorry for the delay in reviewing -- I've been on vacation and no one else, unfortunately, ever reviews docs patches. :(

So most of the changes here look good! A few mostly minor things to fix:

  1. +++ b/core/modules/file/file.module
    @@ -49,15 +49,17 @@ function file_help($route_name, RouteMatchInterface $route_match) {
      * @param array $fids
    - *   (optional) An array of entity IDs. If omitted, all entities are loaded.
    + *   (optional) An array of entity IDs. If omitted or NULL, all entities are
    + *   loaded.
    

    Looks like this can be array|null ?

  2. +++ b/core/modules/file/file.module
    @@ -114,24 +117,25 @@ function file_load($fid, $reset = FALSE) {
      * @param string $destination
    - *   A string containing the destination that $source should be copied to.
    - *   This must be a stream wrapper URI.
    + *   (optional) A string containing the destination that $source should be
    + *   copied to. This must be a stream wrapper URI. Defaults to NULL.
    

    Can be string|null ?

  3. +++ b/core/modules/file/file.module
    @@ -114,24 +117,25 @@ function file_load($fid, $reset = FALSE) {
    + *   (optional) Replace behavior when the destination file already exists.
    + *   Defaults to FILE_EXISTS_RENAME.  Possible values include:
    + *   - FILE_EXISTS_REPLACE: Replace the existing file. If a managed file with
    + *     the destination name exists, then its database entry will be updated. If
    + *     no database entry is found, then a new one will be created.
    + *   - FILE_EXISTS_RENAME: Append _{incrementing number} until the filename is
    + *     unique.
    

    Normally in a list like this, we like to instead of indicating the default above the list, put (default) into the list element that is the default value.

  4. +++ b/core/modules/file/file.module
    @@ -189,26 +193,26 @@ function file_copy(FileInterface $source, $destination = NULL, $replace = FILE_E
      * @param string $destination
    - *   A string containing the destination that $source should be moved to.
    - *   This must be a stream wrapper URI.
    + *   (optional) A string containing the destination that $source should be moved
    + *   to. This must be a stream wrapper URI. Defaults to NULL.
    

    string|null then?

  5. +++ b/core/modules/file/file.module
    @@ -189,26 +193,26 @@ function file_copy(FileInterface $source, $destination = NULL, $replace = FILE_E
    + *   (optional) The replace behavior when the destination file already exists.
    + *   Defaults to FILE_EXISTS_RENAME.  Possible values include:
    

    See above note about list and defaults

  6. +++ b/core/modules/file/file.module
    @@ -265,16 +269,16 @@ function file_move(FileInterface $source, $destination = NULL, $replace = FILE_E
    + *   should return an array of error messages; an empty array (the default)
    

    Um. This is talking about the return value of the callbacks. How does a return value have a default? I think that (the default) is incorrect here.

  7. +++ b/core/modules/file/file.module
    @@ -403,21 +407,22 @@ function file_validate_is_image(FileInterface $file) {
    + *   (optional) A string in the form WIDTHxHEIGHT e.g. '640x480' or '85x85'. If
    

    Can we fix the "e.g." punctuation, and preferably use "for example"? Should be:

    ... WIDTHxHEIGHT; for example, '640x480' ...

  8. +++ b/core/modules/file/file.module
    @@ -403,21 +407,22 @@ function file_validate_is_image(FileInterface $file) {
    + *   dimensions. A value of 0 (the default) indicates no restriction on size, so
    

    If 0 is the default, then the input can be string|int?

  9. +++ b/core/modules/file/file.module
    @@ -403,21 +407,22 @@ function file_validate_is_image(FileInterface $file) {
    + *   image meets a minimum size. A value of 0 (the default) indicates no
    

    If 0 is the default, the value can be string|int?

  10. +++ b/core/modules/file/file.module
    @@ -403,21 +407,22 @@ function file_validate_is_image(FileInterface $file) {
    + * @return array
      *   An array. If the file is an image and did not meet the requirements, it
      *   will contain an error message.
    

    So is it string[] or ... what does it contain if the requirements are satisfied?

  11. +++ b/core/modules/file/file.module
    @@ -458,25 +463,27 @@ function file_validate_image_resolution(FileInterface $file, $maximum_dimensions
      * @param string $destination
    - *   A string containing the destination URI. This must be a stream wrapper URI.
    - *   If no value is provided, a randomized name will be generated and the file
    - *   will be saved using Drupal's default files scheme, usually "public://".
    + *   (optional) A string containing the destination URI. This must be a stream
    + *   wrapper URI. If no value or NULL is provided, a randomized name will be
    

    Looks like this is string|null then?

  12. +++ b/core/modules/file/file.module
    @@ -458,25 +463,27 @@ function file_validate_image_resolution(FileInterface $file, $maximum_dimensions
    + *   (optional) The replace behavior when the destination file already exists.
    + *   Defaults to FILE_EXISTS_RENAME.  Possible values include:
    

    See note above about defaults in lists of options.

  13. +++ b/core/modules/file/file.module
    @@ -667,43 +674,44 @@ function file_cron() {
    + *   (optional) The replace behavior when the destination file already exists.
    + *   Defaults to FILE_EXISTS_RENAME. Possible values include:
    

    See note above about defaults and lists.

  14. +++ b/core/modules/file/file.module
    @@ -667,43 +674,44 @@ function file_cron() {
    + * @return array|null
      *   Function returns array of files or a single file object if $delta
    

    Doesn't seem accurate. Says it can return also a single file object, and looks like it can also return FALSE. ???

  15. +++ b/core/modules/file/file.module
    @@ -1429,24 +1437,24 @@ function file_icon_map($mime_type) {
      *   EntityStorageInterface::FIELD_LOAD_CURRENT to retrieve references
    - *   only in the current revisions.
    + *   only in the current revision.
    

    Actually I think revisions is correct in this case, meaning "revisions of all entities that have references to this file".

  16. +++ b/core/modules/file/file.module
    @@ -1429,24 +1437,24 @@ function file_icon_map($mime_type) {
    + *   returned.  Defaults to 'file'.
    

    Extra space after .

  17. +++ b/core/modules/file/file.module
    @@ -1517,15 +1525,16 @@ function file_get_file_references(FileInterface $file, FieldDefinitionInterface
    + *   The file status as a text string.
    

    could leave out "text" here?

lars toomre’s picture

Status: Needs work » Needs review
StatusFileSize
new8.3 KB
new18.98 KB

Thanks for the review @jhodgdon. It was reassuring to see that I was consistent in making the same list type of error(s). Sorry for the trailing whitespace issue(s). I thought my editor was set-up to eliminate those, but it was not. That editor setting now has been changed so there should be no more issues of that type.

Attached is an interdiff and patch that should address each of your comments from #3. Let's see what the test bot thinks.

jhodgdon’s picture

Status: Needs review » Needs work

Thanks for the new patch! I took a careful look through the patch (not interdiff) again... looking much better!

There are still a couple of things to fix:

  1. +++ b/core/modules/file/file.module
    @@ -265,18 +269,20 @@ function file_move(FileInterface $source, $destination = NULL, $replace = FILE_E
    + *   (optional) An associative array of callback functions used to validate the
      *   file. The keys are function names and the values arrays of callback
    - *   parameters which will be passed in after the file entity. The
    - *   functions should return an array of error messages; an empty array
    - *   indicates that the file passed validation. The functions will be called in
    - *   the order specified.
    + *   parameters which will be passed in after the file entity. The functions
    + *   should return an array of error messages. An empty array (the default)
    + *   indicates that the file passed validation within this module.  The hook
    + *   'hook_file_validate() will nonetheless be called so other modules can
    + *   validate the new file. The callback functions will be called in the order
    + *   specified in the array.
    

    Um. I think I mentioned this in the last review, but I guess I wasn't clear.

    So take a look at how this reads... to summarize:

    It's an optional array of callback functions.

    The keys are function names. Then it documents these callbacks:

    The values in the array are the parameters to be passed in, after the file entity.

    The return value of the function tells whether the file passed validation.

    Now here's the problem: the docs say:

    An empty array [return value] ***(the default)*** indicates passing validation.

    That "the default" is nonsensical -- a function return value does not have a default.

    Also in this patch, we *lost* the important information that the return value should be an array of error messages, with empty array indicating it passed.

    So I think the previous paragraph was better in this sentence than the patch; please restore it.

    Also there are two spaces after the . before the "The hook..." sentence, and in the next line, there is a stray ' at the start of the line.

    Then it documents how the

  2. +++ b/core/modules/file/file.module
    @@ -402,22 +408,23 @@ function file_validate_is_image(FileInterface $file) {
    + * @return string[]
      *   An array. If the file is an image and did not meet the requirements, it
      *   will contain an error message.
    

    Given the previous discussion... Do you think this should say it's an array of validation error messages, with an empty array indicating no problems?

  3. +++ b/core/modules/file/file.module
    @@ -663,43 +672,45 @@ function file_cron() {
    + *   file. See file_validate() for a full discussion of the array format. If no
    + *   extension validator is provided (the default), it will default to a limited
    + *   safe list of extensions, which are as follows: "jpg jpeg gif png txt doc
    + *   xls pdf ppt pps odt ods odp". To allow all extensions, you must explicitly
    + *   set the 'file_validate_extensions' validator to an empty array. (Beware:
    + *   This is not safe and should only be allowed for trusted users, if at all).
    

    Maybe since we need another patch anyway, we should fix up a few nitpicks here:

    The sentence about "If no extension..." is a bit weird. It says "it" will default to a limited safe set of extensions, but "it" refers to the entire array of callbacks, so this doesn't really make sense.

    Really I think that sentence should say something like:

    If the array is empty, it will be set up to call file_validate_extensions() with a safe list of extensions, as follows: "jpg .....".

    And then the next sentence should be:

    To allow all extensions, you must explicitly set this array to ['file_validatate_extensions' => ''].

    And then in the last sentence, the . should be inside the ) at the end.

  4. +++ b/core/modules/file/file.module
    @@ -663,43 +672,45 @@ function file_cron() {
    + *   - FILE_EXISTS_RENAME: (optional) Append _{incrementing number} until the
    

    I think this is (default) and not (optional) ?

  5. +++ b/core/modules/file/file.module
    @@ -663,43 +672,45 @@ function file_cron() {
      *   The documentation for the "File interface" group, which you can find under
      *   Related topics, or the header at the top of this file, documents the
    

    This is not actually accurate. If you look at
    https://api.drupal.org/api/drupal/core!modules!file!file.module/function...
    there is no related topic for "File interface".

    And anyway, we're now returning file entities, not some generic object thing.

    So.

    Really @return should say something like FileInterface[] and not array, to indicate what is in the array (figure out the right interface/class and include the namespace).

    And then I think this whole paragraph is irrelevant and should be removed.

    Right?

  6. +++ b/core/modules/file/file.module
    @@ -1425,24 +1436,25 @@ function file_icon_map($mime_type) {
      * @param \Drupal\Core\Field\FieldDefinitionInterface $field
      *   (optional) A field definition to be used for this check. If given, limits the
    - *   reference check to the given field.
    + *   reference check to the given field. Defaults to NULL.
    

    Should be type |null right?

  7. +++ b/core/modules/file/file.module
    @@ -1425,24 +1436,25 @@ function file_icon_map($mime_type) {
    + *   returned.  Defaults to 'file'.
    

    Extra space after .

rang501’s picture

Status: Needs work » Needs review
StatusFileSize
new16.29 KB
new4.2 KB

I took the liberty to fix the patch according to the review above. Hopefully I got it right.

jhodgdon’s picture

Status: Needs review » Needs work

Thanks for the new patch -- still some problems though:

  1. +++ b/core/modules/file/file.module
    @@ -268,12 +272,15 @@ function file_move(FileInterface $source, $destination = NULL, $replace = FILE_E
    + *   parameters which will be passed in after the file entity. The functions
    + *   should return an array of error messages; an empty array
      *   indicates that the file passed validation. The functions will be called in
      *   the order specified.
    + *   The hook hook_file_validate() will nonetheless be called so other modules can
    + *   validate the new file. The callback functions will be called in the order
    + *   specified in the array.
    

    This section of documentation needs to be rewrapped to as close to 80 character lines as possible.

    Also I wouldn't say "the hook hook_whatever() ... will be called" but instead "hook_whatever() will be invoked"

  2. +++ b/core/modules/file/file.module
    @@ -345,11 +352,11 @@ function file_validate_extensions(FileInterface $file, $extensions) {
      * @param int $file_limit
    - *   An integer specifying the maximum file size in bytes. Zero indicates that
    - *   no limit should be enforced.
    + *   (optional) An integer specifying the maximum file size in bytes. Zero (the
    + *   default) indicates that no limit should be enforced.
    

    I think we can drop the words "the integer specifying" here to be much more concise.

  3. +++ b/core/modules/file/file.module
    @@ -380,7 +387,7 @@ function file_validate_size(FileInterface $file, $file_limit = 0, $user_limit =
    + *   An array with validation errors, empty array indicates no problems.
    

    This is a comma splice. make it into two sentences or use a ;

  4. +++ b/core/modules/file/file.module
    @@ -666,39 +676,34 @@ function file_cron() {
    + *   set this array to ['file_validate_extensions' => '']. (Beware:
    + *   This is not safe and should only be allowed for trusted users, if at all.)
    

    The "beware" sentence should not have a capital letter on "This", and the lines need to be rewrapped to closer to 80 characters.

  5. +++ b/core/modules/file/file.module
    @@ -666,39 +676,34 @@ function file_cron() {
    + * @return FileInterface[]|null|false
      *   Function returns array of files or a single file object if $delta
      *   != NULL. Each file object contains the file information if the
      *   upload succeeded or FALSE in the event of an error. Function
    

    Check the return value type again... doesn't agree with the text.

    And if we're fixing this, we should also take out the "Functions returns" and punctuate this better.

  6. +++ b/core/modules/file/file.module
    @@ -666,39 +676,34 @@ function file_cron() {
      *   upload succeeded or FALSE in the event of an error. Function
    - *   returns NULL if no file was uploaded.
    - *
    - *   The documentation for the "File interface" group, which you can find under
    - *   Related topics, or the header at the top of this file, documents the
    - *   components of a file entity. In addition to the standard components,
    - *   this function adds:
    - *   - source: Path to the file before it is moved.
    - *   - destination: Path to the file after it is moved (same as 'uri').
    + *   returns NULL if no file was uploaded. FALSE will be returned if there was
    + *   an error.
    

    False return value in case of an error is now mentioned twice in this doc area.

  7. +++ b/core/modules/file/file.module
    @@ -1426,20 +1431,21 @@ function file_icon_map($mime_type) {
    + *   returned.  Defaults to 'file'.
    

    extra space in this line

rang501’s picture

Status: Needs work » Needs review
StatusFileSize
new16.47 KB
new4.57 KB

Thanks for the review.
The issues you mentioned, should be fixed now. I hope I got the fifth one right.

jhodgdon’s picture

Status: Needs review » Needs work

Almost! A few more things to fix still:

  1. +++ b/core/modules/file/file.module
    @@ -268,12 +272,16 @@ function file_move(FileInterface $source, $destination = NULL, $replace = FILE_E
    + *   (optional) An associative array of callback functions used to validate the
      *   file. The keys are function names and the values arrays of callback
    - *   parameters which will be passed in after the file entity. The
    - *   functions should return an array of error messages; an empty array
    + *   parameters which will be passed in after the file entity. The functions
    + *   should return an array of error messages; an empty array
      *   indicates that the file passed validation. The functions will be called in
      *   the order specified.
    + *   The hook hook_file_validate() will be invoked so other
    + *   modules can validate the new file.
    + *   The callback functions will be called in the order
    + *   specified in the array.
    

    This whole area of the documentation needs to be rewrapped.

    Documentation lines should go out to as close to 80 character lines as possible, without going over. Most of these lines are too short.

  2. +++ b/core/modules/file/file.module
    @@ -345,11 +353,11 @@ function file_validate_extensions(FileInterface $file, $extensions) {
    + *   (optional) The maximum file size in bytes.
    + *   Zero (the default) indicates that no limit should be enforced.
    

    This should also be rewrapped.

  3. +++ b/core/modules/file/file.module
    @@ -345,11 +353,11 @@ function file_validate_extensions(FileInterface $file, $extensions) {
    + *   (optional) The maximum number of bytes the user is allowed.
    + *   Zero (the default) indicates that no limit should be enforced.
    

    And this.

  4. +++ b/core/modules/file/file.module
    @@ -666,39 +677,33 @@ function file_cron() {
      * @param int $delta
    - *   Delta of the file to save or NULL to save all files. Defaults to NULL.
    + *   (optional) The delta of the file to save or NULL to save all files.
    + *   Defaults to NULL.
    

    The type here should include |null according to the text of this documentation.

  5. +++ b/core/modules/file/file.module
    @@ -666,39 +677,33 @@ function file_cron() {
    + * @return \Drupal\file\FileInterface[]|null|false
    + *   An array of file entities or a single file entity if $delta != NULL.
    + *   Each entity contains the file information if the
      *   upload succeeded or FALSE in the event of an error. Function
      *   returns NULL if no file was uploaded.
    

    The type here still does not match the words in this documentation.

    I am also not sure how an entity can "contain FALSE" -- what does this mean? This doc block still needs some attention I think. It was extensively rewritten but I think it may still not be correct.

rang501’s picture

Hopefully it's better now.

rang501’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 10: 2610202-10-docblocks-file-module-file-d8.patch, failed testing.

rang501’s picture

Status: Needs work » Needs review
jhodgdon’s picture

Status: Needs review » Needs work

Getting better!

So... I took a very careful look through this patch again. Item 5 in comment #9 is still a problem (see below), and because that still needs to be fixed, I also noted a few other things that I think would really improve this documentation:

  1. +++ b/core/modules/file/file.module
    @@ -116,20 +119,21 @@ function file_load($fid, $reset = FALSE) {
    + * @param string|null $destination
    + *   (optional) A string containing the destination that $source should be
    + *   copied to. This must be a stream wrapper URI. Defaults to NULL.
    

    This doc block is kind of problematic. How can you copy a file without a destination? Given that, it seems very odd that NULL should be allowed, unless NULL has some particular meaning -- in which case it needs to be explained what that meaning would be.

  2. +++ b/core/modules/file/file.module
    @@ -191,21 +195,21 @@ function file_copy(FileInterface $source, $destination = NULL, $replace = FILE_E
    + * @param string|null $destination
    + *   (optional) A string containing the destination that $source should be moved
    + *   to. This must be a stream wrapper URI. Defaults to NULL.
    

    Same here.

  3. +++ b/core/modules/file/file.module
    @@ -268,12 +272,13 @@ function file_move(FileInterface $source, $destination = NULL, $replace = FILE_E
    + *   the file passed validation. The hook hook_file_validate() will be invoked
    + *   so other modules can validate the new file. The callback functions will be
    + *   called in the order specified in the array.
    

    When does the hook get called -- before or after the other validation functions?

  4. +++ b/core/modules/file/file.module
    @@ -380,7 +385,7 @@ function file_validate_size(FileInterface $file, $file_limit = 0, $user_limit =
      * @return array
    - *   An array. If the file is not an image, it will contain an error message.
    + *   An array with validation errors. Empty array indicates no problems.
    

    Probably the validation errors here are strings? In which case we should say string[] not generic array for the type line.

  5. +++ b/core/modules/file/file.module
    @@ -460,20 +466,22 @@ function file_validate_image_resolution(FileInterface $file, $maximum_dimensions
    + * @param string|null $destination
    + *   (optional) A string containing the destination URI. This must be a stream
    + *   wrapper URI. If no value or NULL is provided, a randomized name will be
    + *   generated and the file will be saved using Drupal's default files scheme,
    + *   usually "public://".
    

    See, this doc block tells what happens if the input is NULL, so this one is good (unlike the first two mentioned above). For the other two, you'll need to read the function and figure out what the default behavior is for the destination for those functions.

  6. +++ b/core/modules/file/file.module
    @@ -666,39 +674,32 @@ function file_cron() {
    + * @return \Drupal\file\FileInterface[]|null|false
    + *   An array of file entities or a single file entity if $delta != NULL.
    + *   Each entity contains the file information if the upload succeeded. Function
    + *   returns FALSE if there was an error or NULL if no file was uploaded.
    

    Let me try again on this one... I apparently haven't provided enough detail in my other reviews of this doc block (such as comment #9 item 5).

    OK. Let's take a look at this in detail. The wording here (which I'm assuming is correct although I haven't looked at the function code) basically says this function can return:

    a) If $delta is NULL, you get an array of file entities (this is of type FileInterface[] -- so far, so good).

    b) If $delta is not NULL, you get a single file entity. This would be of type FileInterface, but that is NOT included in the @return type specification, so that is not so good.

    c) FALSE if there was an error... that's shown in the @return type line, so that is good. However, if you look at the documentation that this patch is replacing, it says that this FALSE value can be put into **each array element** if there is an error for that one file. So for instance if one of the files failed, you might have an array (entity, FALSE, entity). This is not reflected in either the text in this patch or in the type information on the @return line, although the unpatched documentation has this information.

    d) NULL if there are no files uploaded. This is documented and is in the type line.

    So. Given (c), I think in the @return type information, instead of FileInterface[] we need to just say it's a generic array (because each element of the array is either a FileInterface or FALSE). And then that information needs to be put back into the documentation and not lost as it is in this patch.

    So I think the type line needs to say:

    array|\Drupal\file\FileInterface|null|false

    and the documentation text needs adjustment. Right?

  7. +++ b/core/modules/file/file.module
    @@ -1151,7 +1152,7 @@ function file_managed_file_submit($form, FormStateInterface $form_state) {
    + * @return array|false
      *   An array of file entities for each file that was saved, keyed by its file
      *   ID, or FALSE if no files were saved.
    

    Here instead of generic array we should be specifying something like FileInterface[] right? Or can each entry be FALSE like in the above function (in which case generic "array" would be correct and the text would need to be updated)? I am not sure -- you need to look at the function body and see how it works. Either the type line or the documentation below it needs updating, and I'm not sure which.

  8. +++ b/core/modules/file/file.module
    @@ -1516,9 +1517,10 @@ function file_get_file_references(FileInterface $file, FieldDefinitionInterface
      * @param int $choice
    - *   integer Status code.
    + *   (optional) An integer status code. Defaults to NULL.
    

    If this defaults to NULL then its type line should be
    int|null
    right?

rang501’s picture

Status: Needs work » Needs review
StatusFileSize
new17.13 KB
new3.8 KB

Thanks for the review!
About the points 1, 2 and 5 - first two functions require $destination to be a valid URI, if it's null, then error will be returned (default value seems to be wrong). The third one is correct, $destination will default to default file schema if null.
6 and 7. Hopefully got that right this time.

jhodgdon’s picture

Status: Needs review » Needs work

I agree with your analysis in #15.

So... For this review, in contrast to the others, besides just looking at the patch I looked at the code for all of these functions in file.module to see if the documentation was actually accurate.

It's mostly good, but we still need to fix a few things:

  1. +++ b/core/modules/file/file.module
    @@ -116,20 +119,21 @@ function file_load($fid, $reset = FALSE) {
    + * @param string|null $destination
    

    See below... [this is docs for file_copy()]

  2. +++ b/core/modules/file/file.module
    @@ -192,20 +196,20 @@ function file_copy(FileInterface $source, $destination = NULL, $replace = FILE_E
      * @param string $destination
    

    So in file_move() [here], the @param line says "string", but in file_copy() [above], the @param line says "string|null".

    We should probably do one or the other for both of these functions, because they behave the same way, rather than having them documented differently. ... Which way? Hm...

    Technically, the functions have a NULL default value so they must permit you to pass in NULL; however, they will both return an error if you do pass in NULL, so ... I guess my inclination is not to document that it can be NULL because for all *practical* purposes, it cannot be NULL if you want the functions to actually work, which presumably you do.

    So let's make both of these say "string" only.

  3. +++ b/core/modules/file/file.module
    @@ -268,12 +272,13 @@ function file_move(FileInterface $source, $destination = NULL, $replace = FILE_E
    + *   the file passed validation. The callback functions will be called in the
    + *   order specified in the array, then the hook hook_file_validate()
    + *   will be invoked so other modules can validate the new file.
    

    Nice. Verified it's correct also. :)

  4. +++ b/core/modules/file/file.module
    @@ -379,8 +384,8 @@ function file_validate_size(FileInterface $file, $file_limit = 0, $user_limit =
    - * @return array
    - *   An array. If the file is not an image, it will contain an error message.
    + * @return string[]
    + *   An array with validation errors. Empty array indicates no problems.
    

    The text here for the return value docs is less specific now than it was in the original. Also, this one validation function @return doc was changed where the others were left the same... shouldn't they all be consistent? I think I would recommend a unified style for all of the "run one type of validation" functions. Maybe something like this:

    An empty array if the file is a valid image, and an array containing an error message if it is not.

    Or something similar? This would apply to all of the file_validate_* functions, like file_validate_is_image(), file_validate_name_length(), etc.

    But I think file_validate() itself we should leave as it is in the patch, because that is returning an array of all error messages from a bunch of validation functions.

    Thoughts?

  5. +++ b/core/modules/file/file.module
    @@ -379,8 +384,8 @@ function file_validate_size(FileInterface $file, $file_limit = 0, $user_limit =
    + * @return string[]
    

    Let's make this one just "array" instead of string[] so that all the validator return values are documented the same way?

  6. +++ b/core/modules/file/file.module
    @@ -666,39 +674,32 @@ function file_cron() {
    + * @param null|int $delta
    + *   (optional) The delta of the file to save or NULL to save all files.
    + *   Defaults to NULL.
    

    So, this is interesting. Reading through the code for this function file_save_upload(), it appears to me that even if $delta is FALSE, actually all of the uploaded files that the user submitted are saved.

    All that $delta seems to affect is the return value.

    Right? Let's document it that way then, something like "The delta of the file to return..."

  7. +++ b/core/modules/file/file.module
    @@ -1151,9 +1152,11 @@ function file_managed_file_submit($form, FormStateInterface $form_state) {
      *   An array of file entities for each file that was saved, keyed by its file
    - *   ID, or FALSE if no files were saved.
    + *   ID. Each array element contains a file entity if saved successfully or
    + *   FALSE if there was an error. Function returns FALSE if upload directory
    + *   could not be created or no files were uploaded.
      */
     function file_managed_file_save_upload($element, FormStateInterface $form_state) {
    

    For this function file_managed_file_save_upload(), I looked at the code carefully...

    So, unlike the unmanaged file_save_upload() function, in this one after getting the return value from file_save_upload(), it uses array_filter() to filter out the empty entries, so this statement saying the array can contain FALSE if there was an error with that particular file is incorrect. What really happens is that that one file will get tacitly omitted from the array. So the array contains only file entities, and only the ones that are successfully uploaded and validated.

  8. +++ b/core/modules/file/file.module
    @@ -1515,10 +1518,11 @@ function file_get_file_references(FileInterface $file, FieldDefinitionInterface
    + *   (optional) An integer status code. Defaults to NULL.
    

    Should document what NULL means here... see below...

  9. +++ b/core/modules/file/file.module
    @@ -1515,10 +1518,11 @@ function file_get_file_references(FileInterface $file, FieldDefinitionInterface
      * @return string
    - *   string Text-represented file status.
    + *   The file status as a string.
      */
     function _views_file_status($choice = NULL) {
    

    This isn't really accurate. First off, it's not really a string, since it's the return value of t(), which isn't a string any more but \Drupal\Core\StringTranslation\TranslatableMarkup.

    Second, if $choice is omitted, this function is actually returning an array of both the known options.

    Actually, the only call to this function in Core doesn't pass in $choice, so this is the more common (or only, really) return value... and why it's a separate function instead of just being part of \Drupal\file\Plugin\views\filter\Status::getValueOptions() in the first place is beyond me... but fixing that is beyond the scope of this issue.

    Meanwhile, we should fix the documentation so that it reflects the actual return value here, which is either a single \Drupal\Core\StringTranslation\TranslatableMarkup or an array of them.

rang501’s picture

Status: Needs work » Needs review
StatusFileSize
new18.49 KB
new4.18 KB

Thanks for the detailed review again!
I have made another set of changes according your suggestions.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! I think this is ready to go.

Issue scope note: I think this issue is well-scoped. It is fixing a bunch of doc blocks so that they are more accurate representations of the code. They are all in one file, and all related to file uploads. It's not just coding standards fixes, and couldn't be "sniffed" or fixed with a script, and the fixes aren't really related to fixes that we might need to make elsewhere. So it's one stand-alone issue that fixes up several related functions and I think it's well scoped.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.1.x and cherry-picked to 8.0.x. Thanks!

  • catch committed 1c3c461 on 8.1.x
    Issue #2610202 by rang501, Lars Toomre: Docblock fixes for file.module...

  • catch committed 5a9a4cd on 8.0.x
    Issue #2610202 by rang501, Lars Toomre: Docblock fixes for file.module...

Status: Fixed » Closed (fixed)

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