Skip to content

Commit 202a47c

Browse files
author
Vincent Langlet
committed
🐛 Fix DocCommentGroupSameType for @route, @Assert, etc
1 parent ef32976 commit 202a47c

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

Symfony3Custom/Sniffs/Commenting/DocCommentGroupSameTypeSniff.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
3131
$previousType = '';
3232
foreach ($tokens[$stackPtr]['comment_tags'] as $commentTag) {
3333
$currentType = $tokens[$commentTag]['content'];
34+
$commentTagLine = $tokens[$commentTag]['line'];
3435

3536
$previousString = $phpcsFile->findPrevious(
3637
T_DOC_COMMENT_STRING,
@@ -40,10 +41,15 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
4041

4142
if (false !== $previousString) {
4243
$previousStringLine = $tokens[$previousString]['line'];
43-
$commentTagLine = $tokens[$commentTag]['line'];
44+
45+
if (isset($previousTagLine)) {
46+
$previousLine = max($previousStringLine, $previousTagLine);
47+
} else {
48+
$previousLine = $previousStringLine;
49+
}
4450

4551
if ($previousType === $currentType) {
46-
if ($previousStringLine !== $commentTagLine - 1) {
52+
if ($previousLine !== $commentTagLine - 1) {
4753
$fix = $phpcsFile->addFixableError(
4854
'Expected no empty lines '
4955
. 'between annotations of the same type',
@@ -56,14 +62,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
5662
$this->removeLines(
5763
$phpcsFile,
5864
$previousString,
59-
$previousStringLine + 1,
65+
$previousLine + 1,
6066
$commentTagLine - 1
6167
);
6268
$phpcsFile->fixer->endChangeset();
6369
}
6470
}
6571
} else {
66-
if ($previousStringLine !== $commentTagLine - 2) {
72+
if ($previousLine !== $commentTagLine - 2) {
6773
$fix = $phpcsFile->addFixableError(
6874
'Expected exactly one empty line '
6975
. 'between annotations of different types',
@@ -73,7 +79,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
7379

7480
if ($fix === true) {
7581
$phpcsFile->fixer->beginChangeset();
76-
if ($previousStringLine === $commentTagLine - 1) {
82+
83+
if ($previousLine === $commentTagLine - 1) {
7784
$firstOnLine = $phpcsFile->findFirstOnLine(
7885
array(),
7986
$commentTag,
@@ -95,7 +102,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
95102
$this->removeLines(
96103
$phpcsFile,
97104
$previousString,
98-
$previousStringLine + 2,
105+
$previousLine + 2,
99106
$commentTagLine - 1
100107
);
101108
}
@@ -106,6 +113,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
106113
}
107114

108115
$previousType = $currentType;
116+
$previousTagLine = $commentTagLine;
109117
}
110118
}
111119

Symfony3Custom/Tests/Commenting/DocCommentGroupSameTypeUnitTest.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @see http://doc.example.com/
66
*
77
* @see SomeClass::someMethod()
8+
* @Route()
89
* @param string $a
910
*
1011
*

Symfony3Custom/Tests/Commenting/DocCommentGroupSameTypeUnitTest.inc.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* @see http://doc.example.com/
77
* @see SomeClass::someMethod()
88
*
9+
* @Route()
10+
*
911
* @param string $a
1012
* @param string $b
1113
*

Symfony3Custom/Tests/Commenting/DocCommentGroupSameTypeUnitTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ public function getErrorList()
2222
5 => 1,
2323
7 => 1,
2424
8 => 1,
25-
11 => 1,
25+
9 => 1,
2626
12 => 1,
2727
13 => 1,
28-
18 => 1,
29-
27 => 1,
30-
31 => 1,
28+
14 => 1,
29+
19 => 1,
30+
28 => 1,
31+
32 => 1,
3132
);
3233
}
3334

0 commit comments

Comments
 (0)