Skip to content

Commit 7d6ceeb

Browse files
committed
Add custom variable comment sniff
This allows class properties to be types with int and bool instead of requiring integer or boolean
1 parent 48e45e6 commit 7d6ceeb

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
final class Chadicus_Sniffs_Commenting_VariableCommentSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
4+
{
5+
/**
6+
* Called to process class member vars.
7+
*
8+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
9+
* @param int $stackPtr The position of the current token
10+
* in the stack passed in $tokens.
11+
*
12+
* @return void
13+
*/
14+
public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
15+
{
16+
$tokens = $phpcsFile->getTokens();
17+
$ignore = array(T_PUBLIC, T_PRIVATE, T_PROTECTED, T_VAR, T_STATIC, T_WHITESPACE);
18+
$commentEnd = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
19+
if ($commentEnd === false
20+
|| ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
21+
&& $tokens[$commentEnd]['code'] !== T_COMMENT)
22+
) {
23+
$phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing');
24+
return;
25+
}
26+
27+
if ($tokens[$commentEnd]['code'] === T_COMMENT) {
28+
$phpcsFile->addError('You must use "/**" style comments for a member variable comment', $stackPtr, 'WrongStyle');
29+
return;
30+
}
31+
32+
$commentStart = $tokens[$commentEnd]['comment_opener'];
33+
34+
$foundVar = null;
35+
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
36+
if ($tokens[$tag]['content'] === '@var') {
37+
if ($foundVar !== null) {
38+
$error = 'Only one @var tag is allowed in a member variable comment';
39+
$phpcsFile->addError($error, $tag, 'DuplicateVar');
40+
} else {
41+
$foundVar = $tag;
42+
}
43+
} else if ($tokens[$tag]['content'] === '@see') {
44+
// Make sure the tag isn't empty.
45+
$string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
46+
if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) {
47+
$error = 'Content missing for @see tag in member variable comment';
48+
$phpcsFile->addError($error, $tag, 'EmptySees');
49+
}
50+
} else {
51+
$error = '%s tag is not allowed in member variable comment';
52+
$data = array($tokens[$tag]['content']);
53+
$phpcsFile->addWarning($error, $tag, 'TagNotAllowed', $data);
54+
}
55+
}
56+
57+
// The @var tag is the only one we require.
58+
if ($foundVar === null) {
59+
$error = 'Missing @var tag in member variable comment';
60+
$phpcsFile->addError($error, $commentEnd, 'MissingVar');
61+
return;
62+
}
63+
64+
$firstTag = $tokens[$commentStart]['comment_tags'][0];
65+
if ($foundVar !== null && $tokens[$firstTag]['content'] !== '@var') {
66+
$error = 'The @var tag must be the first tag in a member variable comment';
67+
$phpcsFile->addError($error, $foundVar, 'VarOrder');
68+
}
69+
70+
// Make sure the tag isn't empty and has the correct padding.
71+
$string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $foundVar, $commentEnd);
72+
if ($string === false || $tokens[$string]['line'] !== $tokens[$foundVar]['line']) {
73+
$error = 'Content missing for @var tag in member variable comment';
74+
$phpcsFile->addError($error, $foundVar, 'EmptyVar');
75+
return;
76+
}
77+
}
78+
79+
/**
80+
* Called to process a normal variable.
81+
*
82+
* Not required for this sniff.
83+
*
84+
* @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this token was found.
85+
* @param int $stackPtr The position where the double quoted
86+
* string was found.
87+
*
88+
* @return void
89+
*/
90+
protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
91+
{
92+
93+
}
94+
95+
/**
96+
* Called to process variables found in double quoted strings.
97+
*
98+
* Not required for this sniff.
99+
*
100+
* @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this token was found.
101+
* @param int $stackPtr The position where the double quoted
102+
* string was found.
103+
*
104+
* @return void
105+
*/
106+
protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
107+
{
108+
109+
}
110+
}

Chadicus/ruleset.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<rule ref="Generic.ControlStructures.InlineControlStructure" />
1212
<rule ref="Squiz.Commenting.DocCommentAlignment" />
1313
<rule ref="Squiz.Commenting.FunctionCommentThrowTag" />
14-
<rule ref="Squiz.Commenting.VariableComment" />
1514
<rule ref="Generic.Commenting.Fixme" />
1615
<rule ref="Generic.Commenting.Todo" />
1716
<rule ref="Generic.Metrics.CyclomaticComplexity" />

0 commit comments

Comments
 (0)