Skip to content

Commit 9e8bbfd

Browse files
committed
PSR2/NamespaceDeclaration: do not enforce new line in inline HTML
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.
1 parent 72949bb commit 9e8bbfd

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/Standards/PSR2/Sniffs/Namespaces/NamespaceDeclarationSniff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public function process(File $phpcsFile, $stackPtr)
5050

5151
$end = $phpcsFile->findEndOfStatement($stackPtr);
5252
for ($i = ($end + 1); $i < ($phpcsFile->numTokens - 1); $i++) {
53+
if ($tokens[$i]['code'] === T_CLOSE_TAG) {
54+
// Don't enforce new line if the next thing after the statement is a PHP close tag.
55+
return;
56+
}
57+
5358
if ($tokens[$i]['line'] === $tokens[$end]['line']) {
5459
continue;
5560
}

src/Standards/PSR2/Tests/Namespaces/NamespaceDeclarationUnitTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ namespace Vendor\Package;
2424
$call = namespace\function_name();
2525
echo namespace\CONSTANT_NAME;
2626
// Something which is not a blank line.
27+
28+
// When the next thing after a namespace declaration is a PHP close tag, don't enforce a new line as it may impact HTML display.
29+
namespace Vendor\NoBlankLine\EndsOnCloseTag ?>
30+
<?php
31+
namespace Vendor\HasBlankLine\EndsOnSemiColonPlusCloseTag; ?>
32+
33+
<?php

src/Standards/PSR2/Tests/Namespaces/NamespaceDeclarationUnitTest.inc.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ namespace Vendor\Package;
2626
$call = namespace\function_name();
2727
echo namespace\CONSTANT_NAME;
2828
// Something which is not a blank line.
29+
30+
// When the next thing after a namespace declaration is a PHP close tag, don't enforce a new line as it may impact HTML display.
31+
namespace Vendor\NoBlankLine\EndsOnCloseTag ?>
32+
<?php
33+
namespace Vendor\HasBlankLine\EndsOnSemiColonPlusCloseTag; ?>
34+
35+
<?php

0 commit comments

Comments
 (0)