Domanda

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?

È stato utile?

Soluzione

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 ...
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top