Search code examples
phpzend-framework2phpcodesniffer

ZF2 and PHPCSMD using PSR-2 in NetBeans 7.4


In NetBeans 7.4 I am using a PHP:CS plugin set to use PSR-2 rules for controlling the source code. My colleague has the same settings (we checked it), same version of the plugin, same version of NetBeans. Yet we have different results after the code review.

While this code:

return new JsonModel(array(
    'success' => true,
    'data' => $someData,
));

is displaying as completely OK (no error, no warning) after the code is reviewed by PHPCSMD plugin, on colleagues machine this is marked in red as error and it says it brakes PSR-2 rules because the split rows have to start on a new line.

According to the PSR-2 documentation, this exact case is not mentioned anywhere therefore I have no idea whether it is marked as error on colleagues machine correctly or not (so it is correctly not marked as error on my machine :-) ).

The question: according to PSR-2, is this one really incorrect:

return new JsonModel(array(
    'success' => true,
    'data' => $someData,
));

and should I be only using this:

return new JsonModel(
    array(
        'success' => true,
        'data' => $someData,
    )
);

???


Solution

  • Both are valid, PSR2 compliant, constructs. You can test both (bash) statements with phpcs:

    you@server ~ $ phpcs --standard=PSR2 <<EOF
    return new JsonModel(array(
        'success' => true,
        'data' => $someData,
    ));
    EOF
    

    and:

    you@server ~ $ phpcs --standard=PSR2 <<EOF
    return new JsonModel(
        array(
            'success' => true,
            'data' => $someData,
        )
    );
    EOF
    

    Btw, I've used:

    PHP_CodeSniffer version 1.5.1 (stable) by Squiz (http://www.squiz.net)