Traits should always have the suffix "Trait"

Created on 28 April 2025, 1 day ago

Problem/Motivation

Traits should always have the suffix "Trait". This is a rule that mirrors the rule that interfaces always have the suffix "Interface".

There are 4 traits in Drupal core that do not end in Trait. 3 of them are test traits:

Drupal\Core\Menu\MenuLinkFieldDefinitions
Drupal\Tests\dblog\Functional\FakeLogEntries
Drupal\Tests\link\Traits\LinkInputValuesTraits
Drupal\Tests\workspaces\Functional\WorkspaceTestUtilities

Benefits

In πŸ“Œ Add a fallback classloader that can handle missing traits Active , a classloader was introduced to prevent reflection on classes with missing traits (for example, classes that use traits from uninstalled modules) during plugin discovery by PHP attributes. The detection of missing traits relies on whether the name of class/interface/trait being loaded ends in "Trait". There's no way otherwise to determine what is being loaded, so it is possible for the detection to fail and result in PHP fatal error.

The need for trait detection specifically will presumably go away in PHP 8.5, where missing traits will cause a catchable exception to be thrown instead of resulting in a fatal error. This change in PHP has already been committed.

Three supporters required

  1. https://www.drupal.org/u/ β†’ {userid} (yyyy-mm-dd they added support)
  2. https://www.drupal.org/u/ β†’ {userid} (yyyy-mm-dd they added support)
  3. https://www.drupal.org/u/ β†’ {userid} (yyyy-mm-dd they added support)

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. Class Methods and Properties β†’

  1. Classes and interfaces should use UpperCamel naming.
  2. Methods and class properties should use lowerCamel naming. In Drupal 8, properties of configuration entities are exempt of these conventions. Those properties are allowed to use underscores.
  3. If an acronym is used in a class or method name, make it CamelCase too (SampleXmlClass, not SampleXMLClass). [Note: this standard was adopted in March 2013, reversing the previous standard.]
  4. Classes should not use underscores in class names unless absolutely necessary to derive names inherited class names dynamically. That is quite rare, especially as Drupal does not mandate a class-file naming match.
  5. Names should not include "Drupal".
  6. Class names should not have "Class" in the name.
  7. Interfaces should always have the suffix "Interface".
  8. Test classes should always have the suffix "Test".
  9. Protected or private properties and methods should not use an underscore prefix.
  10. Classes and interfaces should have names that stand alone to tell what they do without having to refer to the namespace, read well, and are as short as possible without losing functionality information or leading to ambiguity. Notes:
    • If necessary for clarity or to prevent ambiguity, include the last component of the namespace in the name.
    • Exception for Drupal 8.x: due to the way database classes are loaded, do not include the database engine name (MySQL, etc.) in engine-specific database class names.
    • Exception for test classes: Test classes only need to be unambiguous within the context of the module they are testing.
  1. Classes and interfaces should use UpperCamel naming.
  2. Methods and class properties should use lowerCamel naming. In Drupal 8, properties of configuration entities are exempt of these conventions. Those properties are allowed to use underscores.
  3. If an acronym is used in a class or method name, make it CamelCase too (SampleXmlClass, not SampleXMLClass). [Note: this standard was adopted in March 2013, reversing the previous standard.]
  4. Classes should not use underscores in class names unless absolutely necessary to derive names inherited class names dynamically. That is quite rare, especially as Drupal does not mandate a class-file naming match.
  5. Names should not include "Drupal".
  6. Class names should not have "Class" in the name.
  7. Interfaces should always have the suffix "Interface".
  8. Traits should always have the suffix "Trait".
  9. Test classes should always have the suffix "Test".
  10. Protected or private properties and methods should not use an underscore prefix.
  11. Classes and interfaces should have names that stand alone to tell what they do without having to refer to the namespace, read well, and are as short as possible without losing functionality information or leading to ambiguity. Notes:
    • If necessary for clarity or to prevent ambiguity, include the last component of the namespace in the name.
    • Exception for Drupal 8.x: due to the way database classes are loaded, do not include the database engine name (MySQL, etc.) in engine-specific database class names.
    • Exception for test classes: Test classes only need to be unambiguous within the context of the module they are testing.

Remaining tasks

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

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

πŸ“Œ Task
Status

Active

Component

Coding Standards

Created by

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

Comments & Activities

Production build 0.71.5 2024