- 🇳🇿New Zealand quietone
Updated issue summary with new template, the current text of PSR-12 and completed the existing Drupal text and proposed text.
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.
If we adopted this change, the Drupal Project would benefit by ...
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. }
For a full explanation of these steps see the Coding Standards project page →
Active
Coding Standards
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.
Updated issue summary with new template, the current text of PSR-12 and completed the existing Drupal text and proposed text.