Problem/Motivation
Note: This issue refers to generic functions written as callbacks for PHP language features like uasort()
, array_walk()
, etc. This is not related to #1250500: [policy adopted; patch done] Find a (standard) way to document callback signatures.
From #1315992: No standard for documenting menu callbacks. Currently, documented summaries for general callbacks used with PHP core language features are very inconsistent. Some reference the function that registers the callback; others do not. Some list the actual function used with the callback; others do not. Many do not follow the standards at http://drupal.org/node/1354#functions. Examples follow.
Sorting functions
Function used by uasort to sort the array structures returned by drupal_add_css() and drupal_add_js().
Function used by uasort to sort structured arrays by weight.
Array sorting callback; sorts elements by title.
unction used by uasort to sort structured arrays by weight, without the property weight prefix
Array sorting callback; sorts elements by 'title' key.
Callbacks for array_walk()
or array_map()
Helper function to strip slashes from $_FILES skipping over the tmp_name keys since PHP generates single backslashes for file paths on Windows systems.
- (blank)
Shutdown functions
Shutdown function for cron cleanup.
Shutdown function; store the current batch data for the next request.
Proposed resolution
Provide a brief standard that:
- Is consistent with #1315992: No standard for documenting menu callbacks
- Gives an example of a proper one-line summary
- Recommends an @see to the function where the callback is used.
An revised proposal for docmenting a standard is in #6.
Comments
Comment #1
xjmExamples of the proposed standard:
In this final example, perhaps no documentation of parameter or return values is needed, because these are a part of the PHP standard for sorting functions.
Edited like 5 times because I copied from the wrong branch. :P
Comment #2
xjmFirst pass at the documentation follows. Edited to remove some redundancy.
Generic callback functions used with basic PHP language features
Generic callback functions used with PHP language features like usort() or array_walk() are documented as follows.
General standard for callbacks
Notes:
Callback
followed by a colon and a space.Callback:
, include a standard function summary that is capitalized and begins with a third-person present indicative verb. Total line length: 80 characters, ending in "."@see
line or lines referencing the specific function(s) that use this callback.Array sorting callbacks
Notes:
Sorting callback
followed by a colon and a space.Shutdown functions
Notes:
Shutdown function
followed by a colon and a space.register_shutdown_function().
Include an @see line referencingdrupal_register_shutdown_function().
@see
line or lines referencing the function where this shutdown function is registered.Comment #2.0
xjmUpdated issue summary.
Comment #3
xjmComment #3.0
xjm.
Comment #3.1
xjmUpdated issue summary.
Comment #4
jhodgdonI would like to see something in the standard that makes it clear in the first line what function the callback is for. Maybe
The menu callbacks from the other issue were a specific case where "xyz callback" is a specific Drupalism. These are not.
Comment #5
xjmfoo()
being (e.g.)uasort()
?Comment #6
xjmHow about this? Edit: I took out the "Sorting" part since that is clear from the rest of the line when we add the reference to
uasort()
.Generic callback functions used with basic PHP language features
Generic callback functions used with PHP language features like usort() or array_walk() are documented as follows.
General standard for callbacks
Notes:
Callback for function()
, wherefunction()
is a base PHP function that accepts a callback as a parameter. This prefix should be followed by a colon and a space.@see
line or lines referencing the specific function(s) that register this callback.Array sorting callbacks
Notes:
Shutdown functions
Notes:
Shutdown function
followed by a colon and a space.register_shutdown_function().
Include an @see line referencingdrupal_register_shutdown_function().
@see
line or lines referencing the function where this shutdown function is registered.Comment #6.0
xjmUpdated issue summary.
Comment #7
xjmJust had a thought re: sorting callbacks; the return value does not need to be documented, but maybe the parameters actually do. They could be any data type, and their structure matters. (E.g. element_sort() requires an associative array that may include a
'#weight'
key). However, it can be said that both parameters should be the same data type. So how about this for sorting callbacks:Notes:
Comment #8
jhodgdonCan we make a generic standard that would work for any PHP function callback, rather than having to list every type we can think of?
Comment #9
xjmIsn't it that already? I just mentioned the specific point about sorting callbacks because I saw a lot of dumb (or entirely missing) docblocks for them. We could leave that off and just use the first bit, if you think that's more helpful.
Comment #10
jhodgdonThe proposal in #6 just seems overly long, and I don't know that we need all 3 sections.
Comment #11
xjmHow about if we keep the example code, and just remove some of the verbosity, like:
Generic callback functions used with basic PHP language features
Generic callback functions used with PHP language features like usort() or array_walk() are documented as follows.
Examples
Notes:
Callback for function():
, wherefunction()
is a base PHP function that accepts a callback as a parameter.@see
line or lines referencing the specific function(s) that register this callback.(Edited a bit to remove unnecessary stuff.)
Comment #12
xjmAlso, I'm not wild about the text of the h3 and I'm not sure the word "generic" even has any meaning here; do you think "Callback functions" would be sufficient? Or too easily confused with our standardized drupal callbacks?
Comment #13
jhodgdonRE #11 - better. Still not sure we need two examples -- not sure what the 2nd one adds that the 1st one doesn't illustrate? Or maybe the 2nd one is better, but then do we need the 1st one?
RE #12 - Wouldn't these standards be good for Drupal function callbacks too?
Comment #14
xjmI guess I wanted to show an example of a sorting callback, and to use actual examples from core, but I haven't come across one that has an @see. I'll look a bit more and see if there's an example that would be better.
And yeah, I think you are right about just using one set of standards, especially since we follow the same pattern as we used for the menu callbacks. My only concern is that our standard for FAPI callbacks is different, but maybe we could just add a note indicating there are special standards for FAPI, similar to what's already elsewhere in the doc. So:
Callback functions
The general standard for documenting callback functions follows. Note that there are specific standards for hook implementations, menu callbacks, and form callbacks.
Notes:
Callback for function():
, wherefunction()
is a function that accepts a callback as a parameter.@see
line or lines referencing the specific function(s) that register this callback.Comment #15
xjmComment #16
xjmsystem_sort_themes() looks promising.
Comment #17
xjmI think this is probably a lot better. My only concern is that I'm noticing having the function name in the summary makes the 80 character limit feel really tight, so do we want to put it on a separate line like we did with menu paths?
Callback functions
The general standard for documenting callback functions follows. Note that there are specific standards for hook implementations, menu callbacks, and form callbacks.
Notes:
Callback:
.Used for function()
, wherefunction()
is a function that accepts a callback as a parameter (e.g.,array_walk()
,uasort()
, etc.).@see
line or lines referencing the specific function(s) that register this callback.(Edited)
Comment #18
xjmComment #19
jhodgdonI guess #17 is OK... maybe instead of "Callback for foo():" we could say "foo() callback:", which would save 4 characters and get the function back in the first line?
Comment #20
xjmHm, that would help a bit; but I'm concerned if we extend the pattern to Drupal functions as well, we'd have to cope with things like:
my_long_module_name_function_that_takes_a_callback() callback: Oops, character limit.
There's definitely value in having it in the summary though, so hmm. Not sure which is better.
Comment #21
jhodgdonYeah... I'm just not sure that "Callback:" is all that useful without it telling what the callback is for. Maybe the first line could just describe what it does, and the "Uses" line could be instead:
Callback for foo() within bar().
As in:
Callback for array_walk() within drupal_whatever_whatever().
Then we could also omit the @see at the bottom, and it would be clear where it's really being used?
Comment #22
xjmAh, I like that idea. (Of course, this means rerolling includes A-C again.) :P
Revised proposal follows. The second bullet could probably be cleaned up. Edit: fixed it a little, but maybe it needs more.
Callback functions
The general standard for documenting callback functions follows. Note that there are specific standards for hook implementations, menu callbacks, and form callbacks.
Notes:
Callback for function_a() within function_b()
, wherefunction_a()
is the API or base PHP function that accepts a callback as a parameter (e.g.uasort()
,array_walk()
, etc.); andfunction_b()
is the API function that callsfunction_a()
with this function as a callback parameter.Comment #23
jhodgdon#22 looks good to me. We should probably also include the function signature in the example docblock, and of course remove that extra space.
Yeah, that second bullet... How about this:
* After the summary line (and a blank line), include a line that states what PHP or Drupal function this is a callback for (e.g., uasort(), array_walk(), etc.), and also what Drupal function is calling that function.
ugh. I don't like that either... Hmmm...
* The "Callback for..." line should tell which PHP/Drupal function directly takes this callback as an argument, and also which Drupal API function calls that function. If multiple API functions call it, format as a list.
?
Anyone other than xjm and me want to comment on this proposed standard? Oops, just realized this issue wasn't tagged.
Comment #24
jhodgdonHere's another thought.
If we adopt #22, then this is really not a different standard for "callbacks" than other functions. So we could probably just add a note in the generic functions doc that if the function is a callback, add this additional line saying "Callback for ...". Thoughts?
Comment #25
xjmAh, it would be nice not to have to add an additional section. On the other hand, an example of exactly what the line should look like is instructive. Would it break from the pattern used on the page too much to do something like the following? Edit: As a subheader under the general standards.
Callbacks
For callbacks, add a blank line after the summary, followed by a line that identifies the callback's purpose. Use the following format:
This line should indicate which PHP or Drupal function directly takes this callback as an argument, and also which Drupal API function calls that function. If multiple API functions call it, format as a list.
Note that there are specific standards for hook implementations, menu callbacks, and form callbacks.
Comment #26
jhodgdonOK. Since this is not a standards change any more (phew! less bikeshedding is needed), I will go ahead and update the page. Suggestions welcome; I can always edit it again and tweak.
http://drupal.org/node/1354#functions
I also added some sub-headings to that section to make it hopefully all a little bit more clear.
For now, I'm marking this fixed. Feel free to offer suggestions and mark "needs work" if I've done something silly. :)
Comment #27.0
(not verified) CreditAttribution: commentedUpdated issue summary.