Closures (anonymous functions) are currently erroneously marked with a style warning: Functions should be called with no spaces between the function name and opening parentheses.

CommentFileSizeAuthor
#1 coder-closures-1272900.patch694 bytessmk-ka
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

smk-ka’s picture

Status: Active » Needs review
FileSize
694 bytes

Added function to the list of ignored words.

douggreen’s picture

Status: Needs review » Needs work

I applied, tested the results before and after, and this did no harm, because Drupal core doesn't use anonymous functions. I applied but we should probably write a test case for this, so leaving open.

douggreen’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
hefox’s picture

To clarify

function ($what) use ($var) {
}

is correct spacing as function is a keyword in this case? (vs. function($what)

thursday_bw’s picture

I'm having issue with closures and anonymous functions..

example:

187       $arr_to_obj = function($arr) {
188         return (object) $arr;
189       }

gives:

 187 | ERROR   | Line indented incorrectly; expected 28 spaces, found 6
 188 | ERROR   | Line indented incorrectly; expected 30 spaces, found 8

Which is just silly.. but then if I do indent line 187 to 28 spaces. I instead get

 187 | ERROR   | Line indented incorrectly; expected 6 spaces, found 28
 188 | ERROR   | Line indented incorrectly; expected 30 spaces, found 8

as a side not, this anonymous function was originally passed straight to array_map like so:

195         'modules' => array_map(function($module) {
196             return (object) $module;
197         }, $course['modules']),

Which gives the same behaviour as described above.

thursday_bw’s picture

More info.. It appears that this is the result of having a closer in a closure. :/
the code snippet above, is I've found now to be nested in an existing closer..
there are no errors about the parent closure.

thursday_bw’s picture

OK.. My last to comments were admittedly a bit blah.

For clarification I have included various examples of closures in different contexts.
Many of these pass the tests, but the last couple fail.

Please let me know if this belongs in it's own thread.

<?php
/**
 * @file
 * This is some basic code with various styles and types of closures contained.
 * To see how well the drupal_standards run against this file.
 */

/**
 * A standard closure.
 *
 * Passes.
 */
$standard_closer = function($arg) {
  $sometruevar = TRUE;
  if (TRUE = $sometruevar) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}


/**
 * A nested closure.
 *
 * Passes.
 */
$nested_closer = function($arg) {
  return function() {
    return 1 + 1;
  }
}

/**
 * A closure nested in a function call as first arg.
 *
 * Passes.
 */
$nested_closer = function($arg) {
  return array_map(function() {
    return 1 + 1;
  }, array());
}

/**
 * A closure nested in a function call as secondarg arg.
 *
 * Passes.
 */
$nested_closer = function($arg) {
  return somefunct($var, function() {
    return 1 + 1;
  }); 
}

/**
 * A closure nested in a function call as secondarg arg.
 *
 * Passes
 */
function some_func_one() {
  $nested_closer = function($arg) {
    return somefunct($var, function() {
      return 1 + 1;
    });
  }
}

/**
 * A closure as argument to function in array definition.
 *
 * Fails.
 * --------------------------------------------------------------------
 *  83 | ERROR | Line indented incorrectly; expected 2 spaces, found 4
 *  84 | ERROR | Line indented incorrectly; expected 4 spaces, found 6
 * ---------------------------------------------------------------------
 */
function some_func_two() {
  $block['content']['children'][] = array(
    '#theme' => 'my_theme',
    '#type' => MY_CONSTANT,
    '#count' => array_reduce($unread, function ($a, $b) {
      return $a + $b->unread;
    }, 0),
  );
}

/**
 * A closure as argument to function in array definition.
 *
 * Fails.
 * --------------------------------------------------------------------
 * 102 | ERROR | Array indentation error, expected 4 spaces but found 2
 * 104 | ERROR | Closing brace indented incorrectly; expected 2 spaces, found 4
 * --------------------------------------------------------------------
 */
function some_func_three() {
  $block['content']['children'][] = array(
    '#theme' => 'my_theme',
    '#type' => MY_CONSTANT,
  '#count' => array_reduce($unread, function ($a, $b) {
    return $a + $b->unread;
    }, 0),
  );
}
shrop’s picture

I am also running into an issue where using anonymous functions causes subsequent lines of code to be falsely flagged as having the wrong indentions.

micromegas’s picture

Issue summary: View changes

I confirm closures are causing issues. For example, if you run coder sniffer on the following file, it will erroneously mark the closure bodies as having incorrect indentation.

<?php
/**
 * @file
 * Shows indentation being incorrectly marked within closures.
 */

/**
 * An array of closures.
 */
$closures = array(
  '1' => function($number) {
    return $number;
  },
  '2' => function($number) {
    return $number * 2;
  },
  '3' => function($number) {
    return $number * 3;
  },
);

$multiplier = '2';
$test = $closures[$multiplier](55);

print_r($test);

/*
FOUND 3 ERROR(S) AFFECTING 3 LINE(S)
--------------------------------------------------------------------------------
 12 | ERROR | Array indentation error, expected 2 spaces but found 4
 15 | ERROR | Array indentation error, expected 2 spaces but found 4
 18 | ERROR | Array indentation error, expected 2 spaces but found 4
--------------------------------------------------------------------------------
*/

Even if you change the indentation to satisfy coder, it still gives the indentation error, asking for the number of spaces that it was before you changed it.

markdorison’s picture

Version: 7.x-2.x-dev » 8.x-2.x-dev
klausi’s picture

Status: Needs work » Fixed

Those example closures now all work as expected with the latest Coder release.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.