2013-01-16

【程式】PHP : PHP Coding Standards Fixer


PHP Coding Standards Fixer
The PHP Coding Standards Fixer tool fixes most issues in your code when you want to follow the PHP coding standards as defined in the PSR-1 and PSR-2 documents.
這東西,真的超好用的。
他會把多餘的空白過濾掉,Tab 用4個空白取代,幫你把括弧排整齊 ~~ blah blah ~~
簡單來說,就是美化你的程式碼啦。

安裝部份,可以看官方網站就好啦。
但記得 PHP 在 Compile 時,要加上 enable-phar,否則會不能執行

下面簡單的記錄一下

安裝
$ wget http://cs.sensiolabs.org/get/php-cs-fixer.phar -O /usr/local/bin/php-cs-fixer
$ chmod a+x /usr/local/bin/php-cs-fixer

更新
$ php-cs-fixer self-update

使用方式
$ php-cs-fixer fix /path/to/dir
$ php-cs-fixer fix /path/to/file

可以使用指定 level 的方式
$ php-cs-fixer fix /path/to/project --level=psr0
$ php-cs-fixer fix /path/to/project --level=psr1
$ php-cs-fixer fix /path/to/project --level=psr2
$ php-cs-fixer fix /path/to/project --level=all

也可以指定 fixers 的方式
$ php-cs-fixer fix /path/to/dir --fixers=linefeed,short_tag,indentation

fixer 可帶的參數如下
  • indentation [PSR-2] Code must use 4 spaces for indenting, not tabs.
  • linefeed [PSR-2] All PHP files must use the Unix LF (linefeed) line ending.
  • trailing_spaces [PSR-2] Remove trailing whitespace at the end of lines.
  • unused_use [all] Unused use statements must be removed.
  • php_closing_tag [PSR-2] The closing ?> tag MUST be omitted from files containing only PHP.
  • short_tag [PSR-1] PHP code must use the long tags or the short-echo tags; it must not use the other tag variations.
  • return [all] An empty line feed should precede a return statement.
  • visibility [PSR-2] Visibility must be declared on all properties and methods; abstract and final must be declared before the visibility; static must be declared after the visibility.
  • braces [PSR-2] Opening braces for classes and methods must go on the next line, and closing braces must go on the next line after the body. Opening braces for control structures must go on the same line, and closing braces must go on the next line after the body.
  • phpdoc_params [all] All items of the @param phpdoc tags must be aligned vertically.
  • eof_ending [PSR-2] A file must always end with an empty line feed.
  • extra_empty_lines [all] Removes extra empty lines.
  • include [all] Include and file path should be devided with single space. File path should not be placed under brackets.
  • psr0 [PSR-0] Classes must be in a path that matches their namespace, be at least one namespace deep, and the class name should match the file name.
  • controls_spaces [all] A single space should be between: the closing brace and the control, the control and the opening parenthese, the closing parenthese and the opening brace.
  • elseif [PSR-2] The keyword elseif should be used instead of else if so that all control keywords looks like single words.
我是使用 fixer ,指定我要的,但也幾乎都指定了
沒指定的只有 return、psr0、include

本來想用 return [all] An empty line feed should precede a return statement. 的。
但如果程式碼是
if ($a=='1')
    return 1;

他就會變成
if ($a=='1')

    return 1;
所以我就沒有加上 return 啦。

大概就是這樣,是一個很好用的東西。


其它資料
Maintainable PHP Source Code 相當不錯,很有參考價值
PSR-0     PSR-1     PSR-2


補充:
可以在 /etc/bash.bashrc 加上
fix() {
    php-cs-fixer fix --fixers=indentation,linefeed,trailing_spaces "${@}"
}
這樣子,只要執行
$ fix path/to/dir or file
就可以執行啦,不用再打一堆參數。

0 comments:

張貼留言