Skip to content

Commit a8aaa01

Browse files
committed
PSR2/NamespaceDeclaration: fix fixer conflict
1 parent a7d77cf commit a8aaa01

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function process(File $phpcsFile, $stackPtr)
6060
// The $i var now points to the first token on the line after the
6161
// namespace declaration, which must be a blank line.
6262
$next = $phpcsFile->findNext(T_WHITESPACE, $i, $phpcsFile->numTokens, true);
63-
if ($next === false) {
63+
if ($next === false || $next === (count($tokens) - 1)) {
6464
return;
6565
}
6666

@@ -73,8 +73,18 @@ public function process(File $phpcsFile, $stackPtr)
7373
$diff = 0;
7474
}
7575

76+
if ($diff === 0 && $tokens[$i]['code'] === T_INLINE_HTML && $tokens[$i]['content'] === "\n") {
77+
return;
78+
}
79+
7680
$error = 'There must be one blank line after the namespace declaration';
77-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'BlankLineAfter');
81+
82+
if ($tokens[$i]['code'] === T_INLINE_HTML) {
83+
$phpcsFile->addError($error, $stackPtr, 'BlankLineAfterHTML');
84+
return;
85+
}
86+
87+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'BlankLineAfter');
7888

7989
if ($fix === true) {
8090
if ($diff === 0) {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,22 @@ namespace Vendor\Package;
2424
$call = namespace\function_name();
2525
echo namespace\CONSTANT_NAME;
2626
// Something which is not a blank line.
27+
28+
namespace Vendor\Package; $var = true;
29+
namespace Vendor\Package; // comment
30+
namespace Vendor\Package; /* comment */
31+
namespace Vendor\Package; ?> inline HTML <?php
32+
namespace Vendor\Package;use Throwable; class Thing {}
33+
namespace Vendor\Package;class Thing {}
34+
namespace Vendor\Package;/* comment */
35+
namespace Vendor\Package;?> inline HTML
36+
37+
The blank line before this text is intentional.
38+
<?php echo __FILE__;
39+
namespace Vendor\Package
40+
; $var = true;
41+
namespace Vendor\Package; ?>
42+
The lack of blank line here is not auto-fixable.
43+
<?php
44+
// This should be the last test in the file. It shouldn't raise any errors.
45+
namespace Vendor\Package; ?>

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,30 @@ namespace Vendor\Package;
2626
$call = namespace\function_name();
2727
echo namespace\CONSTANT_NAME;
2828
// Something which is not a blank line.
29+
30+
namespace Vendor\Package; $var = true;
31+
32+
namespace Vendor\Package; // comment
33+
34+
namespace Vendor\Package; /* comment */
35+
36+
namespace Vendor\Package; ?> inline HTML <?php
37+
38+
namespace Vendor\Package;use Throwable; class Thing {}
39+
40+
namespace Vendor\Package;class Thing {}
41+
42+
namespace Vendor\Package;/* comment */
43+
44+
namespace Vendor\Package;?> inline HTML
45+
46+
The blank line before this text is intentional.
47+
<?php echo __FILE__;
48+
namespace Vendor\Package
49+
; $var = true;
50+
51+
namespace Vendor\Package; ?>
52+
The lack of blank line here is not auto-fixable.
53+
<?php
54+
// This should be the last test in the file. It shouldn't raise any errors.
55+
namespace Vendor\Package; ?>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ public function getErrorList()
3535
9 => 1,
3636
17 => 1,
3737
19 => 1,
38+
28 => 1,
39+
29 => 1,
40+
30 => 1,
41+
31 => 1,
42+
32 => 1,
43+
33 => 1,
44+
34 => 1,
45+
39 => 1,
46+
41 => 1,
3847
];
3948

4049
}//end getErrorList()

0 commit comments

Comments
 (0)