Skip to content

Generic.WhiteSpace.ScopeIndent issues with inline HTML and multi-line function signatures #1188

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
JDGrimes opened this issue Oct 5, 2016 · 2 comments

Comments

@JDGrimes
Copy link
Contributor

JDGrimes commented Oct 5, 2016

Minimum reproducible code:

class Test {

    public function a() {
        ?>a<?php
    }

    public function b(
        $a
    ) { // Line indented incorrectly; expected at least 2 tabs, found 1
        echo $a; // Line indented incorrectly; expected at least 3 tabs, found 2
    }
}

It doesn't relate directly to class scope, but it only appears when the two functions are indented at least one level themselves (as when in a class, and if statement, or another function). Changing the upper function to not contain inline HTML will make the errors go away. Changing the lower function so that it does not have a multi-line declaration will make the errors go away.

No errors are produced when these two elements are not used together in this order.

When the inline HTML is intended further (within several conditions, loops, etc.), the errors will likewise request for greater indents to match.


Tested against PHPCS 01ee360, the latest commit in master as of this writing. The issue also exists in 2.7.0 but not in 2.6.2. I've traced it to 07839d2.

I am using tab indents and a custom ruleset, but I have also tested this by running:

phpcs --standard=Generic --sniffs=Generic.WhiteSpace.ScopeIndent test.php

And the errors are still reporting requiring those same two lines to be indented an extra space (not a full tab, or 4 spaces, but just one space).

Because this behavior is inconsistent (moving the lower method above the upper one will cause all errors to go away) I believe this is a bug.

@gsherwood
Copy link
Member

Debug output:

$ phpcs temp.php --standard=Generic --sniffs=Generic.WhiteSpace.ScopeIndent --runtime-set scope_indent_debug true
Start with token 0 on line 1 with indent 0
Open scope (T_CLASS) on line 2
    => indent set to 4 by token 5 (T_OPEN_CURLY_BRACKET)
Open scope (T_FUNCTION) on line 4
    => indent set to 8 by token 17 (T_OPEN_CURLY_BRACKET)
Close PHP tag found on line 5
    => indent set to 8 by token 20 (T_CLOSE_TAG)
Open PHP tag found on line 5
    => indent set to 0 by token 22 (T_OPEN_TAG)
Close scope (T_FUNCTION) on line 6
    => indent set to 4 by token 24 (T_CLOSE_CURLY_BRACKET)
Closing parenthesis found on line 10
    * token is inside condition 1 (T_CLASS) on line 2 *
    * using condition *
    * open tag is inside condition; using open tag *
    => checking indent of 11; main indent set to 11 by token 22 (T_OPEN_TAG)
[Line 10] Line indented incorrectly; expected at least 11 spaces, found 4
    => Add adjustment of 7 for token 39 (T_CLOSE_PARENTHESIS) on line 10
Open scope (T_FUNCTION) on line 10
    => indent set to 15 by token 41 (T_OPEN_CURLY_BRACKET)
[Line 11] Line indented incorrectly; expected at least 15 spaces, found 8
    => Add adjustment of 7 for token 45 (T_ECHO) on line 11
Close scope (T_FUNCTION) on line 12
    => indent set to 4 by token 52 (T_CLOSE_CURLY_BRACKET)
Close scope (T_CLASS) on line 13
    => indent set to 0 by token 54 (T_CLOSE_CURLY_BRACKET)

FILE: temp.php
----------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
----------------------------------------------------------------------------------
 10 | ERROR | [x] Line indented incorrectly; expected at least 11 spaces, found 4
 11 | ERROR | [x] Line indented incorrectly; expected at least 15 spaces, found 8
----------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------

gsherwood added a commit that referenced this issue Oct 20, 2016
@gsherwood
Copy link
Member

The problem was caused by the fix for #1101, but I've changed that fix and supported this code as well now. Thanks for reporting the bug and providing a great test case.

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