Coding standard to enforce backslash on function calls outside of a namespace

Created on 23 January 2025, 2 months ago

Problem/Motivation

This has been a discussion point in the MR's of our team for our projects. Mainly about global functions calls like \Drupal:: in none name-spaced files (e.g. .module, .install, .api,...).

I would usually write my code with the leading backslash.

$facet_manager = \Drupal::service('facets.manager');

But then a team member would come in saying the leading backslash is not required as we're not in a namespace. Looking at Drupal core, I can see a mix of both being used, so I think it's good if we could standardise this as a standard.

Core example where it is done with a \ : https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/autom...

Core example where it is done withouth a \ : https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/toolb...

Happy to say thought that there are not that many cases without the \

Benefits

By enforcing this change, function calls like \Drupal:: are written the same everywhere when not in a namespace.

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. {link to the documentation heading that is to change}

Add current text in blockquotes

Add proposed text in blockquotes

2. Repeat the above for each page or sub-page that needs to be changed.

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

Feature request
Status

Active

Component

Coding Standards

Created by

🇧🇪Belgium BramDriesen Belgium 🇧🇪

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

Comments & Activities

  • Issue created by @BramDriesen
  • 🇧🇪Belgium BramDriesen Belgium 🇧🇪
  • 🇧🇪Belgium borisson_ Mechelen, 🇧🇪

    Added a very basic line of what we need to add. Should be refined.

  • 🇧🇪Belgium BramDriesen Belgium 🇧🇪

    Attaching screenshot of Drupal Core (11.x) code search. Looks like many things have been fixed already between Drupal 9/10 to 11 as I can find many more occurrences in our projects (might come from patches as well of course).

  • 🇦🇹Austria drunken monkey Vienna, Austria

    Thanks for opening this!

    The current documentation on reads:

    Classes and interfaces without a backslash \ inside their fully-qualified name (for example, the built-in PHP Exception class) must be fully qualified when used in a namespaced file. For example: new \Exception();. Do not use global classes.

    It seems we’d just need to remove the “when used in a namespaced file” part?

    Adding myself as a supporter. Also fixing the title – this is not about function calls but about references to classes (for static method calls, constructing new objects or just the ::class reference).

  • 🇧🇪Belgium BramDriesen Belgium 🇧🇪

    It seems we’d just need to remove the “when used in a namespaced file” part?

    Probably yes, and update coder/phpcs to check for this.

  • 🇬🇧United Kingdom Sophie.SK

    This comes up in our pull requests a lot as well. My preference is to not include the backslash but I think I'm in the minority.

    My preference is for a coding standard and consistency. It looks like the precedent generally is to include the backslash, so lending my support for this change.

  • 🇧🇪Belgium BramDriesen Belgium 🇧🇪
Production build 0.71.5 2024