PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Suggestion: allow simple one-liner methods

Open igorsantos07 opened this issue 7 years ago • 1 comments

This is based on an issue I'm having on the project I work on. While I was looking for a solution for it (and after digging through code and not really understanding its relationship with the affected rules), I stumbled upon a StackOverflow question that summarizes exactly my problem, so I'm quoting it here:

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:

  • Squiz.Functions.MultiLineFunctionDeclaration.ContentAfterBrace says that Opening brace must be the last content on the line
  • Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore says that Closing brace must be on a line by itself
  • Generic.Formatting.DisallowMultipleStatements.SameLine says that Each PHP statement must be on a line by itself 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?

igorsantos07 avatar Dec 26 '18 04:12 igorsantos07

I also would like a sniff for this type of thing for empty methods or very simple methods with a single statement.

i.e:

protected function __construct() {}

or

public function someAbstractMethod() : bool { return TRUE; }

calguy1000 avatar Feb 17 '19 15:02 calguy1000