Skip to content

Commit 248eb0a

Browse files
committed
SlevomatCodingStandard.Classes.MethodSpacing: Fix check for method with attributes
1 parent 372cbf2 commit 248eb0a

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

SlevomatCodingStandard/Sniffs/Classes/MethodSpacingSniff.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function array_key_exists;
1515
use function sprintf;
1616
use function str_repeat;
17+
use const T_ATTRIBUTE;
1718
use const T_FUNCTION;
1819
use const T_SEMICOLON;
1920

@@ -62,17 +63,29 @@ public function process(File $phpcsFile, $methodPointer): void
6263
return;
6364
}
6465

66+
$nextMethodAttributeStartPointer = null;
6567
$nextMethodDocCommentStartPointer = DocCommentHelper::findDocCommentOpenPointer($phpcsFile, $nextMethodPointer);
68+
6669
if (
6770
$nextMethodDocCommentStartPointer !== null
6871
&& $tokens[$tokens[$nextMethodDocCommentStartPointer]['comment_closer']]['line'] + 1 !== $tokens[$nextMethodPointer]['line']
6972
) {
7073
$nextMethodDocCommentStartPointer = null;
74+
} else {
75+
$nextMethodAttributeStartPointer = TokenHelper::findPrevious(
76+
$phpcsFile,
77+
T_ATTRIBUTE,
78+
$nextMethodPointer - 1,
79+
$methodEndPointer
80+
);
7181
}
7282

7383
$nextMethodFirstLinePointer = $tokens[$nextMethodPointer]['line'] === $tokens[$methodEndPointer]['line']
7484
? TokenHelper::findNextEffective($phpcsFile, $methodEndPointer + 1)
75-
: TokenHelper::findFirstTokenOnLine($phpcsFile, $nextMethodDocCommentStartPointer ?? $nextMethodPointer);
85+
: TokenHelper::findFirstTokenOnLine(
86+
$phpcsFile,
87+
$nextMethodDocCommentStartPointer ?? $nextMethodAttributeStartPointer ?? $nextMethodPointer
88+
);
7689

7790
if (TokenHelper::findNextNonWhitespace($phpcsFile, $methodEndPointer + 1, $nextMethodFirstLinePointer) !== null) {
7891
return;

tests/Sniffs/Classes/MethodSpacingSniffTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testErrors(): void
1717
{
1818
$report = self::checkFile(__DIR__ . '/data/methodSpacingErrors.php');
1919

20-
self::assertSame(5, $report->getErrorCount());
20+
self::assertSame(6, $report->getErrorCount());
2121

2222
self::assertSniffError(
2323
$report,
@@ -49,6 +49,12 @@ public function testErrors(): void
4949
MethodSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_METHODS,
5050
'Expected 1 blank line after method, found 2.'
5151
);
52+
self::assertSniffError(
53+
$report,
54+
50,
55+
MethodSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_METHODS,
56+
'Expected 1 blank line after method, found 3.'
57+
);
5258

5359
self::assertAllFixedInFile($report);
5460
}
@@ -60,7 +66,7 @@ public function testErrorsDifferentSettingsAndSameLinesCount(): void
6066
'maxLinesCount' => 2,
6167
]);
6268

63-
self::assertSame(4, $report->getErrorCount());
69+
self::assertSame(5, $report->getErrorCount());
6470

6571
self::assertSniffError(
6672
$report,
@@ -86,6 +92,12 @@ public function testErrorsDifferentSettingsAndSameLinesCount(): void
8692
MethodSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_METHODS,
8793
'Expected 2 blank lines after method, found 0.'
8894
);
95+
self::assertSniffError(
96+
$report,
97+
50,
98+
MethodSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_METHODS,
99+
'Expected 2 blank lines after method, found 3.'
100+
);
89101
}
90102

91103
public function testWithDifferentSettingsNoErrors(): void

tests/Sniffs/Classes/data/methodSpacingErrors.fixed.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,10 @@ public function sixthMethod()
5454

5555
}
5656

57+
#[MyAttribute]
58+
public function seventhMethod()
59+
{
60+
61+
}
62+
5763
}

tests/Sniffs/Classes/data/methodSpacingErrors.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,12 @@ public function sixthMethod()
5252

5353
}
5454

55+
56+
57+
#[MyAttribute]
58+
public function seventhMethod()
59+
{
60+
61+
}
62+
5563
}

tests/Sniffs/Classes/data/methodSpacingNoErrors.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,9 @@ public function returnTrue(): bool
9999
return [$anonymousClassA, $anonymousClassB];
100100
}
101101

102+
#[MyAttribute]
103+
public function sixthMethod()
104+
{
105+
}
106+
102107
}

0 commit comments

Comments
 (0)