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

Incorrect indent behavior using deep-nested function and arrays #945

Closed
stronk7 opened this issue Apr 5, 2016 · 3 comments
Closed

Incorrect indent behavior using deep-nested function and arrays #945

stronk7 opened this issue Apr 5, 2016 · 3 comments

Comments

@stronk7
Copy link
Contributor

stronk7 commented Apr 5, 2016

Recently, we upgraded to latest (master) phpcs version and after some discussions decided to move away from our own Indent sniff to the upstream generic one without modification (accepting its way for #907 and other considerations).

But it seems that have come with a unexpected surprise that we were not experimenting earlier. It happens where there are a number of nested functions, some of them including also some array.

For reference, we have tracked the problem @ https://tracker.moodle.org/browse/CONTRIB-6206

Basically, phpcs (generic indent sniff) considers this correct:

$somevariable = some_function(another_function($param1, $param2),
        more_function($param3, another_one(
                $key1, $value1,
                $key2, $value2)));
$continue = this_line_is_correct_and_works_ok();

But if I replace the another_one() function with an array declaration, everything becomes crazy since that point:

$somevariable = some_function(another_function($param1, $param2),
        more_function($param3, array(
                $key1, $value1,
                $key2, $value2)));
$continue = now_this_and_every_line_after_is_doomed();

And the situation becomes worse (crazier) if we face some construction like this (note I don't like/consider them correct, but shows the problem with arrays in an extreme way):

function read_competency_framework($id) {
    global $PAGE;

    $params = self::validate_parameters(self::read_competency_framework_parameters(),
                                        array(
                                            'id' => $id,
                                        ));

    $framework = api::read_framework($params['id']);
    self::validate_context($framework->get_context());
    $output = $PAGE->get_renderer('tool_lp');
}

(here it asks for a thousand (heh, 40) of chars for the rest of the file).

Ciao :-)

PS: I'll be investigating this along the week, will put any solution if I'm able, just wanted to have it reported asap.

@gsherwood
Copy link
Member

gsherwood commented Apr 27, 2016

Some debug output to show what is going on (just for my records):

First case

1: <?php
2: $somevariable = some_function(another_function($param1, $param2),
3:         more_function($param3, another_one(
4:                 $key1, $value1,
5:                 $key2, $value2)));
6: $continue = this_line_is_correct_and_works_ok();
$ phpcs temp.php --standard=Generic --sniffs=Generic.WhiteSpace.ScopeIndent --runtime-set scope_indent_debug 1
Start with token 0 on line 1 with indent 0

Second case

1: <?php
2: $somevariable = some_function(another_function($param1, $param2),
3:         more_function($param3, array(
4:                 $key1, $value1,
5:                 $key2, $value2)));
6: $continue = now_this_and_every_line_after_is_doomed();
$ phpcs temp.php --standard=Generic --sniffs=Generic.WhiteSpace.ScopeIndent --runtime-set scope_indent_debug 1
Start with token 0 on line 1 with indent 0
Closing parenthesis found on line 5
    * first token on line 3 is 17 (T_STRING) *
    => checking indent of 8; main indent set to 8 by token 17 (T_STRING)
[Line 6] Line indented incorrectly; expected at least 8 spaces, found 0
    => Add adjustment of 8 for token 42 (T_VARIABLE) on line 6

Third case

  1: <?php
 2: function read_competency_framework($id) {
 3:     global $PAGE;
 4: 
 5:     $params = self::validate_parameters(self::read_competency_framework_parameters(),
 6:                                         array(
 7:                                             'id' => $id,
 8:                                         ));
 9: 
10:     $framework = api::read_framework($params['id']);
11:     self::validate_context($framework->get_context());
12:     $output = $PAGE->get_renderer('tool_lp');
13: }
$ phpcs temp.php --standard=Generic --sniffs=Generic.WhiteSpace.ScopeIndent --runtime-set scope_indent_debug 1
Start with token 0 on line 1 with indent 0
Open scope (T_FUNCTION) on line 2
    => indent set to 4 by token 8 (T_OPEN_CURLY_BRACKET)
Closing parenthesis found on line 8
    * first token on line 6 is 34 (T_ARRAY) *
    => checking indent of 40; main indent set to 40 by token 34 (T_ARRAY)
[Line 10] Line indented incorrectly; expected at least 40 spaces, found 4
    => Add adjustment of 36 for token 52 (T_VARIABLE) on line 10
[Line 11] Line indented incorrectly; expected at least 40 spaces, found 4
    => Add adjustment of 36 for token 68 (T_SELF) on line 11
[Line 12] Line indented incorrectly; expected at least 40 spaces, found 4
    => Add adjustment of 36 for token 81 (T_VARIABLE) on line 12
Close scope (T_FUNCTION) on line 13
    => indent set to 0 by token 93 (T_CLOSE_CURLY_BRACKET)

@gsherwood gsherwood changed the title Strange behavior using deep-nested function and arrays Incorrect indent behavior using deep-nested function and arrays May 31, 2016
@gsherwood
Copy link
Member

This has been fixed now.

@stronk7
Copy link
Contributor Author

stronk7 commented Jun 9, 2016

Thanks, just confirming it fixed all our reported cases!

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