Skip to content

Inconsistent indentation check results when ELSE on new line #1255

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
morozov opened this issue Dec 28, 2016 · 2 comments
Closed

Inconsistent indentation check results when ELSE on new line #1255

morozov opened this issue Dec 28, 2016 · 2 comments

Comments

@morozov
Copy link
Contributor

morozov commented Dec 28, 2016

The following is a simplified example of a legacy file which I work with and try to validate the changes with PHP_CodeSniffer:

<?php

    if (true) {
    }
    else if (true) {
    }

$a = 1;

The if-else block is indented incorrectly (one level instead of none), the last line is not indented (which is correct). Here's the results which PHP_CodeSniffer produces for this file when checking against PSR2:

↪  scripts/phpcs --version
PHP_CodeSniffer version 2.7.2 (stable) by Squiz (http://www.squiz.net)

↪  scripts/phpcs ~/test.php --standard=PSR2

FILE: /home/morozov/test.php
----------------------------------------------------------------------------------
FOUND 4 ERRORS AND 1 WARNING AFFECTING 4 LINES
----------------------------------------------------------------------------------
 3 | ERROR   | [x] Line indented incorrectly; expected 0 spaces, found 4
 4 | ERROR   | [x] Line indented incorrectly; expected 0 spaces, found 4
 4 | ERROR   | [x] Expected 1 space after closing brace; newline found
 5 | WARNING | [x] Usage of ELSE IF is discouraged; use ELSEIF instead
 8 | ERROR   | [x] Line indented incorrectly; expected at least 4 spaces, found 0

As you can see, line 8 ($a = 1) is reported as containing error but it's formatted correctly.

Same result is when running with debug output:

↪  scripts/phpcs ~/test.php --standard=PSR2 --runtime-set scope_indent_debug 1
Start with token 0 on line 1 with indent 0
[Line 3] Line indented incorrectly; expected 0 spaces, found 4
	=> Add adjustment of -4 for token 3 (T_IF) on line 3
Closing parenthesis found on line 3
	 * ignoring single-line definition *
Open scope (T_IF) on line 3
	=> indent set to 4 by token 9 (T_OPEN_CURLY_BRACKET)
Close scope (T_IF) on line 4
	=> indent set to 0 by token 12 (T_CLOSE_CURLY_BRACKET)
[Line 4] Line indented incorrectly; expected 0 spaces, found 4
	=> Add adjustment of -4 for token 12 (T_CLOSE_CURLY_BRACKET) on line 4
Closing parenthesis found on line 5
	 * ignoring single-line definition *
Open scope (T_IF) on line 5
	=> indent set to 4 by token 23 (T_OPEN_CURLY_BRACKET)
Close scope (T_IF) on line 6
	=> indent set to 4 by token 26 (T_CLOSE_CURLY_BRACKET)
[Line 8] Line indented incorrectly; expected at least 4 spaces, found 0
	=> Add adjustment of 4 for token 29 (T_VARIABLE) on line 8

FILE: /home/morozov/test.php
----------------------------------------------------------------------------------
FOUND 4 ERRORS AND 1 WARNING AFFECTING 4 LINES
----------------------------------------------------------------------------------
 3 | ERROR   | [x] Line indented incorrectly; expected 0 spaces, found 4
 4 | ERROR   | [x] Line indented incorrectly; expected 0 spaces, found 4
 4 | ERROR   | [x] Expected 1 space after closing brace; newline found
 5 | WARNING | [x] Usage of ELSE IF is discouraged; use ELSEIF instead
 8 | ERROR   | [x] Line indented incorrectly; expected at least 4 spaces, found 0
----------------------------------------------------------------------------------
PHPCBF CAN FIX THE 5 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------

Time: 17ms; Memory: 4Mb

Surprisingly, removing the else block or replacing else if with elseif fixes the issue, so it must be a corner case.

@gsherwood
Copy link
Member

I think the error is caused by the T_ELSE token not being a scope opener in the example code. Changing it to T_ELSEIF makes it a scope opener and the standard rules apply. Having it on the same line as the closing brace of the T_IF also fixes this issue, so I think it's specific to this formatting of code and an exception can be coded in.

@gsherwood gsherwood changed the title Inconsistent indentation check results Inconsistent indentation check results when ELSE on new line Jan 18, 2017
@gsherwood
Copy link
Member

I've added an exception for this case now. Thanks for the report.

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