coding-standards icon indicating copy to clipboard operation
coding-standards copied to clipboard

Lambdas are a thing now

Open okonomiyaki3000 opened this issue 12 years ago • 1 comments

I think the standard needs to be relaxed a little bit for lambdas (if possible). For example:

$mapped = array_map(function ($a) { return $a->prop; }, $someObjects);

Will produce two phpcs errors:

  • Closing brace must be on a line by itself
  • Each PHP statement must be on a line by itself

So then, you might try:

$mapped = array_map(function ($a) {
        return $a->prop;
    }, 
    $someObjects
);

Which is already terrible but then you get:

  • Opening parenthesis of a multi-line function call must be the last content on the line
  • Closing brace indented incorrectly; expected 2 spaces, found 3

So you try:

$mapped = array_map(
    function ($a) {
        return $a->prop;
    }, 
    $someObjects
);

And somehow it works but it's hideous.

okonomiyaki3000 avatar Oct 02 '13 04:10 okonomiyaki3000

Technically it's related to PHPCS and the built-in rules that we borrow from. the rules that are thrown from the first code snip are PEAR.Functions.FunctionDeclaration.ContentAfterBrace Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore Generic.Formatting.DisallowMultipleStatements.SameLine

To solve this we likely need to open an issue with PHPCS relating to lambda parsing and possibly exclusions for lambdas (there might be an open issue with PHPCS on this to join in on).

photodude avatar Jan 09 '17 16:01 photodude