Skip to content

Commit 225878e

Browse files
author
Vincent Langlet
committed
✨ Do not required blank line for custom annotations
1 parent 2e71651 commit 225878e

File tree

4 files changed

+74
-20
lines changed

4 files changed

+74
-20
lines changed

Symfony3Custom/Sniffs/Commenting/DocCommentGroupSameTypeSniff.php

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,42 @@
55
*/
66
class Symfony3Custom_Sniffs_Commenting_DocCommentGroupSameTypeSniff implements PHP_CodeSniffer_Sniff
77
{
8+
/**
9+
* A list of PHPDoc tags that are checked.
10+
*
11+
* @var array
12+
*/
13+
public $tags = array(
14+
'@api',
15+
'@author',
16+
'@category',
17+
'@copyright',
18+
'@deprecated',
19+
'@example',
20+
'@filesource',
21+
'@global',
22+
'@ignore',
23+
'@internal',
24+
'@license',
25+
'@link',
26+
'@method',
27+
'@package',
28+
'@param',
29+
'@property',
30+
'@property-read',
31+
'@property-write',
32+
'@return',
33+
'@see',
34+
'@since',
35+
'@source',
36+
'@subpackage',
37+
'@throws',
38+
'@todo',
39+
'@uses',
40+
'@var',
41+
'@version',
42+
);
43+
844
/**
945
* A list of tokenizers this sniff supports.
1046
*
@@ -39,23 +75,40 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
3975
$stackPtr
4076
);
4177

78+
$previousTag = $phpcsFile->findPrevious(
79+
T_DOC_COMMENT_TAG,
80+
$commentTag - 1,
81+
$stackPtr
82+
);
83+
4284
if (false !== $previousString) {
4385
$previousStringLine = $tokens[$previousString]['line'];
86+
$previousTagLine = $tokens[$previousTag]['line'];
87+
$previousLine = max($previousStringLine, $previousTagLine);
4488

45-
if (isset($previousTagLine)) {
46-
$previousLine = max($previousStringLine, $previousTagLine);
47-
} else {
48-
$previousLine = $previousStringLine;
49-
}
89+
$currentIsCustom = ! in_array($currentType, $this->tags);
90+
$previousIsCustom = ! in_array($previousType, $this->tags);
5091

51-
if ($previousType === $currentType) {
92+
if (($previousType === $currentType)
93+
|| ($currentIsCustom && $previousIsCustom)
94+
) {
5295
if ($previousLine !== $commentTagLine - 1) {
53-
$fix = $phpcsFile->addFixableError(
54-
'Expected no empty lines '
55-
. 'between annotations of the same type',
56-
$commentTag,
57-
'SameType'
58-
);
96+
97+
if ($previousType === $currentType) {
98+
$fix = $phpcsFile->addFixableError(
99+
'Expected no empty lines '
100+
. 'between annotations of the same type',
101+
$commentTag,
102+
'SameType'
103+
);
104+
} else {
105+
$fix = $phpcsFile->addFixableError(
106+
'Expected no empty lines '
107+
. 'between custom annotations',
108+
$commentTag,
109+
'CustomType'
110+
);
111+
}
59112

60113
if ($fix === true) {
61114
$phpcsFile->fixer->beginChangeset();
@@ -113,7 +166,6 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
113166
}
114167

115168
$previousType = $currentType;
116-
$previousTagLine = $commentTagLine;
117169
}
118170
}
119171

Symfony3Custom/Tests/Commenting/DocCommentGroupSameTypeUnitTest.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* @see http://doc.example.com/
66
*
77
* @see SomeClass::someMethod()
8-
* @Route()
8+
* @Route("/{id}/", name="test")
9+
* @Security()
910
* @param string $a
1011
*
1112
*

Symfony3Custom/Tests/Commenting/DocCommentGroupSameTypeUnitTest.inc.fixed

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* @see http://doc.example.com/
77
* @see SomeClass::someMethod()
88
*
9-
* @Route()
9+
* @Route("/{id}/", name="test")
10+
* @Security()
1011
*
1112
* @param string $a
1213
* @param string $b

Symfony3Custom/Tests/Commenting/DocCommentGroupSameTypeUnitTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public function getErrorList()
2222
5 => 1,
2323
7 => 1,
2424
8 => 1,
25-
9 => 1,
26-
12 => 1,
25+
10 => 1,
2726
13 => 1,
2827
14 => 1,
29-
19 => 1,
30-
28 => 1,
31-
32 => 1,
28+
15 => 1,
29+
20 => 1,
30+
29 => 1,
31+
33 => 1,
3232
);
3333
}
3434

0 commit comments

Comments
 (0)