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
-
https://www.drupal.org/u/ →
{userid} (yyyy-mm-dd they added support)
-
https://www.drupal.org/u/ →
{userid} (yyyy-mm-dd they added support)
-
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:
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
- Add supporters
- Create a Change Record
- Review by the Coding Standards Committee
- Coding Standards Committee takes action as required
- Discussed by the Core Committer Committee, if it impacts Drupal Core
- Final review by Coding Standards Committee
- Documentation updates
- Edit all pages
- Publish change record
- Remove 'Needs documentation edits' tag
- If applicable, create follow-up issues for PHPCS rules/sniffs changes
For a full explanation of these steps see the
Coding Standards project page →