Allow omitting @var for strictly typed class properties

Created on 21 September 2021, over 3 years ago
Updated 13 September 2023, over 1 year ago

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 โ†’

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 โ†’ .

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.

Remaining tasks

โœจ Feature request
Status

Fixed

Component

Coding Standards

Created by

๐Ÿ‡ณ๐Ÿ‡ฑNetherlands kingdutch

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024