Problem/Motivation
Both core and contrib have code that is intended for internal use only, and should not be called from other contrib modules or custom code. This can be functions, interfaces, or certain methods.
On D7, this was indicated for functions by prefixing the function name with an underscore. That's great if you know what it means, but isn't at all clear if you don't know the convention.
We should have an indication in the docblocks for these functions or interfaces. Currently we have either:
- @internal
- Nothing (most functions)
- "(internal use only)" in the one line description (e.g.
_theme) - @deprecated (e.g.
_drupal_add_js)
Of these three I think "(internal use only)" is the best because it's most visible, but it's still not totally clear what qualifies as internal and uses characters in the one-line description. @deprecated is just plain wrong - the functions can still be used, but only by internal code.
Benefits
If we adopted this change, the Drupal Project would benefit by ...
Three supporters required
- Gogowitsch
- joachim
- {link to user}
Proposed changes
Provide all proposed changes to the Drupal Coding standards. Give a link to each section that will be changed, and show the current text and proposed text as in the following layout:
1. documentation on function declarations
Current text
function funstuff_system($field) { $system["description"] = t("This module inserts funny text into posts randomly."); return $system[$field]; }Arguments with default values go at the end of the argument list. Always attempt to return a meaningful value from a function if one is appropriate.
Anonymous functions should have a space between "function" and its parameters, as in the following example:
array_map(function ($item) use ($id) { return $item[$id]; }, $items);
Proposed text
function funstuff_system($field) { $system["description"] = t("This module inserts funny text into posts randomly."); return $system[$field]; }Arguments with default values go at the end of the argument list. Always attempt to return a meaningful value from a function if one is appropriate.
Anonymous functions should have a space between "function" and its parameters, as in the following example:
array_map(function ($item) use ($id) { return $item[$id]; }, $items);A function outside of classes, interfaces and traits cannot be marked as private. Instead, you can mark functions that are not to be called from outside the project by starting their name with an underscore. In addition, use the @internal annotation.
/** * @internal */ function _private_funstuff_system($field) { $system["description"] = t("This module inserts funny text into posts randomly."); return $system[$field]; }
2. Repeat the above for each page or sub-page that needs to be changed.
Remaining tasks
Create this issue in the Coding Standards queue, using the defined template- List three supporters
- Create a Change Record
- Review by the Coding Standards Committee
- Coding Standards Committee takes action as required
- Tagged with 'Needs documentation edits' if Core is not affected
- Discussed by the Core Committer Committee, if it impacts Drupal Core
- Documentation updates
- Edit all pages
- Publish change record
- Remove 'Needs documentation edits' tag
- If applicable, create follow-up issues for PHPCS rules/sniffs changes
For a fuller explanation of these steps see the Coding Standards project page
Comments
Comment #1
jhodgdonCoding standards issues are now supposed to be discussed and decided upon by the Technical Working Group.
Comment #2
tim.plunkett@internal is used by phpDocumentor and is proposed for PHP-FIG
https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpd...
Comment #3
tizzo commentedMoving this issue to the Coding Standards queue per the new workflow defined in #2428153: Create and document a process for updating coding standards.
Comment #4
joachim commentedClosing #2382873: [policy] Formalize coding standard for functions not intended for public use as a duplicate.
Comment #5
joachim commentedSince this was posted, the @internal tag has started being used in core: I count 32 uses (and rising, I've seen it patches recently).
Comment #6
jhodgdonI think this issue / coding standards proposal should be slightly amended. @internal should be usable by contrib projects as well, to indicate "this is internal to this project and is subject to change without notice". As written, the issue title and summary only refer to core.
Comment #7
joachim commentedUpdated summary.
Comment #8
gogowitsch commentedFunction naming conventions are described here: https://www.drupal.org/docs/develop/standards/coding-standards#s-functio...
I propose to add these sentences:
This way, we also make it more obvious that the section on naming functions is not about class methods, which should follow lowerCamel naming.
Comment #9
joachim commentedProposed wording LGTM.
Comment #10
larowlanAdding text to IS
Comment #11
larowlanDraft CR https://www.drupal.org/node/3396761
Comment #12
quietone commentedUpdate to new template
Comment #13
quietone commentedIn the meeting, catch questioned if this is needed, saying that they prefer that private functions were discouraged. I am setting this to needs review to allow discussion of that.
The meeting minutes will be in #3393077: Coding Standards Meeting Wednesday 2023-10-25 0900 UTC.