Problem/Motivation
When an object variable or a parameter or a returned value is type-hinted, the current policy requires the FQCN of the object to be used.
For example,
* @param array|\Drupal\Component\HtmlAttribute\HtmlAttributeCollection $attributes
* An optional array or HtmlAttributeCollection object of link attributes.
However, since the introduction of union types in PHP 8, readibility of such annotations got worse when multiple classes are allowed for a single object.
For example,
* @param \Drupal\Component\Render\MarkupInterface|\Drupal\Component\HtmlAttribute\HtmlAttributeValueBase|scalar|array<scalar>|null $value
* The value to set.
Other projects just annotate the unqualified class name, and rely on the use imports at the top of each file to fully qualify the classes when needed. PHPStan supports this model natively (and IDEs too??).
Allow using the class name only in the annotations. If the class name is not present in the same namespace of the class where the annotation is present, a matching use import should be present to allow matching the referred class existance.
Given the second example above, this will become
namespace Drupal\Component\HtmlAttribute;
use Drupal\Component\Render\MarkupInterface;
...
* @param MarkupInterface|HtmlAttributeValueBase|scalar|array<scalar>|null $value
* The value to set.
Benefits
If we adopted this change, the Drupal Project would benefit by having shorter and more readable PHPDoc annotations, which also makes it easier to adopt more complex PHPStan capabilities.
It also will make the PHPDoc annotations equal to the actual inlined type hints (as long as iterables are not involved, which is a different case). This will help removing unnecessary @var annotations from the code base.
Three supporters required
- https://www.drupal.org/u/Kingdutch (2024-04-10)
- https://www.drupal.org/u/borisson_ (2024-04-10)
- https://www.drupal.org/u/bbrala (2024-04-10)
- https://www.drupal.org/u/dww (2024-04-10)
Proposed changes
Create an MR with all proposed changes.
Remaining tasks
Create this issue in the Coding Standards queue, using the defined templateAdd supportersFinish fleshing out the proposed standards changes- Create a Change Record
Review by the Coding Standards CommitteeCoding Standards Committee takes action as required- Discussed by the Core Committer Committee, if it impacts Drupal Core
- Final review by Coding Standards Committee
- 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 full explanation of these steps see the Coding Standards project page
Issue fork coding_standards-3360160
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
mondrakeOpened #3360163: Stop using FQCN in @param @var @return annotations in Coder's issue queue to match this.
Comment #3
klausiI think I agree with this proposal. We could simply drop the check for FQN names in doc comments from Coder. Tools like PHPStan are better in verifying that the class names used there are valid references.
Comment #4
kingdutchI used to be against this because it led to codebases having `use` import statements for types that weren't used in code but only in comments. However:
usestatement is already neededGiven those developments within PHP, I'm also happy to +1 this proposal :D
Comment #5
acbramley commentedBig +1 on this, we've got some wildly long union types, could this also apply to @property annotations?
Comment #6
kingdutchMoved into new coding standards template and added myself as supporter.
Generalised the title to PHPDoc annotations so we don't need to list all
@annotations that it may support.Comment #7
borisson_Comment #8
bbralaComment #9
mondrakeBTW Rector here can help a lot, https://getrector.com/documentation/import-names
Working with the configuration of
withImportNamesone can get (or not) existing FQCNs in PHPDoc be split between theuseimport and the remaining class name in the docs, can remove (or not) unuseduseimports.Once this is finalized, a single rector run could just clean the entire codebase.
Comment #10
mondrakeComment #11
dwwTagging for a summary update since I'm out of time to start fleshing out the proposed changes. Also worth reviewing that I found all the spots we need to fix.
Thanks!
-Derek
Comment #12
quietone commentedComment #13
andypostComment #14
quietone commentedComment #16
quietone commentedStarted an MR with a minor change just to show where this needs to be changed. Not sure if there are other places.
Or, the text could be like what is in the PHPStan docs
Comment #17
borisson_I think this is the only place that needs to change, I like the proposed text, to me that is clear. I like that phpstan docs a bit more because it also includes this part: resolved on the current namespace or via a use statements. If we can add that it seems good to go.
Comment #18
quietone commentedI made that change.
Comment #19
borisson_Looks good to me, thanks @quietone
Comment #20
drunken monkeyNot sure, I do think there are other places where this needs to be changed. I see the following other lines on this page:
I’d say those should be changed (or maybe even removed, in the former case) as well.
I also think we should be consistent in whether we hyphenate “fully-qualified” but that might be a different issue. (I see two occurences without a hyphen, here and in
namespaces.md.)The change itself looks good to me, though, so +1 on that.
Comment #21
quietone commentedUpdated the MR so the change is described in 'data types in documentation' and have other places reference that. That should help to keep future changes in one place, not many.
Comment #22
borisson_This looks great. I really like that last change, it should help keep us up to date in future.
Comment #24
quietone commentedThis was brought up in meeting, #3576923: Coding Standards Meeting Wednesday 2026-03-04 0700 UTC, and there was no disagreement with this. Removing the tag for an IS update since that seemed about getting the wording correct and that is now in the MR.
Comment #25
quietone commentedComment #26
jonathan1055 commentedI ran the 'pages' manual job and here is the updated (temporary) site, so that you can check the actual changes:
https://coding-standards-3360160-86968b.pages.drupalcode.org/php/documen...
I would have linked to the actual paragraph, but not all of them have automatic links to the sub-sections. It's confusing because the ones that do have links appear to be smaller with a smaller title. It should be easy to do, and would really help with navigation, so maybe would you like me to open a new issue for that?
Comment #27
borisson_@jonathan1055 I think that's a good idea, can you open a new issue for that?
Comment #28
jonathan1055 commentedOK here is it #3580194: Titles and automatic links to sub-sections
Comment #29
jonathan1055 commentedActually I was wrong, there are links to the sections. Here is a link to the updated paragraph
https://coding-standards-3360160-86968b.pages.drupalcode.org/php/documen...
I was thinking of the functionality of sites such as the Gitlab Templates documentaion, where if you hover over the title you see the pop-up link which gives you the permalink url. But that feature is provided by Zensical not MkDocs.
I will close the other issue, sorry for the distraction.
Comment #30
borisson_The cr looks good, but we should resolve the remark by @drunken monkey as well.
Comment #31
joachim commentedWe don't seem to have checked that IDEs actually support this?
I can confirm that Visual Studio Code does.
Comment #32
mstrelan commentedPhpStorm does too.