Php_codesniffer: Indentation false positive when closure containing nested IF conditions used as function argument

Created on 19 Mar 2019  路  6Comments  路  Source: squizlabs/PHP_CodeSniffer

foo('bar', function () {
    if ($conditionA &&
        $conditionB
    ) {
        if ($conditionC) {
            $a = 1;
        }
    }
});

After the merge of #2414 solution, the code above started generating the following errors from Generic.WhiteSpace.ScopeIndent:
5 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 8
7 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 8

Bug

Most helpful comment

I've committed a fix for this and tested with the code linked in the comments. Thanks for submitting so much to test - made it much easier to fix. This will be in 3.4.2, which I'll try and release quite soon.

All 6 comments

It's also failing for us with the following report:

FILE: ...race-php/src/DDTrace/Integrations/Symfony/SymfonyIntegration.php
----------------------------------------------------------------------
FOUND 6 ERRORS AFFECTING 6 LINES
----------------------------------------------------------------------
 50 | ERROR | [x] Line indented incorrectly; expected 12 spaces,
    |       |     found 16
    |       |     (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
 52 | ERROR | [x] Line indented incorrectly; expected 12 spaces,
    |       |     found 16
    |       |     (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
 54 | ERROR | [x] Line indented incorrectly; expected 12 spaces,
    |       |     found 16
    |       |     (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
 59 | ERROR | [x] Line indented incorrectly; expected 12 spaces,
    |       |     found 16
    |       |     (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
 61 | ERROR | [x] Line indented incorrectly; expected 12 spaces,
    |       |     found 16
    |       |     (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
 69 | ERROR | [x] Line indented incorrectly; expected 12 spaces,
    |       |     found 16
    |       |     (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
----------------------------------------------------------------------
PHPCBF CAN FIX THE 6 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 1.61 secs; Memory: 12MB

Here are the lines in question. These tests passed when running version 3.4.0 but fail with version 3.4.1.

Debugged this a bit and found it is not related to the change in #2414 , which is also unreleased.

This was actually caused by the fix for #2396

I've committed a fix for this and tested with the code linked in the comments. Thanks for submitting so much to test - made it much easier to fix. This will be in 3.4.2, which I'll try and release quite soon.

Wow - that was fast! Thanks so much @gsherwood! :)

Was this page helpful?
0 / 5 - 0 ratings