@@ -44,21 +44,68 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
44
44
}
45
45
}
46
46
47
+ $ name = $ phpcsFile ->getDeclarationName ($ stackPtr );
48
+ $ commentRequired = strpos ($ name , 'test ' ) !== 0
49
+ && $ name !== 'setUp '
50
+ && $ name !== 'tearDown ' ;
51
+
47
52
if ($ tokens [$ commentEnd ]['code ' ] !== T_DOC_COMMENT_CLOSE_TAG
48
53
&& $ tokens [$ commentEnd ]['code ' ] !== T_COMMENT
49
54
) {
50
- $ name = $ phpcsFile ->getDeclarationName ($ stackPtr );
55
+ $ hasComment = false ;
56
+ $ phpcsFile ->recordMetric ($ stackPtr , 'Function has doc comment ' , 'no ' );
57
+
58
+ if ($ commentRequired ) {
59
+ $ phpcsFile ->addError ('Missing function doc comment ' , $ stackPtr , 'Missing ' );
60
+ return ;
61
+ } else {
62
+ // The comment may not be required, we'll see in next checks
63
+ }
64
+ } else {
65
+ $ hasComment = true ;
66
+ $ phpcsFile ->recordMetric ($ stackPtr , 'Function has doc comment ' , 'yes ' );
67
+ }
51
68
52
- $ commentRequired = strpos ($ name , 'test ' ) !== 0
53
- && $ name !== 'setUp '
54
- && $ name !== 'tearDown ' ;
69
+ $ commentStart = null ;
70
+ if ($ hasComment ) {
71
+ if ($ tokens [$ commentEnd ]['code ' ] === T_COMMENT ) {
72
+ $ phpcsFile ->addError ('You must use "/**" style comments for a function comment ' , $ stackPtr , 'WrongStyle ' );
55
73
56
- if (! $ commentRequired ) {
57
74
return ;
58
75
}
76
+
77
+ if ($ tokens [$ commentEnd ]['line ' ] !== ($ tokens [$ stackPtr ]['line ' ] - 1 )) {
78
+ $ error = 'There must be no blank lines after the function comment ' ;
79
+ $ phpcsFile ->addError ($ error , $ commentEnd , 'SpacingAfter ' );
80
+ }
81
+
82
+ $ commentStart = $ tokens [$ commentEnd ]['comment_opener ' ];
83
+ foreach ($ tokens [$ commentStart ]['comment_tags ' ] as $ tag ) {
84
+ if ($ tokens [$ tag ]['content ' ] === '@see ' ) {
85
+ // Make sure the tag isn't empty.
86
+ $ string = $ phpcsFile ->findNext (T_DOC_COMMENT_STRING , $ tag , $ commentEnd );
87
+ if ($ string === false || $ tokens [$ string ]['line ' ] !== $ tokens [$ tag ]['line ' ]) {
88
+ $ error = 'Content missing for @see tag in function comment ' ;
89
+ $ phpcsFile ->addError ($ error , $ tag , 'EmptySees ' );
90
+ }
91
+ }
92
+ }
59
93
}
60
94
61
- parent ::process ($ phpcsFile , $ stackPtr );
95
+ $ this ->processReturn ($ phpcsFile , $ stackPtr , $ commentStart );
96
+
97
+ $ realParams = $ phpcsFile ->getMethodParameters ($ stackPtr );
98
+ if ($ hasComment ) {
99
+ // These checks need function comment
100
+ $ this ->processParams ($ phpcsFile , $ stackPtr , $ commentStart );
101
+ $ this ->processThrows ($ phpcsFile , $ stackPtr , $ commentStart );
102
+ } elseif (count ($ realParams ) > 0 ) {
103
+ foreach ($ realParams as $ neededParam ) {
104
+ $ error = 'Doc comment for parameter "%s" missing ' ;
105
+ $ data = array ($ neededParam ['name ' ]);
106
+ $ phpcsFile ->addError ($ error , $ stackPtr , 'MissingParamTag ' , $ data );
107
+ }
108
+ }
62
109
}
63
110
64
111
/**
@@ -67,7 +114,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
67
114
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
68
115
* @param int $stackPtr The position of the current token
69
116
* in the stack passed in $tokens.
70
- * @param int $commentStart The position in the stack
117
+ * @param int|null $commentStart The position in the stack
71
118
* where the comment started.
72
119
*
73
120
* @return void
@@ -77,8 +124,10 @@ protected function processReturn(
77
124
$ stackPtr ,
78
125
$ commentStart
79
126
) {
80
-
81
- if ($ this ->isInheritDoc ($ phpcsFile , $ stackPtr )) {
127
+ // Check for inheritDoc if there is comment
128
+ if ((null !== $ commentStart )
129
+ && $ this ->isInheritDoc ($ phpcsFile , $ stackPtr )
130
+ ) {
82
131
return ;
83
132
}
84
133
@@ -105,7 +154,15 @@ protected function processReturn(
105
154
if ($ tokens [$ i ]['code ' ] === T_RETURN
106
155
&& $ this ->isMatchingReturn ($ tokens , $ i )
107
156
) {
108
- parent ::processReturn ($ phpcsFile , $ stackPtr , $ commentStart );
157
+ if (null !== $ commentStart ) {
158
+ parent ::processReturn ($ phpcsFile , $ stackPtr , $ commentStart );
159
+ } else {
160
+ // There is no doc and we need one with @return
161
+ $ error = 'Missing @return tag in function comment ' ;
162
+ $ phpcsFile ->addError ($ error , $ stackPtr , 'MissingReturn ' );
163
+
164
+ }
165
+
109
166
break ;
110
167
}
111
168
}
0 commit comments