Skip to content

Commit a5dda73

Browse files
committed
TraitUseSpacingSniff: Fix for uses with comments
1 parent 6f45c01 commit a5dda73

File tree

5 files changed

+100
-2
lines changed

5 files changed

+100
-2
lines changed

SlevomatCodingStandard/Sniffs/Classes/TraitUseSpacingSniff.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use PHP_CodeSniffer\Util\Tokens;
78
use SlevomatCodingStandard\Helpers\ClassHelper;
89
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
910
use SlevomatCodingStandard\Helpers\TokenHelper;
1011
use function count;
12+
use function in_array;
1113
use function sprintf;
1214
use function substr_count;
1315
use const T_ANON_CLASS;
@@ -74,14 +76,22 @@ private function checkLinesBeforeFirstUse(File $phpcsFile, int $firstUsePointer)
7476
{
7577
$tokens = $phpcsFile->getTokens();
7678

79+
$useStartPointer = $firstUsePointer;
80+
7781
/** @var int $pointerBeforeFirstUse */
7882
$pointerBeforeFirstUse = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $firstUsePointer - 1);
83+
84+
if (in_array($tokens[$pointerBeforeFirstUse]['code'], Tokens::$commentTokens, true)) {
85+
$pointerBeforeFirstUse = TokenHelper::findPreviousEffective($phpcsFile, $pointerBeforeFirstUse - 1);
86+
$useStartPointer = TokenHelper::findNext($phpcsFile, Tokens::$commentTokens, $pointerBeforeFirstUse + 1);
87+
}
88+
7989
$isAtTheStartOfClass = $tokens[$pointerBeforeFirstUse]['code'] === T_OPEN_CURLY_BRACKET;
8090

8191
$whitespaceBeforeFirstUse = '';
8292

8393
if ($pointerBeforeFirstUse + 1 !== $firstUsePointer) {
84-
$whitespaceBeforeFirstUse .= TokenHelper::getContent($phpcsFile, $pointerBeforeFirstUse + 1, $firstUsePointer - 1);
94+
$whitespaceBeforeFirstUse .= TokenHelper::getContent($phpcsFile, $pointerBeforeFirstUse + 1, $useStartPointer - 1);
8595
}
8696

8797
$requiredLinesCountBeforeFirstUse = SniffSettingsHelper::normalizeInteger($this->linesCountBeforeFirstUse);
@@ -91,6 +101,7 @@ private function checkLinesBeforeFirstUse(File $phpcsFile, int $firstUsePointer)
91101
) {
92102
$requiredLinesCountBeforeFirstUse = SniffSettingsHelper::normalizeInteger($this->linesCountBeforeFirstUseWhenFirstInClass);
93103
}
104+
94105
$actualLinesCountBeforeFirstUse = substr_count($whitespaceBeforeFirstUse, $phpcsFile->eolChar) - 1;
95106

96107
if ($actualLinesCountBeforeFirstUse === $requiredLinesCountBeforeFirstUse) {
@@ -206,7 +217,14 @@ private function checkLinesBetweenUses(File $phpcsFile, array $usePointers): voi
206217
$previousUseEndPointer = $tokens[$previousUseEndPointer]['bracket_closer'];
207218
}
208219

209-
$actualLinesCountAfterPreviousUse = $tokens[$usePointer]['line'] - $tokens[$previousUseEndPointer]['line'] - 1;
220+
$useStartPointer = $usePointer;
221+
$pointerBeforeUse = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $usePointer - 1);
222+
223+
if (in_array($tokens[$pointerBeforeUse]['code'], Tokens::$commentTokens, true)) {
224+
$useStartPointer = TokenHelper::findNext($phpcsFile, Tokens::$commentTokens, TokenHelper::findPreviousEffective($phpcsFile, $pointerBeforeUse - 1) + 1);
225+
}
226+
227+
$actualLinesCountAfterPreviousUse = $tokens[$useStartPointer]['line'] - $tokens[$previousUseEndPointer]['line'] - 1;
210228

211229
if ($actualLinesCountAfterPreviousUse === $requiredLinesCountBetweenUses) {
212230
$previousUsePointer = $usePointer;

tests/Sniffs/Classes/TraitUseSpacingSniffTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ public function testOneUseDefaultSettingErrors(): void
5858
self::assertAllFixedInFile($report);
5959
}
6060

61+
public function testDefaultSettingWithCommentsNoErrors(): void
62+
{
63+
$report = self::checkFile(__DIR__ . '/data/traitUseSpacingDefaultSettingsWithCommentsNoErrors.php');
64+
self::assertNoSniffErrorInFile($report);
65+
}
66+
67+
public function testDefaultSettingWithCommentsErrors(): void
68+
{
69+
$report = self::checkFile(__DIR__ . '/data/traitUseSpacingDefaultSettingsWithCommentsErrors.php');
70+
71+
self::assertSame(2, $report->getErrorCount());
72+
73+
self::assertSniffError($report, 8, TraitUseSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_USE);
74+
self::assertSniffError($report, 14, TraitUseSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_USES);
75+
76+
self::assertAllFixedInFile($report);
77+
}
78+
6179
public function testModifiedSettingNoErrors(): void
6280
{
6381
$report = self::checkFile(__DIR__ . '/data/traitUseSpacingModifiedSettingsNoErrors.php', [
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
class Whatever
4+
{
5+
6+
// Comment
7+
use A;
8+
use B {
9+
B::b as bb;
10+
}
11+
// Comment
12+
use C;
13+
use D;
14+
15+
public function __construct()
16+
{
17+
18+
}
19+
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
class Whatever
4+
{
5+
6+
7+
// Comment
8+
use A;
9+
use B {
10+
B::b as bb;
11+
}
12+
13+
// Comment
14+
use C;
15+
use D;
16+
17+
public function __construct()
18+
{
19+
20+
}
21+
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
class Whatever
4+
{
5+
6+
// Comment
7+
use A;
8+
use B {
9+
B::b as bb;
10+
}
11+
// Comment
12+
use C;
13+
use D;
14+
15+
public function __construct()
16+
{
17+
18+
}
19+
20+
}

0 commit comments

Comments
 (0)