Problem/Motivation
PHP 7.0 will bring some new features that need some coding standard decisions. How we want to format anonymous classes is the largest among them. There are also some gray areas in the current standards that aren't significant enough to warrant tightening on their own, but could be included in a larger sweep. Creating this ticket now to allow some time (over a year) for debate. The decisions around this issue ticket should be finalized before the first version of Drupal that does not support PHP 5, else any code written to use newer PHP 7 features will be doing so without coding standard guidelines.
Note:I'm not proposing changing any existing code standard - this is an expansion.
Proposed resolution
These are the current items on discussion.
#Function Declarations
There is no current rule on multiple arguments, especially multiple type hinted arguments. Consider the following out of the DrupalKernel class
public static function createFromRequest(Request $request, $class_loader, $environment, $allow_dumping = TRUE, $app_root = NULL) {This is already over 80 characters by a long shot, and under PHP 7.0 we would want to enforce the scalar types and return as follows:
public static function createFromRequest(Request $request, $class_loader, string $environment, bool $allow_dumping = TRUE, string $app_root = NULL) : DrupalKernel {
Consider the following alternatives:
// Function with argument list less than 80 characters.
public static function createFromRequest( Request $request ) : DrupalKernel {
}
// Function with argument list more than 80 characters, with return type.
public static function createFromRequest(
Request $request,
$class_loader,
string $environment,
bool $allow_dumping = TRUE,
string $app_root = NULL
) : DrupalKernel {
}
// Without return type
public function foo (
$a,
$b
) {
Note the above mimics the existing coding standards for arrays.
#Anonymous Classes
Here's the PHP page example.
$app->setLogger(new class implements Logger {
public function log(string $msg) {
echo $msg;
}
});
This seems in line with what we already do, and it's similar enough to anonymous functions that we might just add an example and not actually change anything.
Comments
Comment #2
ivan berezhnov commentedComment #3
Aki Tendo commentedFixing a typo in summary.
Comment #6
chi commentedComment #7
drunken monkey+1 for the anonymous class example, -10 for changing our function declaration standards. (This also runs against your note about not wanting to change existing standards.)
Note that there’s also already #2928856: Adopt the PSR-12 standard for PHP return types for return type declarations. You’re right, we should really aim to get these standards tied down before Core starts using PHP 7 features. Unfortunately, though, it seems the coding standards approval process has died down once again. (But maybe I’m just imagining it.)
Comment #8
Aki Tendo commentedI was not aware of an existing standard for return types - but keep in mind I also put this in almost 9 months ago. As there is a standard just disregard my suggestion and follow that standard.
Comment #9
quietone commentedThis issue was created to discuss changes to coding standards in response to the introduction of PHP7. It was to be done before the first version of Drupal that does not support PHP 5. When was the first version of Drupal that did not support PHP 5?
Support for PHP 5.5 and 5.6 was dropped in Drupal 8.7x. That was back in 2019. While versions of PHP 7 are still supported they are not recommended for Drupal 9 or 10. I think that makes this outdated. And there are already issues in the queue for adapting to PHP 8.
I am going to close this as outdated. If you disagree, re-open and explain what else needs to be done.
Thanks