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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了