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

  1. Gogowitsch
  2. joachim
  3. {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

  1. Create this issue in the Coding Standards queue, using the defined template
  2. List three supporters
  3. Create a Change Record
  4. Review by the Coding Standards Committee
  5. Coding Standards Committee takes action as required
  6. Tagged with 'Needs documentation edits' if Core is not affected
  7. Discussed by the Core Committer Committee, if it impacts Drupal Core
  8. Documentation updates
    1. Edit all pages
    2. Publish change record
    3. Remove 'Needs documentation edits' tag
  9. 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

jhodgdon’s picture

Project: Drupal core » Drupal Technical Working Group
Version: 8.0.x-dev »
Component: documentation » Documentation

Coding standards issues are now supposed to be discussed and decided upon by the Technical Working Group.

tim.plunkett’s picture

@internal is used by phpDocumentor and is proposed for PHP-FIG
https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpd...

tizzo’s picture

Project: Drupal Technical Working Group » Coding Standards
Component: Documentation » Coding Standards
joachim’s picture

Title: [policy, then patch] Clearly mark functions that should not be used by contrib » Clearly mark functions that should not be used outside of core
Issue tags: -Coding standards, -DX, -DX (Developer Experience)
joachim’s picture

Since this was posted, the @internal tag has started being used in core: I count 32 uses (and rising, I've seen it patches recently).

jhodgdon’s picture

I 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.

joachim’s picture

Title: Clearly mark functions that should not be used outside of core » Clearly mark functions that should not be used outside of their project
Issue summary: View changes

Updated summary.

gogowitsch’s picture

Status: Active » Needs review

Function naming conventions are described here: https://www.drupal.org/docs/develop/standards/coding-standards#s-functio...

I propose to add these sentences:

"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, please use the @internal annotation."

This way, we also make it more obvious that the section on naming functions is not about class methods, which should follow lowerCamel naming.

joachim’s picture

Status: Needs review » Reviewed & tested by the community

Proposed wording LGTM.

larowlan’s picture

Issue summary: View changes

Adding text to IS

larowlan’s picture

quietone’s picture

Issue summary: View changes

Update to new template

quietone’s picture

Status: Reviewed & tested by the community » Needs review

In 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.