PHP-CS-Fixer / PHP-CS-Fixer

A tool to automatically fix PHP Coding Standards issues
https://cs.symfony.com
MIT License
12.86k stars 1.58k forks source link

braces: Incorrect indentation for “if-then” with only a comment #6478

Closed TimWolla closed 2 years ago

TimWolla commented 2 years ago

Bug report

PHP-CS-Fixer incorrectly indents (or rather unindents) the comment within the “then” part of the if of the example code:

$ phpcs-fixer fix -vvv --diff --dry-run test.php
PHP CS Fixer 3.9.2 (9b23c42) Grand Awaiting by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 8.0.21
Loaded config default.
F                                                                                                                                                                                                      1 / 1 (100%)
Legend: ?-unknown, I-invalid file syntax (file ignored), S-skipped (cached or empty file), .-no changes, F-fixed, E-error
   1) test.php (braces)
      ---------- begin diff ----------
--- /pwd/test.php
+++ /pwd/test.php
@@ -3,7 +3,7 @@
 function test()
 {
     if (true) {
-        // do nothing
+    // do nothing
     } else {
         \var_dump(1);
     }

      ----------- end diff -----------

Checked all files in 0.012 seconds, 12.000 MB memory used

No config used; test.php is the only file in the directory.

Code snippet that reproduces the problem

<?php

function test()
{
    if (true) {
        // do nothing
    } else {
        \var_dump(1);
    }
}
TimWolla commented 2 years ago

This effectively was already reported here: https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/6470#issuecomment-1180263054, as part of a larger comment. I believe this issue is still useful, as it is very narrow in scope and focusing on a single bad fix.