Skip to content

Commit 95436f1

Browse files
committed
NamespaceSpacingSniff: fixed when namespace is after line comment
1 parent 9cefbf1 commit 95436f1

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/NamespaceSpacingSniff.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ private function checkLinesBeforeNamespace(\PHP_CodeSniffer\Files\File $phpcsFil
4949

5050
if ($tokens[$pointerBeforeNamespace]['code'] === T_OPEN_TAG) {
5151
$whitespaceBeforeNamespace .= substr($tokens[$pointerBeforeNamespace]['content'], strlen('<?php'));
52+
} elseif ($tokens[$pointerBeforeNamespace]['code'] === T_COMMENT) {
53+
$whitespaceBeforeNamespace .= $phpcsFile->eolChar;
5254
}
5355

5456
if ($pointerBeforeNamespace + 1 !== $namespacePointer) {
@@ -80,6 +82,8 @@ private function checkLinesBeforeNamespace(\PHP_CodeSniffer\Files\File $phpcsFil
8082

8183
if ($tokens[$pointerBeforeNamespace]['code'] === T_OPEN_TAG) {
8284
$phpcsFile->fixer->replaceToken($pointerBeforeNamespace, '<?php');
85+
} elseif ($tokens[$pointerBeforeNamespace]['code'] === T_COMMENT) {
86+
$phpcsFile->fixer->replaceToken($pointerBeforeNamespace, rtrim($tokens[$pointerBeforeNamespace]['content'], $phpcsFile->eolChar));
8387
}
8488

8589
for ($i = $pointerBeforeNamespace + 1; $i < $namespacePointer; $i++) {

tests/Sniffs/Namespaces/NamespaceSpacingSniffTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ public function testAfterOpenTagErrors(): void
5252
self::assertAllFixedInFile($report);
5353
}
5454

55+
public function testAfterLineCommentNoErrors(): void
56+
{
57+
$report = self::checkFile(__DIR__ . '/data/namespaceSpacingAfterLineCommentNoErrors.php');
58+
self::assertNoSniffErrorInFile($report);
59+
}
60+
61+
public function testAfterLineCommentErrors(): void
62+
{
63+
$report = self::checkFile(__DIR__ . '/data/namespaceSpacingAfterLineCommentErrors.php');
64+
65+
self::assertSame(1, $report->getErrorCount());
66+
67+
self::assertSniffError($report, 4, NamespaceSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_NAMESPACE);
68+
69+
self::assertAllFixedInFile($report);
70+
}
71+
5572
public function testModifiedSettingsNoErrors(): void
5673
{
5774
$report = self::checkFile(__DIR__ . '/data/namespaceSpacingWithModifiedSettingsNoErrors.php', [
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
// Comment
4+
5+
namespace Something;
6+
7+
class Whatever
8+
{
9+
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
// Comment
4+
namespace Something;
5+
6+
class Whatever
7+
{
8+
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
// Comment
4+
5+
namespace Something;
6+
7+
class Whatever
8+
{
9+
10+
}

0 commit comments

Comments
 (0)