Skip to content

Commit d5a5ebb

Browse files
committed
SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses: Fixer should not remove doc comments
1 parent 87a410c commit d5a5ebb

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/AlphabeticallySortedUsesSniff.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use PHP_CodeSniffer\Util\Tokens;
78
use SlevomatCodingStandard\Helpers\CommentHelper;
89
use SlevomatCodingStandard\Helpers\FixerHelper;
910
use SlevomatCodingStandard\Helpers\NamespaceHelper;
@@ -107,7 +108,7 @@ private function fixAlphabeticalOrder(File $phpcsFile, array $useStatements): vo
107108
foreach ($useStatements as $useStatement) {
108109
$pointerBeforeUseStatement = TokenHelper::findPreviousNonWhitespace($phpcsFile, $useStatement->getPointer() - 1);
109110

110-
if (!in_array($tokens[$pointerBeforeUseStatement]['code'], TokenHelper::$inlineCommentTokenCodes, true)) {
111+
if (!in_array($tokens[$pointerBeforeUseStatement]['code'], Tokens::$commentTokens, true)) {
111112
continue;
112113
}
113114

@@ -116,7 +117,9 @@ private function fixAlphabeticalOrder(File $phpcsFile, array $useStatements): vo
116117
continue;
117118
}
118119

119-
$commentStartPointer = CommentHelper::getMultilineCommentStartPointer($phpcsFile, $pointerBeforeUseStatement);
120+
$commentStartPointer = in_array($tokens[$pointerBeforeUseStatement]['code'], TokenHelper::$inlineCommentTokenCodes, true)
121+
? CommentHelper::getMultilineCommentStartPointer($phpcsFile, $pointerBeforeUseStatement)
122+
: $tokens[$pointerBeforeUseStatement]['comment_opener'];
120123

121124
$commentsBefore[$useStatement->getPointer()] = TokenHelper::getContent(
122125
$phpcsFile,

tests/Sniffs/Namespaces/AlphabeticallySortedUsesSniffTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ public function testFixableWithCommentBeforeFirst(): void
8686
self::assertAllFixedInFile($report);
8787
}
8888

89+
public function testFixableWithDocComment(): void
90+
{
91+
$report = self::checkFile(
92+
__DIR__ . '/data/fixableAlphabeticalSortedUsesWithDocComment.php',
93+
[],
94+
[AlphabeticallySortedUsesSniff::CODE_INCORRECT_ORDER]
95+
);
96+
self::assertAllFixedInFile($report);
97+
}
98+
8999
public function testFixableNotPsr12Compatible(): void
90100
{
91101
$report = self::checkFile(__DIR__ . '/data/fixableAlphabeticalSortedUsesNotPsr12Compatible.php', [
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Abc;
4+
5+
/**
6+
* A comment
7+
*/
8+
use A;
9+
/**
10+
* B comment
11+
*/
12+
use B;
13+
/**
14+
* C comment
15+
*/
16+
use C;
17+
/**
18+
* D comment
19+
*/
20+
use D;
21+
22+
class Alphabetical
23+
{
24+
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Abc;
4+
5+
/**
6+
* C comment
7+
*/
8+
use C;
9+
/**
10+
* B comment
11+
*/
12+
use B;
13+
/**
14+
* D comment
15+
*/
16+
use D;
17+
/**
18+
* A comment
19+
*/
20+
use A;
21+
22+
class Alphabetical
23+
{
24+
25+
}

0 commit comments

Comments
 (0)