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

  1. Create a coding standard.
  2. Update Drupal Core to use and conform to that standard.

Or we could do nothing and let both be acceptable.

User interface changes

API changes

Data model changes

Comments

solideogloria created an issue. See original summary.

solideogloria’s picture

Title: Inconsistent spacing after "fn" of arrow function » Standardize spacing after "fn" of arrow function
Category: Task » Feature request
solideogloria’s picture

Issue summary: View changes
eric_a’s picture

Commented in #3364902-4: Define a standard for arrow functions:

From https://www.php-fig.org/per/coding-style/#71-short-closures:

Short closures, also known as arrow functions, MUST follow the same guidelines and principles as long closures above, with the following additions.

The fn keyword MUST NOT be succeeded by a space.

joachim’s picture

> 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

solideogloria’s picture

The 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