在合作开发时需要统一下code style
,如给method写annotation时一些tag如@params ,@throw,@return得按照字母顺序来写,等等。推荐一个package:friendsofphp/php-cs-fixer
,该package是Symfony作者写的,质量有保证。
安装下该package:
composer require friendsofphp/php-cs-fixer --dev
然后项目根目录创建一个可执行文件如.cs文件:
#!/bin/bash
vendor/bin/php-cs-fixer fix
以后在项目根目录只需执行./cs
就可以自动修复不复合规定code style的代码,code rules的定义在该package packgist的网站上有说明。vendor/bin/php-cs-fixer fix
会读取.php_cs
文件返回的code rules,所以还得定义哪些code rules,同样在项目根目录中新建文件.php_cs
,然后加上code rules,如:
<?php
$finder = Symfony\CS\Finder\DefaultFinder::create()
->exclude('bootstrap')
->exclude('database')
->exclude('public')
->exclude('resources')
->exclude('storage')
->exclude('vendor')
->notPath('.phpstorm.meta.php')
->notPath('_ide_helper.php')
->notPath('server.php')
->in(__DIR__);
return Symfony\CS\Config::create()
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
->fixers([
// Use all symfony fixers but the following '-'.'fixer'.
// Exclude psr-0
'-psr0',
// Concatenation should be used without spaces.
'-concat_without_spaces',
// A return statement wishing to return nothing should be simply "return".
'-empty_return',
// Remove useless semicolon statements.
'-no_empty_comment',
// Binary operators should be surrounded by at least one space.
'-operators_spaces',
// Phpdocs annotation descriptions should not end with a full stop.
'-phpdoc_annotation_without_dot',
// @return void and @return null annotations should be omitted from phpdocs.
'-phpdoc_no_empty_return',
// @package and @subpackage annotations should be omitted from phpdocs.
'-phpdoc_no_package',
// All items of the @param, @throws, @return, @var, and @type phpdoc tags must be aligned vertically.
'-phpdoc_params',
// Annotations in phpdocs should be grouped together so that annotations of the same type immediately follow each other,
// and annotations of a different type are separated by a single blank line.
'-phpdoc_separation',
// Phpdocs short descriptions should end in either a full stop, exclamation mark, or question mark.
'-phpdoc_short_description',
// Docblocks should only be used on structural elements.
'-phpdoc_to_comment',
// Pre incrementation/decrementation should be used if possible.
'-pre_increment',
// Unalign double arrow symbols.
'-unalign_double_arrow',
// Unalign equals symbols.
'-unalign_equals',
// Use all the following fixers.
// Align double arrow symbols in consecutive lines.
'align_double_arrow',
// Align equals symbols in consecutive lines.
'align_equals',
// Concatenation should be used with at least one whitespace around.
'concat_with_spaces',
// Replace deprecated ereg regular expression functions with preg.
'ereg_to_preg',
// Add, replace or remove header comment.
// 'header_comment',
// Multi-line whitespace before closing semicolon are prohibited.
'multiline_spaces_before_semicolon',
// Ensure there is no code on the same line as the PHP open tag.
'newline_after_open_tag',
// There should not be an empty return statement at the end of a function.
'no_useless_return',
// Ordering use statements.
'ordered_use',
// Convert PHP4-style constructors to __construct.
'php4_constructor',
// PHPUnit assertion method calls like "->assertSame(true, $foo)" should be written with dedicated method like "->assertTrue($foo)".
'php_unit_construct',
// PHPUnit methods like "assertSame" should be used instead of "assertEquals".
'php_unit_strict',
// Annotations in phpdocs should be ordered so that param annotations come first,
// then throws annotations, then return annotations.
'phpdoc_order',
// PHP arrays should use the PHP 5.4 short-syntax.
'short_array_syntax',
// Replace short-echo <?= with long format <?php echo syntax.
'short_echo_tag',
// Comparison should be strict.
'strict',
// Functions should be used with $strict param.
'strict_param',
// Remove trailing whitespace at the end of blank lines.
'whitespacy_lines',
])
->finder($finder)
->setUsingCache(true);
code rules可以添加或删除,需要团队统一,一般至少尽量符合PSR-2的大部分标准。每一个code rule的定义可以参考package packgist的主页。
这样,团队里以后每次push code前先./cs
下:
当然,还应当在PHPStorm IDE里的Preference->Editor->Code Style->PHP
里也设置同样的code rules,然后导出一个xml文件,方便在团队里共享并导入到每个开发者的PHPStorm,这样保证团队的每一个Code Style
保持相同。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。