【问题标题】:Fixing "Line indented incorrectly" error from phpcs修复 phpcs 中的“行缩进错误”错误
【发布时间】:2014-05-01 07:15:34
【问题描述】:

我正在使用phpcs 验证 PHP 代码:

phpcs --standard=PSR1 .

它会产生this output,上面到处都是:

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
...

我尝试使用php-cs-fixer 解决此问题,但是它们不支持词法分析和正确设置缩进,因此它只能转换制表符。见:https://github.com/fabpot/PHP-CS-Fixer/issues/229

既然 phpcs 自信地告诉我需要多少个空格,我有没有办法纠正整个项目所需的缩进?

【问题讨论】:

    标签: php coding-style whitespace


    【解决方案1】:

    首先,最好知道这些缩进错误来自您的 PSR2 运行,而不是 PSR1 运行。 PSR2 包含来自 PSR1 的所有检查,因此您实际上不需要执行 2 次 PHPCS 运行。如果你想同时遵守它们,你可以只使用 --standard=PSR2。

    至于修复,PHP_CodeSniffer 的当前 alpha 版本包含一个名为 phpcbf 的脚本,它可以为您自动修复错误,包括缩进问题。当我在您的一个文件(setup/upgrade/upgrade.php)上运行 PHP_CodeSniffer 的 alpha 版本时,我得到了 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
    --------------------------------------------------------------------------------
    

    如果我随后使用新的差异报告运行 PHPCS,它将显示需要对文件进行哪些更改,包括这个 sn-p:

    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;
             }
    

    如果要自动修复文件,请使用 phpcbf 命令而不是 phpcs 命令:

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

    您可以在此处阅读更多信息:https://github.com/squizlabs/PHP_CodeSniffer/wiki/Fixing-Errors-Automatically

    这是您想要获得的版本:https://github.com/squizlabs/PHP_CodeSniffer/releases/tag/2.0.0a1

    或者您可以克隆 Github 存储库并检查 phpcs-fixer 分支以获取最新代码。然后,您可以从克隆中运行 phpcs 和 phpcbf,而无需通过 Composer 的 PEAR 安装它们:

    git clone -b phpcs-fixer git://github.com/squizlabs/PHP_CodeSniffer.git
    cd PHP_CodeSniffer
    php scripts/phpcs ...
    php scripts/phpcbf ...
    

    【讨论】:

    • 谢谢,我很高兴看到这个新功能,并将关注您博客上的进展。仅供参考,我必须使用 ~/phpcsGIT/scripts/phpcbf --standard=PSR2 --sniffs=PSR2.Methods.FunctionCallSignature.Indent --report=diff *php */*php */*/*php */*/*/*php | patch -p4 才有效,但 phpcbf 中的相同命令给出“补丁输入中仅发现垃圾”。
    • 感谢您告诉我。我还没有调查任何补丁问题,但是 phpcbf 也接受 --no-patch 命令行参数来简化生活,只需使用 PHP 覆盖文件,这可以解决大多数补丁问题(和 Windows)。我将尝试运行您在存储库中提供的命令,而不使用管道进行修补,看看我是否可以复制问题。
    • 不幸遇到了和你一样的补丁问题:php scripts/phpcbf /path/to/cameralife/ --standard=PSR2 --sniffs=PSR2.Methods.FunctionCallSignature.Indent 导致 Patched 11 files Time: 2.62 seconds, Memory: 34.50Mb 和 git diff 看起来不错。
    猜你喜欢
    • 1970-01-01
    • 2018-10-09
    • 2021-08-11
    • 1970-01-01
    • 2019-04-07
    • 2022-11-21
    • 2019-08-25
    • 1970-01-01
    • 2022-06-10
    相关资源
    最近更新 更多