Skip to content

Scope indent calculated incorrectly when using array destructuring #1392

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

Closed
franfran3 opened this issue Mar 17, 2017 · 1 comment
Closed

Comments

@franfran3
Copy link

When destructuring an array (PHP 7.1) , false positives are triggered.

<?php
// code1.php
function test()
{
    $array = [];
    foreach ($array as $data) {
        [
            'key1' => $var1,
            'key2' => $var2,
        ] = $data;
        foreach ($var1 as $method) {
            echo $method . $var2;
        }
    }
}

Running phpcs 2.8.1 against the file will report an unexpected indent violation:

$ phpcs --standard=PSR2 --encoding=utf-8 --colors --report=full code.php 

FILE: /home/fmusy/www/projects/app-tests/t/code.php
----------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
----------------------------------------------------------------------
 11 | ERROR | [x] Line indented incorrectly; expected 0 spaces, found
    |       |     8
 13 | ERROR | [x] Line indented incorrectly; expected 0 spaces, found
    |       |     8
----------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 26ms; Memory: 4Mb

If destructuration is done in one line, no violation is reported.

...
  ['key1' => $var1, 'key2' => $var2] = $data;
...

If destructuration is done with array keyword, phpcs reports an invalid withespace at EOL:

...
        array( 
            'key1' => $var1,
            'key2' => $var2
        ) = $data;
...

with output

$ phpcs --standard=PSR2 --encoding=utf-8 --colors --report=full code4.php 

FILE: /home/fmusy/www/projects/app-tests/t/code4.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 6 | ERROR | [x] Whitespace found at end of line
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 26ms; Memory: 4Mb
@gsherwood gsherwood changed the title Invalid handling of array destructuring Scope indent calculated incorrectly when using array destructuring Mar 19, 2017
gsherwood added a commit that referenced this issue Mar 19, 2017
@gsherwood
Copy link
Member

The issue was caused by the T_WHITESPACE token setting the indent, when it never should. This was due to an assumption in the sniff that a square bracket would never be the first piece of code in a statement.

Debug output was:

Start with token 0 on line 1 with indent 0
Closing parenthesis found on line 2
	 * ignoring single-line definition *
Open scope (T_FUNCTION) on line 3
	=> indent set to 4 by token 7 (T_OPEN_CURLY_BRACKET)
Closing short array bracket found on line 4
	 * ignoring single-line definition *
Closing parenthesis found on line 5
	 * ignoring single-line definition *
Open scope (T_FOREACH) on line 5
	=> indent set to 8 by token 29 (T_OPEN_CURLY_BRACKET)
Closing short array bracket found on line 9
	* first token on line 6 is 32 (T_OPEN_SHORT_ARRAY) *
	=> checking indent of 8; main indent set to 0 by token 31 (T_WHITESPACE)
[Line 10] Line indented incorrectly; expected 0 spaces, found 8
	=> Add adjustment of -8 for token 59 (T_FOREACH) on line 10
Closing parenthesis found on line 10
	 * ignoring single-line definition *
Open scope (T_FOREACH) on line 10
	=> indent set to 4 by token 69 (T_OPEN_CURLY_BRACKET)
Indent adjusted to 4 for T_ECHO on line 11
	=> Add adjustment of -8 for token 72 (T_ECHO) on line 11
Close scope (T_FOREACH) on line 12
	=> indent set to 0 by token 82 (T_CLOSE_CURLY_BRACKET)
[Line 12] Line indented incorrectly; expected 0 spaces, found 8
	=> Add adjustment of -8 for token 82 (T_CLOSE_CURLY_BRACKET) on line 12
Close scope (T_FOREACH) on line 13
	=> indent set to 4 by token 85 (T_CLOSE_CURLY_BRACKET)
Close scope (T_FUNCTION) on line 14
	=> indent set to 0 by token 87 (T_CLOSE_CURLY_BRACKET)

This is now fixed. Thanks for reporting it.

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