简体   繁体   English

如何规避PSR-2?

[英]How to circumvent PSR-2?

When Laravel 5.1 is upon us, PSR-2 will be enforced. 当Laravel 5.1出现时,将执行PSR-2。

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. 我是PHP-FIG的忠实拥护者,对我而言,不幸的是,我真的习惯并适应了新系列中的控制结构。

Consider this current piece of code, already adhering to PSR-2: 考虑下面已经遵守PSR-2的当前代码:

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

I understand the following is not PSR-2: 我了解以下不是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? 可以将上述任何变体适当地视为PSR-2吗?

No, none of those variations are PSR-2 compliant either. 不,这些版本均不符合PSR-2。 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 右括号必须在主体之后的下一行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM