Search code examples
phplaravelphp-psrphp-fig

How to circumvent PSR-2?


When Laravel 5.1 is upon us, PSR-2 will be enforced.

I'm a big fan of PHP-FIG, unfortunately for me I got really used to and comfortable with control structures in a new line.

Consider this current piece of code, already adhering to PSR-2:

foreach($items as $item) {
    Cart::update($item, Input::get('qty_' .$item));
}

I understand the following is not PSR-2:

foreach($items as $item)
{
    Cart::update($item, Input::get('qty_' .$item));
}

But, how about these variations?

foreach($items as $item) Cart::update($item, Input::get('qty_' .$item));


foreach($items as $item)

    Cart::update($item, Input::get('qty_' .$item));


foreach($items as $item):

    Cart::update($item, Input::get('qty_' .$item));

endforeach;

As you can see, I got addicted to the white space resulting from the lead curly brace when going into a new line.

Can any of the variations mentioned be considered properly PSR-2?


Solution

  • No, none of those variations are PSR-2 compliant either. A control structure needs to have braces and there should be a space following the control structure name. These rules are defined rather explicitly here:

    Control Structure Guidelines

    • There MUST be one space after the control structure keyword
    • There MUST NOT be a space after the opening parenthesis
    • There MUST NOT be a space before the closing parenthesis
    • There MUST be one space between the closing parenthesis and the opening brace
    • The structure body MUST be indented once
    • The closing brace MUST be on the next line after the body