Problem/Motivation
Drupal core doesn't use any standard regarding arrow function spacing.
Steps to reproduce
# \Drupal\Core\Config\Schema\SchemaCheckTrait.php
fn(ConstraintViolation $v) => !static::isViolationForIgnoredPropertyPath($v)
# OR, in \Drupal\Core\Form\ConfigTarget.php
array_walk($value, fn (mixed $value, string $property) => match ($value) {
// No-op.
ToConfig::NoOp => NULL,
// Delete.
ToConfig::DeleteKey => $config->clear($property),
// Set.
default => $config->set($property, $value),
});
The first example has fn(...) with no space after "fn". The second example has fn (...) with a space after "fn".
Proposed resolution
The Drupal coding standards for PHP page does say
Anonymous functions should have a space between "function" and its parameters, as in the following example:
array_map(function ($item) use ($id) { return $item[$id]; }, $items);
However, this isn't for arrow functions, which are newer. When searching in a codebase that contains lots of contrib modules, almost all of the arrow functions, including the ones in Core, use arrow functions without a space. For reference, Core 10.2.4 has 46 uses of arrow functions without a space. There are only 5 occurrences of an arrow function with a space in the same version of Core.
Remaining tasks
- Create a coding standard.
- Update Drupal Core to use and conform to that standard.
Or we could do nothing and let both be acceptable.
Comments
Comment #2
solideogloria commentedComment #3
solideogloria commentedComment #4
eric_a commentedCommented in #3364902-4: Define a standard for arrow functions:
From https://www.php-fig.org/per/coding-style/#71-short-closures:
Comment #5
joachim commented> The fn keyword MUST NOT be succeeded by a space.
What's the logic behind this?
It's a bit confusing to have different spacing rules for the short and the long anonymous function notation
Comment #6
solideogloria commentedThe PHP standards are voted on by the voting members of the group. See https://www.php-fig.org/faqs/
Note that this is the same way that features are added or not for PHP itself. See https://php.watch/rfcs