Skip to content

Commit dd1df37

Browse files
committed
added fixer for Property Comment sniff
1 parent 722f1c8 commit dd1df37

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

Sniffs/Commenting/PropertyCommentSniff.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,7 @@ protected function processTokenWithinScope(
9595
if ($code === T_DOC_COMMENT_CLOSE_TAG) {
9696
$commentStart = $tokens[$commentEnd]['comment_opener'];
9797

98-
if ($tokens[$commentStart]['line'] === $tokens[$commentEnd]['line']) {
99-
$phpcsFile->addError(
100-
'property doc comment must be multi line',
101-
$commentEnd,
102-
'NotMultiLineDocBlock'
103-
);
104-
}
98+
$isCommentOneLiner = $tokens[$commentStart]['line'] === $tokens[$commentEnd]['line'];
10599

106100
$length = $commentEnd - $commentStart + 1;
107101
$tokensAsString = $phpcsFile->getTokensAsString(
@@ -116,15 +110,44 @@ protected function processTokenWithinScope(
116110
$commentStart,
117111
'NoVarDefined'
118112
);
119-
} else if ($varCount > 1) {
113+
} elseif ($varCount > 1) {
120114
$phpcsFile->addError(
121115
'property doc comment must no multiple @var annotations',
122116
$commentStart,
123117
'MultipleVarDefined'
124118
);
125119
}
120+
121+
if ($varCount === 1) {
122+
if ($isCommentOneLiner) {
123+
$fix = $phpcsFile->addFixableError(
124+
'property doc comment must be multi line',
125+
$commentEnd,
126+
'NotMultiLineDocBlock'
127+
);
128+
129+
if ($fix) {
130+
$phpcsFile->fixer->beginChangeset();
131+
$phpcsFile->fixer->addContent($commentStart, "\n *");
132+
$phpcsFile->fixer->replaceToken(
133+
$commentEnd - 1,
134+
rtrim($tokens[$commentEnd - 1]['content'])
135+
);
136+
$phpcsFile->fixer->addContentBefore($commentEnd, "\n ");
137+
$phpcsFile->fixer->endChangeset();
138+
}
139+
}
140+
} else {
141+
if ($isCommentOneLiner) {
142+
$phpcsFile->addError(
143+
'property doc comment must be multi line',
144+
$commentEnd,
145+
'NotMultiLineDocBlock'
146+
);
147+
}
148+
}
126149
} elseif ($code === T_COMMENT) {
127-
$commentStart = $phpcsFile->findPrevious([T_COMMENT], $commentEnd, null, true);
150+
$commentStart = $phpcsFile->findPrevious(T_COMMENT, $commentEnd, null, true);
128151
$phpcsFile->addError(
129152
'property doc comment must begin with /**',
130153
$commentStart + 1,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
class X
4+
{
5+
public $publicPropertySelfDescriptive;
6+
7+
/**
8+
* @var int
9+
*/
10+
public $publicProperty = 1;
11+
12+
/**
13+
* @varx array
14+
*
15+
* array of foo => bar arrays
16+
*/
17+
private $_privateProperty = [['foo' => 'bar']];
18+
19+
/**
20+
* @var
21+
*
22+
* foo
23+
*
24+
* @var array
25+
*/
26+
private $_tmi;
27+
28+
/** stuff */
29+
private $_moar;
30+
31+
/*
32+
* no doc block
33+
*/
34+
private $_priv;
35+
36+
// no doc block
37+
private $_otherPriv;
38+
}

0 commit comments

Comments
 (0)