Problem/Motivation

A number of issues have been rejected because a @param type has been too specific, for example @param array<string, string|false>.

Instead of maintaining a definition of what is a valid param type we should lean on the wider community and "get off the island".

I propose that anything included in https://phpstan.org/writing-php-code/phpdoc-types should be allowed.

Benefits

Allowing newer param types to be used without waiting for coding standards process

Three supporters required

  1. https://www.drupal.org/u/mondrake 2024-08-15
  2. https://www.drupal.org/u/dpi 2024-08-15
  3. https://www.drupal.org/u//larowlan 2024-09-11
  4. https://www.drupal.org/u/kimpepper 2024-09-11
  5. https://www.drupal.org/u/feyp 2024-09-12
  6. https://www.drupal.org/u/donquixote 2025-05-05

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. Indicating data types in documentation

Current text

Certain tags can include data types (@param, @return, etc.).

Syntax examples:

int
string|bool
\Drupal\Core\Database\StatementInterface
int[]
\Drupal\node\NodeInterface[]
$this
static 

Syntax notes:

  • Data types can be primitive types (int, string, etc.), complex PHP built-in types (array, object, resource), or PHP classes.
  • If only one data type is possible, just use its name.
  • If multiple types are possible, separate them by a vertical bar ("|").
  • For an array where the values are all of one particular class/interface type follow the class name by [].
  • Indicate arrays of built-in PHP types by following the type name by [] (example: int[], string[], etc.).
  • When you are returning the main class object ($this), use @return $this.
  • When creating a new instance of the same class, use @return static.

Drupal standards notes:

  • Use interface names if possible, or the most general possible class, in place of a specific class.
  • Always prefix types with the fully-qualified namespace for classes and interfaces (beginning with a backslash). If the class/interface is in the global namespace, prefix by a backslash.
  • You may omit the description if using @return $this or @return static.
  • For the PHP built-in types, use the following names:
    • array (NOT "Array"). However, the [] syntax (see above) is preferred if appropriate. That is, if you know what the array contains, and all the elements are a particular primitive type, indicate that with string[] or \My\Class\Name[] etc. instead of using the generic "array" type.
    • bool (NOT "boolean" or "Boolean"). If only TRUE or only FALSE is a possible value, rather than either one being possible, use true or false instead of bool.
    • false (NOT "FALSE", see bool)
    • float
    • int (NOT "integer")
    • null (NOT "NULL")
    • object (NOT "stdClass")
    • resource
    • string
    • true (NOT "TRUE", see bool)
Proposed text

Tags that indicate the type of a variable, of a parameter, or of the return value of a function/method (@var, @param, @return) are compliant with the PHPDoc Type syntax as documented by PHPStan. This allows advanced static analysis of Drupal codebase.

The following additional Drupal standards are applicable:

  • Use interface names if possible, or the most general possible class, in place of a specific class.
  • Always prefix types with the fully-qualified namespace for classes and interfaces (beginning with a backslash). If the class/interface is in the global namespace, prefix by a backslash.
  • You may omit the description if using @return $this or @return static.
  • Spell the following types accordingly
    • array (NOT "Array").
    • bool (NOT "boolean" or "Boolean"). If only TRUE or only FALSE is a possible value, rather than either one being possible, use true or false instead of bool.
    • true (NOT "TRUE", see bool)
    • false (NOT "FALSE", see bool)
    • int (NOT "integer")
    • null (NOT "NULL")
    • object (NOT "stdClass")

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. Add supporters
  3. Create a Change Record
  4. Review by the Coding Standards Committee
  5. Coding Standards Committee takes action as required
  6. Discussed by the Core Committer Committee, if it impacts Drupal Core
  7. Final review by Coding Standards Committee
  8. Documentation updates
    1. Edit all pages</li>
    2. Publish change record
    3. Remove 'Needs documentation edits' tag
  9. If applicable, create follow-up issues for PHPCS rules/sniffs changes

For a full explanation of these steps see the Coding Standards project page

Comments

mstrelan created an issue. See original summary.

dpi’s picture

All for this, I find it frustrating that the wider PHP community has adopted in Stan / Psalm / others, but we're slowed by approving things piecemeal.

Recently the PHPDoc maintainer said something relevant to this topic:

https://phpc.social/@jaapio/112797821284066042

I'm going to try to reboot the working group for @phpfig's psr-5. Goal is to write down the standards we are already using in @phpstan @phpdoc @phpstorm, @psalm and many others.
I'm looking for people that will help me once in a while to check if I do not write down things that will break these amazing tools.

Some projects have been left behind by recent innovation.

We should just allow anything, and let our approved static analyser (PHPStan) validate it. Its not really within the domain of Coding Standards to do validation.

See also, the discussion at #3309010: Support PHPDoc Types in @param @var @return annotations

mondrake’s picture

Kind of duplicate of #3309010: Support PHPDoc Types in @param @var @return annotations.

Badly,badly needed if Drupal will ever try to get to PHPStan level 6+.

mondrake’s picture

Issue summary: View changes

+1, added support

dpi’s picture

Issue summary: View changes
larowlan’s picture

Issue summary: View changes

Added support, Next step - can we get some proposed text before/after in the IS please? thanks!

mstrelan’s picture

Issue summary: View changes
andypost’s picture

IS said

If multiple types are possible, separate them by a vertical bar ("|").

but there's DNF types since PHP 8.2 so it needs rewording

mstrelan’s picture

@andypost that's the "before" section, we need to rewrite the "after" section. I'm proposing that we leave it mostly empty, and just linking off to phpstan docs.

kim.pepper’s picture

Issue summary: View changes

Jumping in to give my +1 to this.

mondrake’s picture

Tried a stab at the to-be text.

Note that

Always prefix types with the fully-qualified namespace for classes and interfaces (beginning with a backslash). If the class/interface is in the global namespace, prefix by a backslash.

is not what other projects do, and #3360160: Stop using FQCN in PHPDoc annotations aims at removing it.

mondrake’s picture

Issue summary: View changes
dww’s picture

Agreed with #3 -- isn't this fully duplicate with #3309010: Support PHPDoc Types in @param @var @return annotations? Can we close this and focus efforts in one place?

mondrake’s picture

Honestly now I prefer the turn that this issue has taken, and would rather close #3309010: Support PHPDoc Types in @param @var @return annotations as duplicate at this stage.

feyp’s picture

Issue summary: View changes

Looks like we already have enough supporters, but fwiw +1 on this one.

mstrelan’s picture

Status: Active » Needs review

Added a CR

quietone’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Just pointing out that the next text is using 'must' which is not the more friendly tone that our coding standards use.

Updating issue summary.

Setting to RTBC so it can get on the agenda for a CS meeting.

dpi’s picture

I don't think there is any tone implied. It is merely communicating clearly, similar or according to RFC2119.

It would make sense for a coding standard to adopt clear language.

quietone’s picture

@dpi, I agree on having clarity but we should keep in mind that the Drupal Standards do not follow RFC2119. See #1795750: Revise coding standards to use IETF RFC 2119 standards.

borisson_’s picture

Issue summary: View changes

Changed the wording.

borisson_’s picture

Change record looks good.

longwave’s picture

+1 this change and the change record both look good to me

niklan’s picture

+1 for this change. PHPStan annotations allow doing more explicit return types and detecting more bugs and issues before it goes live.

quietone’s picture

This was discussed at the Coding Standards Meeting Tuesday 2025-02-26 0900 UTC and there were no objections.

quietone’s picture

Issue summary: View changes

This was in the meeting 26 March and there were no objections.

This was included in the last core committer meeting and there were no objections to this change.

quietone’s picture

Updating credit.

quietone’s picture

Sniffs were not discussed here. What, if any, changes are required in Coder?

mstrelan’s picture

Sniffs were not discussed here. What, if any, changes are required in Coder?

We don't need to change anything to allow the proposed changes, only if we want to restrict them.

I tested this by updating the @param doc on a constructor param to use a variety of different types listed in the phpstan docs. I also started putting random strings in, and they mostly passed, unless I used any of @#,.. For example, @param zDSA[]|<>D-&* $date_formatter is acceptable, but @param zDSA[]|<>D-&*# $date_formatter is not.

donquixote’s picture

object (NOT "stdClass")

I would disagree with that point.
Instead:

Use `object` for values that can be instances of any class.
Use `\stdClass` for values that can only be instances of stdClass.

In addition, I would like to see a sentence saying that we should always prefer the most specific type that the context allows.
E.g. `array` or `list` instead of `string[]`.

An exception can be very complex arrays like render arrays, or theme registry items.
Especially when there could be more keys added by modules.
Here the doc would be like `array{'preprocess functions': list, ...}` which is just too much.

donquixote’s picture

Issue summary: View changes

fwiw I support this move in general, but would not mind to see it be even stronger as mentioned above :)

mstrelan’s picture

#29/#30 seem like unrelated follow ups to me. The object/stdClass example is in the original text and unchanged in the proposed text.

donquixote’s picture

#29/#30 seem like unrelated follow ups to me. The object/stdClass example is in the original text and unchanged in the proposed text.

Ok to me if we do this in follow-ups.

borisson_’s picture

quietone’s picture

Issue summary: View changes

I edit the documentation, so that needs to be reviewed. If the changes are correct then the reviewer can publish the change record.

This change removed all the examples, so we should add some back.

penyaskito’s picture

Verified the doc edits are as described in the issue summary.

penyaskito’s picture

Status: Reviewed & tested by the community » Fixed

Published the change record.

Created #3525397: Provide examples for phpdoc allowed types as a follow-up for evaluating if we need to add examples back.

quietone’s picture

Status: Fixed » Closed (fixed)

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