Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic.Arrays.ArrayIndent can request close brace indent to be less than the statement indent level #2882

Closed
morozov opened this issue Feb 27, 2020 · 3 comments
Milestone

Comments

@morozov
Copy link
Contributor

morozov commented Feb 27, 2020

Originally, this issue was found when using the Doctrine Coding Standard but as it turned out in slevomat/coding-standard#913, it’s reproducible with PHP_CodeSniffer as well.

Consider the following code example and standard configuration:

<?php

$foo =
[
    'bar' =>
   [
    ],
];
<?xml version="1.0"?>
<ruleset>
    <rule ref="Generic.WhiteSpace.ScopeIndent">
        <properties>
            <property name="ignoreIndentationTokens" type="array">
                <element value="T_COMMENT"/>
                <element value="T_DOC_COMMENT_OPEN_TAG"/>
            </property>
        </properties>
    </rule>
    <rule ref="Generic.Arrays.ArrayIndent"/>
</ruleset>

The following violations are reported:

-----------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------------------------------------
 7 | ERROR | [x] Array close brace not indented correctly; expected 3 spaces but found 4
-----------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------------

An attempt to auto-fix the file fails:

PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE                                                  FIXED  REMAINING
----------------------------------------------------------------------
/home/morozov/Projects/dbal/test.php                  FAILED TO FIX
----------------------------------------------------------------------
A TOTAL OF 0 ERRORS WERE FIXED IN 1 FILE
----------------------------------------------------------------------
PHPCBF FAILED TO FIX 1 FILE
----------------------------------------------------------------------

And introduces another violation (both lines 6 and 7 are now indented using 3 spaces instead of 4):

<?php

$foo =
[
    'bar' =>
   [
   ],
];

An attempt to fix the modified file also fails. Note that fixing the file using --standard=Squiz (as per the reported violations) succeeds:

$ phpcbf --standard=Squiz test.php

PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE                                                  FIXED  REMAINING
----------------------------------------------------------------------
/home/morozov/Projects/dbal/test.php                  4      1
----------------------------------------------------------------------
A TOTAL OF 4 ERRORS WERE FIXED IN 1 FILE
----------------------------------------------------------------------
<?php

$foo = [
    'bar' => [],
];
@gsherwood
Copy link
Member

The core issue with this one appears to be that the open bracket of the array needs to be indented to at least the level of the start of the statement. In the code sample, it's indented 3 spaces instead of 4, which causes the conflicts. I'm going to look at changing the ArrayIndent sniff to enforce this base indent, but not a specific indent level.

Note that the Squiz standard works fine because it has additional rules that force the array open brace to be on the same line as the start of the statement, so the indent is always calculated correctly.

@gsherwood gsherwood changed the title Unable to fix a trivial indentation issue Generic.Arrays.ArrayIndent can request close brace indent to be less than the statement indent level Aug 19, 2020
gsherwood added a commit that referenced this issue Aug 19, 2020
…indent to be less than the statement indent level
gsherwood added a commit that referenced this issue Aug 19, 2020
@gsherwood
Copy link
Member

The change described above has fixed this issue without impacting any existing tests. Thanks for reporting this issue.

@morozov
Copy link
Contributor Author

morozov commented Aug 20, 2020

It works well on master:

$ bin/phpcbf --standard=standard.xml test.php

PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE                                                  FIXED  REMAINING
----------------------------------------------------------------------
/home/morozov/Projects/php-code-sniffer/test.php      1      0
----------------------------------------------------------------------
A TOTAL OF 1 ERROR WERE FIXED IN 1 FILE
----------------------------------------------------------------------

Time: 21ms; Memory: 4MB
diff --git a/test.php b/test.php
index 126663e5a..5cdf6591d 100644
--- a/test.php
+++ b/test.php
@@ -3,6 +3,6 @@
 $foo =
 [
     'bar' =>
-   [
+    [
     ],
 ];

Thanks, @gsherwood!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants