Skip to content

Commit 374ad13

Browse files
committed
SlevomatCodingStandard.Classes.MethodSpacing: Fixed check for methods with more attributes
1 parent 8bf0408 commit 374ad13

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

SlevomatCodingStandard/Sniffs/Classes/MethodSpacingSniff.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use function sprintf;
1616
use function str_repeat;
1717
use const T_ATTRIBUTE;
18+
use const T_ATTRIBUTE_END;
1819
use const T_FUNCTION;
1920
use const T_SEMICOLON;
2021

@@ -76,6 +77,23 @@ public function process(File $phpcsFile, $methodPointer): void
7677
$nextMethodPointer - 1,
7778
$methodEndPointer,
7879
);
80+
81+
if ($nextMethodAttributeStartPointer !== null) {
82+
do {
83+
$pointerBefore = TokenHelper::findPreviousNonWhitespace(
84+
$phpcsFile,
85+
$nextMethodAttributeStartPointer - 1,
86+
$methodEndPointer,
87+
);
88+
89+
if ($tokens[$pointerBefore]['code'] === T_ATTRIBUTE_END) {
90+
$nextMethodAttributeStartPointer = $tokens[$pointerBefore]['attribute_opener'];
91+
continue;
92+
}
93+
94+
break;
95+
} while (true);
96+
}
7997
}
8098

8199
$nextMethodFirstLinePointer = $tokens[$nextMethodPointer]['line'] === $tokens[$methodEndPointer]['line']

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(6, $report->getErrorCount());
20+
self::assertSame(7, $report->getErrorCount());
2121

2222
self::assertSniffError(
2323
$report,
@@ -55,6 +55,12 @@ public function testErrors(): void
5555
MethodSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_METHODS,
5656
'Expected 1 blank line after method, found 3.',
5757
);
58+
self::assertSniffError(
59+
$report,
60+
58,
61+
MethodSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_METHODS,
62+
'Expected 1 blank line after method, found 4.',
63+
);
5864

5965
self::assertAllFixedInFile($report);
6066
}
@@ -66,7 +72,7 @@ public function testErrorsDifferentSettingsAndSameLinesCount(): void
6672
'maxLinesCount' => 2,
6773
]);
6874

69-
self::assertSame(5, $report->getErrorCount());
75+
self::assertSame(6, $report->getErrorCount());
7076

7177
self::assertSniffError(
7278
$report,
@@ -98,6 +104,12 @@ public function testErrorsDifferentSettingsAndSameLinesCount(): void
98104
MethodSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_METHODS,
99105
'Expected 2 blank lines after method, found 3.',
100106
);
107+
self::assertSniffError(
108+
$report,
109+
58,
110+
MethodSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_METHODS,
111+
'Expected 2 blank lines after method, found 4.',
112+
);
101113
}
102114

103115
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
@@ -60,4 +60,10 @@ public function seventhMethod()
6060

6161
}
6262

63+
#[MyAttribute]
64+
#[MyAttribute2]
65+
public function eighthMethod()
66+
{
67+
}
68+
6369
}

tests/Sniffs/Classes/data/methodSpacingErrors.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,13 @@ public function seventhMethod()
6060

6161
}
6262

63+
64+
65+
66+
#[MyAttribute]
67+
#[MyAttribute2]
68+
public function eighthMethod()
69+
{
70+
}
71+
6372
}

0 commit comments

Comments
 (0)