PSR-2 代码风格规范

本指南扩展并扩展psr-1基本编码标准。

本指南的目的是减少扫描代码时的认知摩擦
来自不同作者。它通过枚举共享的规则集和
关于如何格式化php代码的期望。

这里的样式规则是从各个成员之间的共性派生出来的。
项目。当不同的作者跨多个项目进行协作时,它会帮助您
在所有项目中都要使用一套指导方针。因此,
本指南的益处并不在于规则本身,而是在共享
那些规则。

关键词“MUST(必须”、“MUST NOT(不可”、“REQUIRED(必要”、“SHALL(”、“SHALL NOT(不应”、“SHOULD(”、“SHOULD NOT(不
本文件中“RECOMMENDED(推荐”、“MAY可能”和“OPTIONAL(可选”。
如图中所述解释RFC 2119

1.概述

  • 代码必须遵循一个“编码样式指南”psr[psr-1*

  • 代码必须使用4个空格来缩进,而不是制表符。

  • 行长度不得有硬性限制;软极限必须为120。
    字符;线条应该是80个字符或更少。

  • 在此之后必须有一个空行。namespace宣言,还有那里
    必须是一个空行之后的块use声明。

  • 对于类的打开大括号必须在下一行上,并且关闭大括号必须
    在尸体后继续下一行。

  • 对于方法的打开大括号必须在下一行上,并且关闭大括号必须
    在尸体后继续下一行。

  • 必须在所有属性和方法上声明可视性;abstract
    final必须在能见度之前声明;static必须声明
    能见度之后。

  • 控制结构关键字必须有一个空格后;方法和
    函数调用必须不。

  • 控制结构的打开大括号必须在同一条线上,并关闭
    支架必须在身体后的下一行上进行。

  • 控制结构的打开括号必须在它们之后没有空间,
    对于控制结构的闭合括号必须没有空间。

1.1.例

本例包含以下一些规则,作为一个快速概述:

<?php
namespace Vendor\Package;

use FooInterface;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;

class Foo extends Bar implements FooInterface
{
    public function sampleMethod($a $b = null)
    {
        if ($a === $b) {
            bar();
        } elseif ($a > $b) {
            $foo->bar($arg1);
        } else {
            BazClass::bar($arg2 $arg3);
        }
    }

    final public static function bar()
    {
        // method body
    }
}

2.将军

2.1.基本编码标准

代码必须遵循以下所有规则psr-1

2.2.文件

所有php文件都必须使用unix lf(Linefeed)行结束。

所有php文件都必须以一个空行结束。

闭幕式?>标记必须从包含php的文件中删除。

2.3.线

在线路长度上不得有硬性限制。

线路长度的软限制必须是120字符;自动样式检查器
必须警告但不要在软限制下出错。

行不应超过80个字符;行长度大于该字符
将每个字符分成多个后续行,每个字符不超过80个字符。

在非空行的末尾,不得有尾随空格。

可以添加空行以提高可读性并说明相关内容
代码块。

每行不能超过一个语句。

2.4.缩进

代码必须使用4个空格的缩进,并且不能使用制表符缩进。

使用空格,而不是将空格与制表符混合,有助于避免
有diffs、补丁、历史和注释的问题。空间的使用
也使得插入细粒度子压缩进线变得容易
对齐。

2.5.关键词与真/假/空

PHP关键词一定是在下个案子里。

php常量truefalse还有null一定是在下个案子里。

3.命名空间和使用声明

当出现时,必须有一个空行之后namespace声明。

当礼物出现时,所有use声明必须在namespace
声明。

一定有一个use关键字每声明。

在此之后必须有一个空行。use封锁。

例如:

<?php
namespace Vendor\Package;

use FooClass;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;

// ... additional PHP code ...

4.类、属性和方法

“类”这个词指的是所有类、接口和特性。

4.1.扩展和实现

extendsimplements关键字必须以相同的行声明为
班级名字。

上课的开口支撑必须按自己的路线;闭合支撑
因为上课必须在身体后的下一行上进行。

<?php
namespace Vendor\Package;

use FooClass;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;

class ClassName extends ParentClass implements \ArrayAccess \Countable
{
    // constants, properties, methods
}

清单implements可能拆分为多行,每个行都是
后续行缩进一次。当这样做时,列表中的第一个项
必须位于下一行,并且每行必须只有一个接口。

<?php
namespace Vendor\Package;

use FooClass;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;

class ClassName extends ParentClass implements
    \ArrayAccess
    \Countable
    \Serializable
{
    // constants, properties, methods
}

4.2.属性

必须在所有属性上声明可见性。

var关键字不能用于声明属性。

每个语句中不能声明多于一个属性。

属性名称不应以单个下划线为前缀,以表示
保护或私人能见度。

属性声明如下所示。

<?php
namespace Vendor\Package;

class ClassName
{
    public $foo = null;
}

4.3.方法

必须在所有方法中声明可见性。

方法名称不应以单个下划线为前缀,以表示
保护或私人能见度。

方法名称不能在方法名称之后使用空格来声明。大
打开支撑必须靠自己的线,并且关闭支撑必须在
下一行在尸体后面。打开后一定没有空间
括号,并且在结束括号之前必须没有空格。

方法声明如下所示。注意放置
圆括号、逗号、空格和大括号:

<?php
namespace Vendor\Package;

class ClassName
{
    public function fooBarBaz($arg1 &$arg2 $arg3 = [])
    {
        // method body
    }
}

4.4.方法参数

在参数列表中,在每个逗号之前都不能有空格,并且在这里
每个逗号都必须是一个空格。

方法参数的结尾必须在参数结束时进行
名单。

<?php
namespace Vendor\Package;

class ClassName
{
    public function foo($arg1 &$arg2 $arg3 = [])
    {
        // method body
    }
}

参数列表可以拆分到多个行中,每个行中每个行
缩进一次。当这样做时,列表中的第一个项必须位于
下一行,每行只需一个参数。

当参数列表拆分为多行时,则关闭括号
而打开支撑必须放在自己的线上,并有一个空间
他们之间。

<?php
namespace Vendor\Package;

class ClassName
{
    public function aVeryLongMethodName(
        ClassTypeHint $arg1
        &$arg2
        array $arg3 = []
    ) {
        // method body
    }
}

4.5.abstractfinal还有static

当出现时,abstractfinal声明必须先于
能见度声明。

当出现时,static声明必须在能见度之后才会出现
声明。

<?php
namespace Vendor\Package;

abstract class ClassName
{
    protected static $foo;

    abstract protected function zim();

    final public static function bar()
    {
        // method body
    }
}

4.6.方法和函数调用

在创建方法或函数调用时,必须在
方法或函数名和打开括号,必须没有空格。
在打开括号之后,在
闭合括号。在参数列表中,必须先没有空格
每个逗号,每个逗号之后都必须有一个空格。

<?php
bar();
$foo->bar($arg1);
Foo::bar($arg2 $arg3);

参数列表可以拆分到多个行中,每个行中每个行
缩进一次。当这样做时,列表中的第一个项必须位于
下一行,每行只需一个参数。

<?php
$foo->bar(
    $longArgument
    $longerArgument
    $muchLongerArgument
);

5.控制结构

控制结构的一般样式规则如下:

  • 控件结构关键字之后必须有一个空格。
  • 在打开括号之后,必须没有空格。
  • 在截止括号之前,必须没有空格。
  • 在闭合括号和开口之间必须有一个空格
    支柱
  • 结构体必须一次缩进
  • 关闭支架必须在身体后的下一行

每个结构的身体必须用大括号括起来。这标准化了如何
结构看起来,并且减少了将错误引入新的可能性
线被添加到身体里。

5.1.ifelseifelse

if结构看起来像下面的。注意括号的位置,
空格,大括号;以及elseelseif
从早期尸体上关闭支架。

<?php
if ($expr1) {
    // if body
} elseif ($expr2) {
    // elseif body
} else {
    // else body;
}

关键词elseif应该使用代替else if这样所有的控制
关键词看起来像单个单词。

5.2.switchcase

switch结构看起来像下面的。注意放置
括号、空格和大括号。大case语句必须有一次缩进
switch以及break关键字(或其他终止关键字)必须是
缩进与case尸体。必须有一个评论,例如
// no break当跌倒是故意在一个非空的case尸体。

<?php
switch ($expr) {
    case 0
        echo 'First case, with a break';
        break;
    case 1
        echo 'Second case, which falls through';
        // no break
    case 2
    case 3
    case 4
        echo 'Third case, return instead of break';
        return;
    default
        echo 'Default case';
        break;
}

5.3.whiledo while

while语句看起来如下所示。注意放置
括号、空格和大括号。

<?php
while ($expr) {
    // structure body
}

类似地,ado while语句看起来如下所示。注意放置
括号、空格和大括号。

<?php
do {
    // structure body;
} while ($expr);

5.4.for

for语句看起来如下所示。注意括号的位置,
空格和大括号。

<?php
for ($i = 0; $i < 10; $i++) {
    // for body
}

5.5.foreach

foreach语句看起来如下所示。注意放置
括号、空格和大括号。

<?php
foreach ($iterable as $key => $value) {
    // foreach body
}

5.6.trycatch

try catch块看起来像下面的。注意放置
括号、空格和大括号。

<?php
try {
    // try body
} catch (FirstExceptionType $e) {
    // catch body
} catch (OtherExceptionType $e) {
    // catch body
}

6.闭包

闭包必须在下列情况下使用空格声明function关键字和
空间前后的空间use关键字。

打开支架必须按同一条线进行,并且关闭支撑必须继续
身体后面的下一行。

在参数列表的打开括号之后,必须没有空格。
或者变量列表,在结束括号之前必须没有空格。
参数列表或变量列表。

在参数列表和变量列表中,必须在每个空格之前都没有空格。
逗号,每个逗号都必须有一个空格。

带有默认值的闭包参数必须在参数结束时进行
名单。

闭包声明如下所示。注意放置
圆括号、逗号、空格和大括号:

<?php
$closureWithArgs = function ($arg1 $arg2) {
    // body
};

$closureWithArgsAndVars = function ($arg1 $arg2) use ($var1 $var2) {
    // body
};

参数列表和变量列表可以跨多行拆分,其中
每一行都缩进一次。这样做时,第一个项目
列表必须位于下一行,并且必须只有一个参数或变量
每一行。

当结束列表(无论参数或变量)是否拆分为
多行,必须放置闭合括号和开口大括号
他们各自在自己的线上,在他们之间有一个空间。

下面是包含和没有参数列表的闭包的示例,以及
变量列表拆分跨多行。

<?php
$longArgs_noVars = function (
    $longArgument
    $longerArgument
    $muchLongerArgument
) {
    // body
};

$noArgs_longVars = function () use (
    $longVar1
    $longerVar2
    $muchLongerVar3
) {
    // body
};

$longArgs_longVars = function (
    $longArgument
    $longerArgument
    $muchLongerArgument
) use (
    $longVar1
    $longerVar2
    $muchLongerVar3
) {
    // body
};

$longArgs_shortVars = function (
    $longArgument
    $longerArgument
    $muchLongerArgument
) use ($var1) {
    // body
};

$shortArgs_longVars = function ($arg) use (
    $longVar1
    $longerVar2
    $muchLongerVar3
) {
    // body
};

注意,当关闭使用闭包时,格式规则也会适用。
作为参数调用函数或方法调用。

<?php
$foo->bar(
    $arg1
    function ($arg2) use ($var1) {
        // body
    },
    $arg3
);

7.结论

有许多风格和实践的元素有意省略
向导。这些包括但不限于:

  • 全局变量的声明和全局常数

  • 职能宣言

  • 运算符和赋值

  • 线间比对

  • 注释和文档块

  • 类名前缀和后缀

  • 最佳做法

今后的建议可修订并延长本指南以解决这些问题或
其他风格和实践的元素。

附录a.调查

在撰写此风格指南时,小组对会员项目进行了调查
确定共同做法。本调查保留于本章供后人使用。

A.1.调查数据

url,http://www.horde.org/apps/horde/docs/CODING_STANDARDS,http://pear.php.net/manual/en/standards.php,http://solarphp.com/manual/appendix-standards.style,http://framework.zend.com/manual/en/coding-standard.html,http://symfony.com/doc/2.0/contributing/code/standards.html,http://www.ppi.io/docs/coding-standards.html,https://github.com/ezsystems/ezp-next/wiki/codingstandards,http://book.cakephp.org/2.0/en/contributing/cakephp-coding-conventions.html,https://github.com/UnionOfRAD/lithium/wiki/Spec%3A-Coding,http://drupal.org/coding-standards,http://code.google.com/p/sabredav/,http://area51.phpbb.com/docs/31x/coding-guidelines.html,https://docs.google.com/a/zikula.org/document/edit?authkey=CPCU0Us&hgd=1&id=1fcqb93Sn-hR9c0mkN6m_tyWnmEvoswKBtSc0tKkZmJA,http://www.chisimba.com,n/a,https://github.com/Respect/project-info/blob/master/coding-standards-sample.php,n/a,Object Calisthenics for PHP,http://doc.nette.org/en/coding-standard,http://flow3.typo3.org,https://github.com/propelorm/Propel2/wiki/Coding-Standards,http://developer.joomla.org/coding-standards.html
voting,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,no,no,no,?,yes,no,yes
indent_type,4,4,4,4,4,tab,4,tab,tab,2,4,tab,4,4,4,4,4,4,tab,tab,4,tab
line_length_limit_soft,75,75,75,75,no,85,120,120,80,80,80,no,100,80,80,?,?,120,80,120,no,150
line_length_limit_hard,85,85,85,85,no,no,no,no,100,?,no,no,no,100,100,?,120,120,no,no,no,no
class_names,studly,studly,studly,studly,studly,studly,studly,studly,studly,studly,studly,lower_under,studly,lower,studly,studly,studly,studly,?,studly,studly,studly
class_brace_line,next,next,next,next,next,same,next,same,same,same,same,next,next,next,next,next,next,next,next,same,next,next
constant_names,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper
true_false_null,lower,lower,lower,lower,lower,lower,lower,lower,lower,upper,lower,lower,lower,upper,lower,lower,lower,lower,lower,upper,lower,lower
method_names,camel,camel,camel,camel,camel,camel,camel,camel,camel,camel,camel,lower_under,camel,camel,camel,camel,camel,camel,camel,camel,camel,camel
method_brace_line,next,next,next,next,next,same,next,same,same,same,same,next,next,same,next,next,next,next,next,same,next,next
control_brace_line,same,same,same,same,same,same,next,same,same,same,same,next,same,same,next,same,same,same,same,same,same,next
control_space_after,yes,yes,yes,yes,yes,no,yes,yes,yes,yes,no,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes
always_use_control_braces,yes,yes,yes,yes,yes,yes,no,yes,yes,yes,no,yes,yes,yes,yes,no,yes,yes,yes,yes,yes,yes
else_elseif_line,same,same,same,same,same,same,next,same,same,next,same,next,same,next,next,same,same,same,same,same,same,next
case_break_indent_from_switch,0/1,0/1,0/1,1/2,1/2,1/2,1/2,1/1,1/1,1/2,1/2,1/1,1/2,1/2,1/2,1/2,1/2,1/2,0/1,1/1,1/2,1/2
function_space_after,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no
closing_php_tag_required,no,no,no,no,no,no,no,no,yes,no,no,no,no,yes,no,no,no,no,no,yes,no,no
line_endings,LF,LF,LF,LF,LF,LF,LF,LF,?,LF,?,LF,LF,LF,LF,?,,LF,?,LF,LF,LF
static_or_visibility_first,static,?,static,either,either,either,visibility,visibility,visibility,either,static,either,?,visibility,?,?,either,either,visibility,visibility,static,?
control_space_parens,no,no,no,no,no,no,yes,no,no,no,no,no,no,yes,?,no,no,no,no,no,no,no
blank_line_after_php,no,no,no,no,yes,no,no,no,no,yes,yes,no,no,yes,?,yes,yes,no,yes,no,yes,no
class_method_control_brace,next/next/same,next/next/same,next/next/same,next/next/same,next/next/same,same/same/same,next/next/next,same/same/same,same/same/same,same/same/same,same/same/same,next/next/next,next/next/same,next/same/same,next/next/next,next/next/same,next/next/same,next/next/same,next/next/same,same/same/same,next/next/same,next/next/next

A.2.调查传奇

indent_type
缩进类型。tab=“使用制表符”,24=“空格数”

line_length_limit_soft
“软”线长度限制,字符。?=不可辨别或没有反应no意味着没有限制。

line_length_limit_hard
“硬”线长度限制,字符。?=不可辨别或没有反应no意味着没有限制。

class_names
类是如何命名的。lower=小写字母,lower_under=小写字母,下划线分隔符,studly=StudlyCase。

class_brace_line
类的打开大括号是否继续same行作为类关键字,或在next之后行吗?

constant_names
类常量是如何命名的?upper=Uppercase带下划线分隔符。

true_false_null
truefalse还有null拼写为所有拼写lower案件,或全部upper案子?

method_names
方法是怎样命名的?camel=camelCaselower_under=小写字母与下划线分隔符。

method_brace_line
方法的打开大括号是否继续same行作为方法名称,或在next排队?

control_brace_line
控件结构的打开支架是否继续same线,或在next排队?

control_space_after
控件结构关键字之后是否有空格?

always_use_control_braces
控制结构总是使用大括号吗?

else_elseif_line
使用时elseelseif它是否继续same线作为前一个关闭支柱,或者它是在next排队?

case_break_indent_from_switch
多少次?casebreak从开口缩进switch口供?

function_space_after
函数调用是否在函数名之后有空格,然后在打开括号之前?

closing_php_tag_required
在包含php的文件中,是关闭?>标签需要吗?

line_endings
什么类型的线结束使用?

static_or_visibility_first
当声明方法时,static先来,还是先来?

control_space_parens
在控制结构表达式中,是否存在打开括号之后的空格和关闭括号之前的空格?yes=if ( $expr )no=if ($expr)

blank_line_after_php
在打开php标记之后是否有空行?

class_method_control_brace
关于类、方法和控件结构的打开花括号所做的一行摘要。

A.3.调查结果

indent_type:
    tab: 7
    2: 1
    4: 14
line_length_limit_soft:
    ?: 2
    no: 3
    75: 4
    80: 6
    85: 1
    100: 1
    120: 4
    150: 1
line_length_limit_hard:
    ?: 2
    no: 11
    85: 4
    100: 3
    120: 2
class_names:
    ?: 1
    lower: 1
    lower_under: 1
    studly: 19
class_brace_line:
    next: 16
    same: 6
constant_names:
    upper: 22
true_false_null:
    lower: 19
    upper: 3
method_names:
    camel: 21
    lower_under: 1
method_brace_line:
    next: 15
    same: 7
control_brace_line:
    next: 4
    same: 18
control_space_after:
    no: 2
    yes: 20
always_use_control_braces:
    no: 3
    yes: 19
else_elseif_line:
    next: 6
    same: 16
case_break_indent_from_switch:
    0/1: 4
    1/1: 4
    1/2: 14
function_space_after:
    no: 22
closing_php_tag_required:
    no: 19
    yes: 3
line_endings:
    ?: 5
    LF: 17
static_or_visibility_first:
    ?: 5
    either: 7
    static: 4
    visibility: 6
control_space_parens:
    ?: 1
    no: 19
    yes: 2
blank_line_after_php:
    ?: 1
    no: 13
    yes: 8
class_method_control_brace:
    next/next/next: 4
    next/next/same: 11
    next/same/same: 1
    same/same/same: 6
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值