@@ -67,7 +67,6 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6767 $ phpcsFile ->recordMetric ($ stackPtr , 'Function has doc comment ' , 'yes ' );
6868 }
6969
70- $ commentStart = null ;
7170 if ($ hasComment ) {
7271 if ($ tokens [$ commentEnd ]['code ' ] === T_COMMENT ) {
7372 $ phpcsFile ->addError (
@@ -95,17 +94,30 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
9594 }
9695 }
9796 }
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+ }
98110 }
99111
100- $ this ->processReturn ($ phpcsFile , $ stackPtr , $ commentStart );
112+ $ this ->processReturn ($ phpcsFile , $ stackPtr , $ commentStart, $ hasComment );
101113
102114 $ realParams = $ phpcsFile ->getMethodParameters ($ stackPtr );
103115 if ($ hasComment ) {
104116 // These checks need function comment
105117 $ this ->processParams ($ phpcsFile , $ stackPtr , $ commentStart );
106118 $ this ->processThrows ($ phpcsFile , $ stackPtr , $ commentStart );
107119 } else {
108- $ this ->processWhitespace ($ phpcsFile , $ stackPtr , $ stackPtr );
120+ $ this ->processWhitespace ($ phpcsFile , $ commentStart , $ hasComment );
109121
110122 if (count ($ realParams ) > 0 ) {
111123 foreach ($ realParams as $ neededParam ) {
@@ -120,23 +132,23 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
120132 /**
121133 * Process the return comment of this function comment.
122134 *
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
128141 *
129142 * @return void
130143 */
131144 protected function processReturn (
132145 PHP_CodeSniffer_File $ phpcsFile ,
133146 $ stackPtr ,
134- $ commentStart
147+ $ commentStart ,
148+ $ hasRealComment = true
135149 ) {
136150 // Check for inheritDoc if there is comment
137- if ((null !== $ commentStart )
138- && $ this ->isInheritDoc ($ phpcsFile , $ stackPtr )
139- ) {
151+ if ($ hasRealComment && $ this ->isInheritDoc ($ phpcsFile , $ stackPtr )) {
140152 return ;
141153 }
142154
@@ -163,7 +175,7 @@ protected function processReturn(
163175 if ($ tokens [$ i ]['code ' ] === T_RETURN
164176 && $ this ->isMatchingReturn ($ tokens , $ i )
165177 ) {
166- if (null !== $ commentStart ) {
178+ if ($ hasRealComment ) {
167179 parent ::processReturn ($ phpcsFile , $ stackPtr , $ commentStart );
168180 } else {
169181 // There is no doc and we need one with @return
@@ -180,13 +192,13 @@ protected function processReturn(
180192
181193 /**
182194 * @param PHP_CodeSniffer_File $phpcsFile
183- * @param int $stackPtr
184195 * @param int $commentStart
196+ * @param bool $hasRealComment
185197 */
186198 protected function processWhitespace (
187199 PHP_CodeSniffer_File $ phpcsFile ,
188- $ stackPtr ,
189- $ commentStart
200+ $ commentStart ,
201+ $ hasRealComment = true
190202 ) {
191203 $ tokens = $ phpcsFile ->getTokens ();
192204 $ before = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ commentStart - 1 ), null , true );
@@ -202,13 +214,12 @@ protected function processWhitespace(
202214 $ found = 0 ;
203215 }
204216
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 ) {
210218 $ error = 'Expected 1 blank line before docblock; %s found ' ;
211219 $ rule = 'SpacingBeforeDocblock ' ;
220+ } else {
221+ $ error = 'Expected 1 blank line before function; %s found ' ;
222+ $ rule = 'SpacingBeforeFunction ' ;
212223 }
213224
214225 $ data = array ($ found );
@@ -274,7 +285,7 @@ protected function processParams(
274285 return ;
275286 }
276287
277- $ this ->processWhitespace ($ phpcsFile , $ stackPtr , $ commentStart );
288+ $ this ->processWhitespace ($ phpcsFile , $ commentStart );
278289
279290 parent ::processParams ($ phpcsFile , $ stackPtr , $ commentStart );
280291 }
0 commit comments