Problem/Motivation
With the introduction of PHP 7.4 the language now supports indicating what type a class property is (in older versions this was limited to PHP primitive types). In the current Drupal Coding Standards a @var annotation is required for class properties. However, since this information can now be added in code (when targetting at least PHP 7.4), those comments are often duplicated information that risk becoming out of sync.
Steps to reproduce
Proposed resolution
Adjust the coding standards to allow not adding a @var comment to class properties in case those properties have a type defined using PHP's typing system.
#3123282: Do not require @var tag if a property has typehint provides a patch for coder that demonstrates this behaviour.
The following code would now be valid.
class Foo {
/**
* Where one can order a soda.
*/
protected Bar $baz;
/**
* Some complex type that probably needs cleaning up in a future.
*
* This type can not be represented in the PHP typesystem yet.
*
* @var int|string|NULL
*/
public $someVar;
}
Documentation changes
1. @var: Class property data types
Current text
class FooBar { /** * The database connection object for this statement's DatabaseConnection. * * @var \Drupal\Core\Database\Connection */ public $dbh; }Syntax notes: The @var tag is followed by a space and then a data type specification.
Proposed text
class FooBar { /** * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; }Syntax notes: The @var tag is followed by a space and then a data type specification.
class FooBar { use \Drupal\Core\Entity\EntityTypeManagerInterface; /** * The entity type manager. */ protected EntityTypeManagerInterface $entityTypeManager; }Typed class properties may omit the @var declaration. It is recommended to use typed properties whenever possible.
Comments
Comment #2
chi commentedIt can be presented using union type hint in PHP 8.
Comment #3
drunken monkey+1
Comment #4
pfrenssen+1
Comment #5
claudiu.cristea+1. I think it makes sense.
The Coder PR is ready for review https://github.com/pfrenssen/coder/pull/164
Comment #6
pfrenssenComment #7
romain.sickenberg+1!
Comment #8
mondrake+1
Comment #9
larowlanFWIW another +1 from me, we've already adopted this in our client projects
Comment #10
borisson_Another +1; we now need to go trough announcement and final discussions.
Comment #11
donquixote commented+1, yes!
Comment #12
ghost of drupal pastGood first step but needs several followups
protected LockbackendInterface $lockBackend. Optionally yes but most of the time the type tells the storyprotected function getLockbackend(): LockbackendInterface, gee, I have no idea what this method could possibly be about, let me try to guess.Comment #13
quietone commentedAdd the existing documentation to the Issue Summary.
It will help if someone would complete the proposed text section of the Issue Summary.
Comment #14
larowlanTook a shot at a proposed text
Comment #15
ghost of drupal pastFiled #3376518: Allow omitting @var, @param and @return tags or their descriptions if the variable name and type is enough for a followup.
Comment #16
quietone commented@larowlan, thanks.
I like examples, so I expanded on suggestion.
Comment #21
quietone commentedClosed #3291517: [policy, no patch] Use type declarations for new class properties as a duplicate. I am adding credit.
Comment #22
quietone commentedComment #23
tyler36 commented+1 Feels like we are embracing PHP conventions.
Comment #24
gngn commented+1
Comment #25
bbralaThis has been standard practice in our client code for ages. +1
Comment #26
dww+1, yes please!
Comment #27
dwwRemove duplicate
<code>from proposed resComment #28
dwwComment #29
mondrakeIMHO the examples should be more generic. The
$dbhproperty is long gone from the Database API and regadless of that it was a very special case being a PDO related property.Comment #30
wim leers🙏 Yes, please! Far less boilerplate 👍
Comment #31
urvashi_vora commented+1
Comment #32
pfrenssenUpdated the wording taking into account the suggestions from #28 and #29. Switched from a public property to a protected one. Used the EntityTypeManagerInterface since this is very recognizable, and uses the interface as a type which is the best practice.
Comment #33
catchI agree with this as it stands, but it seems worth noting that #3278431: [May 2026] Use PHP 8 constructor property promotion for existing code means that any property promoted via a contructor won't be defined separately at all, so the entire statement goes in that case. That makes me wonder how many strictly typed properties will be left to document once that issue lands.
Comment #34
quietone commentedI think we missed a step, according the current process this needs to have a working patch to test the change.
And what happens to the existing sniff, currently excluded,
<exclude name="Drupal.Commenting.VariableComment.Missing"/>Comment #35
catchIf we follow the new proposal at #3365085: Update the coding standards process on the project page then based on the current situation, we should have checked the existing sniff (which you just have), but we're not blocked on a new one.
Since
<exclude name="Drupal.Commenting.VariableComment.Missing"/>is already disabled and therefore changing the rule won't break core's existing enabled ruleset.That would get us to:
in the new process.
i.e. we should open an issue against coder asking for either a new sniff, or a modification to
<exclude name="Drupal.Commenting.VariableComment.Missing"/>, as part of the process of marking this issue fixed.Based on that, moving back to RTBC.
If we want to follow the documented process, then I think we should just update the new process to what we've got proposed rather than trying to follow the old one.
Comment #36
quietone commentedI have updated the documentation.
Comment #37
bbralaConfirmed the changes in the documentation. This can be marked as fixed.
Comment #38
bbralaFixing credits:
Kingdutch: made issue, discussed in slack.
quietone: managing this the last few months, and driving discussion.
larowlan: heping out with issue management and discussion
chx: discussion, and followup work.
bbrala: discussions and issue management
pfrenssen: work on docs
dww: work on docs
Comment #39
bbralaMarking as fixed. Lovely to see a codingstandards issue fixed. <3
Thanks everyone.
Comment #41
penyaskitoDid we ever create a follow-up for converting existing code? It's hard to enforce this policy when consistency matters.
Comment #42
bbralaProbably not, just make one?
Comment #43
bbralaCreated a child: #3522636: Remove @var for strictly typed class properties