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
- https://www.drupal.org/u/mondrake 2024-08-15
- https://www.drupal.org/u/dpi 2024-08-15
- https://www.drupal.org/u//larowlan 2024-09-11
- https://www.drupal.org/u/kimpepper 2024-09-11
- https://www.drupal.org/u/feyp 2024-09-12
- 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 staticSyntax 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 $thisor@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 $thisor@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
Create this issue in the Coding Standards queue, using the defined templateAdd supportersCreate a Change RecordReview by the Coding Standards CommitteeCoding Standards Committee takes action as requiredDiscussed by the Core Committer Committee, if it impacts Drupal CoreFinal review by Coding Standards Committee- Documentation updates
Edit all pages</li>- Publish change record
Remove 'Needs documentation edits' tag
- 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
Comment #2
dpiAll 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
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
Comment #3
mondrakeKind 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+.
Comment #4
mondrake+1, added support
Comment #5
dpiComment #6
larowlanAdded support, Next step - can we get some proposed text before/after in the IS please? thanks!
Comment #7
mstrelan commentedComment #8
andypostIS said
but there's DNF types since PHP 8.2 so it needs rewording
Comment #9
mstrelan commented@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.
Comment #10
kim.pepperJumping in to give my +1 to this.
Comment #11
mondrakeTried a stab at the to-be text.
Note that
is not what other projects do, and #3360160: Stop using FQCN in PHPDoc annotations aims at removing it.
Comment #12
mondrakeComment #13
dwwAgreed 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?
Comment #14
mondrakeHonestly 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.
Comment #15
feyp commentedLooks like we already have enough supporters, but fwiw +1 on this one.
Comment #16
mstrelan commentedAdded a CR
Comment #17
quietone commentedJust 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.
Comment #18
dpiI 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.
Comment #19
quietone commented@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.
Comment #20
borisson_Changed the wording.
Comment #21
borisson_Change record looks good.
Comment #22
longwave+1 this change and the change record both look good to me
Comment #23
niklan+1 for this change. PHPStan annotations allow doing more explicit return types and detecting more bugs and issues before it goes live.
Comment #24
quietone commentedThis was discussed at the Coding Standards Meeting Tuesday 2025-02-26 0900 UTC and there were no objections.
Comment #25
quietone commentedThis 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.
Comment #26
quietone commentedUpdating credit.
Comment #27
quietone commentedSniffs were not discussed here. What, if any, changes are required in Coder?
Comment #28
mstrelan commentedWe don't need to change anything to allow the proposed changes, only if we want to restrict them.
I tested this by updating the
@paramdoc 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_formatteris acceptable, but@param zDSA[]|<>D-&*# $date_formatteris not.Comment #29
donquixote commentedI would disagree with that point.
Instead:
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.
Comment #30
donquixote commentedfwiw I support this move in general, but would not mind to see it be even stronger as mentioned above :)
Comment #31
mstrelan commented#29/#30 seem like unrelated follow ups to me. The object/stdClass example is in the original text and unchanged in the proposed text.
Comment #32
donquixote commentedOk to me if we do this in follow-ups.
Comment #33
borisson_Have a look at #3315957: Allow stdClass instead of object in tags that include a data type for object vs stdClass
Comment #34
quietone commentedI 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.
Comment #35
penyaskitoVerified the doc edits are as described in the issue summary.
Comment #36
penyaskitoPublished the change record.
Created #3525397: Provide examples for phpdoc allowed types as a follow-up for evaluating if we need to add examples back.
Comment #37
quietone commented