0

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?

user2094178
  • 9,204
  • 10
  • 41
  • 70
  • 5
    "When Laravel 5.1 is upon us, PSR-2 will be enforced." Not on your code. Don't want to write PSR-2? Don't. – ceejayoz Mar 29 '15 at 00:44

1 Answers1

1

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
ChadSikorra
  • 2,829
  • 2
  • 21
  • 27