Adopt the PSR-12 standard for PHP7 return types

Created on 6 December 2017, about 7 years ago
Updated 27 November 2024, 29 days ago

Problem/Motivation

PHP 7 introduces return type declarations. We should define a standard for using them. It would be interesting to follow the standard that is (or will be) adopted by the wider PHP community. Return types are not described in any of the existing standards like PSR-2, but it is part of PSR-12 which currently is in draft version (ref. https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-12-ext...).

Here are the relevant sections from the standard:

When you have a return type declaration present, there MUST be one space after the colon followed by the type declaration. The colon and declaration MUST be on the same line as the argument list closing parenthesis with no spaces between the two characters.

<?php

declare(strict_types=1);

namespace Vendor\Package;

class ReturnTypeVariations
{
    public function functionName(int $arg1, $arg2): string
    {
        return 'foo';
    }

    public function anotherFunction(
        string $foo,
        string $bar,
        int $baz
    ): string {
        return 'foo';
    }
}

In nullable type declarations, there should be no space between the question mark and the type.

<?php

declare(strict_types=1);

namespace Vendor\Package;

class ReturnTypeVariations
{
    public function functionName(?string $arg1, ?int &$arg2): ?string
    {
        return 'foo';
    }
}

We can start with this as a basis, rewriting it a bit in the "more human friendly" writing style that we typically use, and rework the examples to be more Drupally.

Benefits

If we adopted this change, the Drupal Project would benefit by ...

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. https://www.drupal.org/docs/develop/standards/php/php-coding-standards#s...

Beginning with Drupal 9, parameter and return type hints should be used wherever possible. Example function definition using parameter and return type hints:

public function myMethod(MyClass $myClass, string $id): array {
  // Method code here.
}

New functions and methods

Parameter and return type hints should be included for all new functions and methods, including new child implementations of methods for existing classes and interfaces.

Beginning with Drupal 9, parameter and return type hints should be used wherever possible. Example function definition using parameter and return type hints:

public function myMethod(MyClass $myClass, string $id): array {
  // Method code here.
}

New functions and methods

Parameter and return type hints should be included for all new functions and methods, including new child implementations of methods for existing classes and interfaces.

In nullable type declarations, there should be no space between the question mark and the type.

public function myMethod(MyClass $myClass, string $id): array {
  // Method code here.
}

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

🇧🇬Bulgaria pfrenssen Sofia

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