duanbai1027 2014-03-24 03:55
浏览 479
已采纳

从phpcs修复“行缩进错误”错误

I am validating PHP code with phpcs using:

phpcs --standard=PSR1 .

And it produces this output which is littered with:

FILE: /home/travis/build/fulldecent/cameralife/setup/upgrade/upgrade.php
--------------------------------------------------------------------------------
FOUND 7 ERROR(S) AND 1 WARNING(S) AFFECTING 8 LINE(S)
--------------------------------------------------------------------------------
 34 | ERROR   | Line indented incorrectly; expected 4 spaces, found 8
...

I tried to fix this with php-cs-fixer, however they do not support lexing and properly setting indentation, so it can only convert tabs. See: https://github.com/fabpot/PHP-CS-Fixer/issues/229

Since phpcs confidently tells me how many spaces are required, is there way I can correct to the required indentation for the entire project?

  • 写回答

1条回答 默认 最新

  • dongxian6653 2014-03-24 21:41
    关注

    First up, it might be good to know that those indent errors are coming from your PSR2 run and not the PSR1 run. PSR2 contains all of the checks from PSR1, so you don't actually need to do 2 PHPCS runs. You can just use --standard=PSR2 if you want to adhere to both of them.

    As for fixing, the current alpha release of PHP_CodeSniffer contains a script called phpcbf, which can fix errors automatically for you, including the indent issues. When I run the alpha version of PHP_CodeSniffer on one of your files (setup/upgrade/upgrade.php) I get this report for PSR2:

    phpcs --standard=PSR2 /path/to/file
    --------------------------------------------------------------------------------
    FOUND 8 ERRORS AND 1 WARNING AFFECTING 10 LINES
    --------------------------------------------------------------------------------
     34 | ERROR   | [x] Line indented incorrectly; expected 4 spaces, found 8
     36 | ERROR   | [x] Line indented incorrectly; expected 8 spaces, found 12
     40 | ERROR   | [x] Line indented incorrectly; expected 4 spaces, found 8
     43 | ERROR   | [x] Line indented incorrectly; expected 8 spaces, found 12
     47 | ERROR   | [x] Line indented incorrectly; expected 8 spaces, found 12
     51 | ERROR   | [x] Line indented incorrectly; expected 12 spaces, found 16
     52 | WARNING | [ ] Line exceeds 120 characters; contains 200 characters
     55 | ERROR   | [x] Line indented incorrectly; expected 4 spaces, found 8
     60 | ERROR   | [x] A closing tag is not permitted at the end of a PHP file
    --------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 8 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    --------------------------------------------------------------------------------
    

    If I then run PHPCS with the new diff report, it will show me what changes need to be made to the file, including this snippet:

    phpcs --standard=PSR2 --report=diff /path/to/file
    @@ -31,32 +31,29 @@
         if ($installed_version >= $latest_version) {
             echo "<p style=\"color:green\">No upgrade is necessary. Return to the <a href=\"../../\">main page</a>.</p>";
         } else {
    -        foreach (glob(dirname(__FILE__) . '/*.inc') as $script) {
    -            $a = basename($script, '.inc');
    -            if (is_numeric($a) && ($a > $installed_version) && ($a <= $latest_version)) {
    -                $scripts[] = $a;
    -            }
    +    foreach (glob(dirname(__FILE__) . '/*.inc') as $script) {
    +        $a = basename($script, '.inc');
    +        if (is_numeric($a) && ($a > $installed_version) && ($a <= $latest_version)) {
    +            $scripts[] = $a;
             }
    

    If you want the file fixed automatically, you use the phpcbf command instead of the phpcs command:

    phpcbf --standard=PSR2 /path/to/file
    Patched 1 files
    Time: 78 ms, Memory: 4.50Mb
    

    You can read more about this here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Fixing-Errors-Automatically

    And this is the release you are going to want to get: https://github.com/squizlabs/PHP_CodeSniffer/releases/tag/2.0.0a1

    Or you can clone the Github repo and checkout the phpcs-fixer branch to get the very latest code. You can then run phpcs and phpcbf from the clone without having to install them via PEAR of Composer:

    git clone -b phpcs-fixer git://github.com/squizlabs/PHP_CodeSniffer.git
    cd PHP_CodeSniffer
    php scripts/phpcs ...
    php scripts/phpcbf ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配