fracz
fracz

Reputation: 21278

PHPCS Allow simple methods in one line

I find the code

$ids = array_map(function ($entity) { return $entity->getId(); }, $entities);

much more readable than

$ids = array_map(function ($entity) { 
    return $entity->getId(); 
}, $entities);

However, the PHPCS forces me to use the second style, because the first one fails with the following for PSR-2 styles:

While I generally agree with these checks in other places, in this case they are completely wrong.

Is there a rule that would say to PHPCS that it should allow Simple methods in one line just like PHPStorm does?

Simple methods in one line in PHPStorm

I know I can ignore PHPCS checks in such places but I would prefer tackling this in a global configuration.

Upvotes: 5

Views: 621

Answers (1)

rassoh
rassoh

Reputation: 674

If you don't like the above and are using PHP >= 8, you can do this instead:

$ids = array_map(fn ($entity) => $entity->getId(), $entities);

PHP CodeSniffer won't complain about that one.

Upvotes: 1

Related Questions