从phpcs修复"行缩进错误"错误

Wil*_*ken 2 php whitespace coding-style

我使用phpcs验证PHP代码:

phpcs --standard=PSR1 .
Run Code Online (Sandbox Code Playgroud)

它产生的输出充满了:

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
...
Run Code Online (Sandbox Code Playgroud)

我尝试使用php-cs- fixer 修复此问题,但是它们不支持lexing并正确设置缩进,因此它只能转换标签.请参阅:https://github.com/fabpot/PHP-CS-Fixer/issues/229

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

Gre*_*ood 6

首先,知道那些缩进错误来自您的PSR2运行而不是PSR1运行可能会很好.PSR2包含来自PSR1的所有检查,因此您实际上不需要执行2次PHPCS运行.如果要同时使用--standard = PSR2,则可以使用它们.

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

如果我随后使用新的diff报告运行PHPCS,它将显示需要对该文件进行哪些更改,包括此代码段:

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;
         }
Run Code Online (Sandbox Code Playgroud)

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

phpcbf --standard=PSR2 /path/to/file
Patched 1 files
Time: 78 ms, Memory: 4.50Mb
Run Code Online (Sandbox Code Playgroud)

你可以在这里阅读更多相关信息: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 ...
Run Code Online (Sandbox Code Playgroud)