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

PSR2/NamespaceDeclaration: do not enforce new line in inline HTML #815

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Feb 12, 2025

Description

If a namespace declaration has a PHP close tag on the same line, the new line would be added as T_INLINE_HTML and therefore not be recognized as a blank line as the sniff solely looks for T_WHITESPACE. This then leads to a fixer conflict where the sniff just keeps adding new lines until it runs out of loops.

        => Fixing file: 0/6 violations remaining [made 46 passes]...
        * fixed 0 violations, starting loop 47 *
        PSR2.Namespaces.NamespaceDeclaration:81 replaced token 118 (T_INLINE_HTML on line 32) "\n\n" => "\n\n\n"
        PSR2.Namespaces.NamespaceDeclaration:81 replaced token 153 (T_INLINE_HTML on line 58) "\n\n" => "\n\n\n"
        => Fixing file: 2/6 violations remaining [made 47 passes]...
        * fixed 2 violations, starting loop 48 *
        **** PSR2.Namespaces.NamespaceDeclaration:81 has possible conflict with another sniff on loop 46; caused by the following change ****
        **** replaced token 118 (T_INLINE_HTML on line 32) "\n\n" => "\n\n\n" ****
        **** ignoring all changes until next loop ****
        => Fixing file: 0/6 violations remaining [made 48 passes]...
        * fixed 0 violations, starting loop 49 *

In my opinion, the sniff should bow out in that situation and should not enforce a new line as it may impact HTML display.

This commit implements this.

Includes a test safeguarding the fix.

Suggested changelog entry

PSR2.Namespaces.NamespaceDeclaration: prevent fixer conflict when the next thing after a namespace declaration is a PHP close tag

Related issues/external references

Related to #152

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

If a namespace declaration has a PHP close tag on the same line, the new line would be added as `T_INLINE_HTML` and therefore not be recognized as a blank line as the sniff solely looks for `T_WHITESPACE`. This then leads to a fixer conflict where the sniff just keeps adding new lines until it runs out of loops.

```
        => Fixing file: 0/6 violations remaining [made 46 passes]...
        * fixed 0 violations, starting loop 47 *
        PSR2.Namespaces.NamespaceDeclaration:81 replaced token 118 (T_INLINE_HTML on line 32) "\n\n" => "\n\n\n"
        PSR2.Namespaces.NamespaceDeclaration:81 replaced token 153 (T_INLINE_HTML on line 58) "\n\n" => "\n\n\n"
        => Fixing file: 2/6 violations remaining [made 47 passes]...
        * fixed 2 violations, starting loop 48 *
        **** PSR2.Namespaces.NamespaceDeclaration:81 has possible conflict with another sniff on loop 46; caused by the following change ****
        **** replaced token 118 (T_INLINE_HTML on line 32) "\n\n" => "\n\n\n" ****
        **** ignoring all changes until next loop ****
        => Fixing file: 0/6 violations remaining [made 48 passes]...
        * fixed 0 violations, starting loop 49 *
```

In my opinion, the sniff should bow out in that situation and should not enforce a new line as it may impact HTML display.

This commit implements this.

Includes a test safeguarding the fix.
@jrfnl
Copy link
Member Author

jrfnl commented Feb 13, 2025

FYI: Build failure is unrelated to this PR, but has to do with an outage at Coveralls. See lemurheavy/coveralls-public#1791 and https://status.coveralls.io/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant