@@ -67,7 +67,6 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
67
67
$ phpcsFile ->recordMetric ($ stackPtr , 'Function has doc comment ' , 'yes ' );
68
68
}
69
69
70
- $ commentStart = null ;
71
70
if ($ hasComment ) {
72
71
if ($ tokens [$ commentEnd ]['code ' ] === T_COMMENT ) {
73
72
$ phpcsFile ->addError (
@@ -95,17 +94,30 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
95
94
}
96
95
}
97
96
}
97
+ } else {
98
+ // No comment but maybe a method prefix
99
+ $ methodPrefixes = $ phpcsFile ->findFirstOnLine (
100
+ PHP_CodeSniffer_Tokens::$ methodPrefixes ,
101
+ $ stackPtr
102
+ );
103
+
104
+ if ($ methodPrefixes !== false ) {
105
+ $ commentStart = $ methodPrefixes ;
106
+ } else {
107
+ // No comment and no method prefix
108
+ $ commentStart = $ stackPtr ;
109
+ }
98
110
}
99
111
100
- $ this ->processReturn ($ phpcsFile , $ stackPtr , $ commentStart );
112
+ $ this ->processReturn ($ phpcsFile , $ stackPtr , $ commentStart, $ hasComment );
101
113
102
114
$ realParams = $ phpcsFile ->getMethodParameters ($ stackPtr );
103
115
if ($ hasComment ) {
104
116
// These checks need function comment
105
117
$ this ->processParams ($ phpcsFile , $ stackPtr , $ commentStart );
106
118
$ this ->processThrows ($ phpcsFile , $ stackPtr , $ commentStart );
107
119
} else {
108
- $ this ->processWhitespace ($ phpcsFile , $ stackPtr , $ stackPtr );
120
+ $ this ->processWhitespace ($ phpcsFile , $ commentStart , $ hasComment );
109
121
110
122
if (count ($ realParams ) > 0 ) {
111
123
foreach ($ realParams as $ neededParam ) {
@@ -120,23 +132,23 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
120
132
/**
121
133
* Process the return comment of this function comment.
122
134
*
123
- * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
124
- * @param int $stackPtr The position of the current token
125
- * in the stack passed in $tokens.
126
- * @param int|null $commentStart The position in the stack
127
- * where the comment started.
135
+ * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
136
+ * @param int $stackPtr The position of the current token
137
+ * in the stack passed in $tokens.
138
+ * @param int|null $commentStart The position in the stack
139
+ * where the comment started.
140
+ * @param bool $hasRealComment
128
141
*
129
142
* @return void
130
143
*/
131
144
protected function processReturn (
132
145
PHP_CodeSniffer_File $ phpcsFile ,
133
146
$ stackPtr ,
134
- $ commentStart
147
+ $ commentStart ,
148
+ $ hasRealComment = true
135
149
) {
136
150
// Check for inheritDoc if there is comment
137
- if ((null !== $ commentStart )
138
- && $ this ->isInheritDoc ($ phpcsFile , $ stackPtr )
139
- ) {
151
+ if ($ hasRealComment && $ this ->isInheritDoc ($ phpcsFile , $ stackPtr )) {
140
152
return ;
141
153
}
142
154
@@ -163,7 +175,7 @@ protected function processReturn(
163
175
if ($ tokens [$ i ]['code ' ] === T_RETURN
164
176
&& $ this ->isMatchingReturn ($ tokens , $ i )
165
177
) {
166
- if (null !== $ commentStart ) {
178
+ if ($ hasRealComment ) {
167
179
parent ::processReturn ($ phpcsFile , $ stackPtr , $ commentStart );
168
180
} else {
169
181
// There is no doc and we need one with @return
@@ -180,13 +192,13 @@ protected function processReturn(
180
192
181
193
/**
182
194
* @param PHP_CodeSniffer_File $phpcsFile
183
- * @param int $stackPtr
184
195
* @param int $commentStart
196
+ * @param bool $hasRealComment
185
197
*/
186
198
protected function processWhitespace (
187
199
PHP_CodeSniffer_File $ phpcsFile ,
188
- $ stackPtr ,
189
- $ commentStart
200
+ $ commentStart ,
201
+ $ hasRealComment = true
190
202
) {
191
203
$ tokens = $ phpcsFile ->getTokens ();
192
204
$ before = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ commentStart - 1 ), null , true );
@@ -202,13 +214,12 @@ protected function processWhitespace(
202
214
$ found = 0 ;
203
215
}
204
216
205
- if ($ stackPtr === $ commentStart ) {
206
- // There is no docblock
207
- $ error = 'Expected 1 blank line before function; %s found ' ;
208
- $ rule = 'SpacingBeforeFunction ' ;
209
- } else {
217
+ if ($ hasRealComment ) {
210
218
$ error = 'Expected 1 blank line before docblock; %s found ' ;
211
219
$ rule = 'SpacingBeforeDocblock ' ;
220
+ } else {
221
+ $ error = 'Expected 1 blank line before function; %s found ' ;
222
+ $ rule = 'SpacingBeforeFunction ' ;
212
223
}
213
224
214
225
$ data = array ($ found );
@@ -274,7 +285,7 @@ protected function processParams(
274
285
return ;
275
286
}
276
287
277
- $ this ->processWhitespace ($ phpcsFile , $ stackPtr , $ commentStart );
288
+ $ this ->processWhitespace ($ phpcsFile , $ commentStart );
278
289
279
290
parent ::processParams ($ phpcsFile , $ stackPtr , $ commentStart );
280
291
}
0 commit comments