@@ -44,21 +44,68 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
4444 }
4545 }
4646
47+ $ name = $ phpcsFile ->getDeclarationName ($ stackPtr );
48+ $ commentRequired = strpos ($ name , 'test ' ) !== 0
49+ && $ name !== 'setUp '
50+ && $ name !== 'tearDown ' ;
51+
4752 if ($ tokens [$ commentEnd ]['code ' ] !== T_DOC_COMMENT_CLOSE_TAG
4853 && $ tokens [$ commentEnd ]['code ' ] !== T_COMMENT
4954 ) {
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+ }
5168
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 ' );
5573
56- if (! $ commentRequired ) {
5774 return ;
5875 }
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+ }
5993 }
6094
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+ }
62109 }
63110
64111 /**
@@ -67,7 +114,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
67114 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
68115 * @param int $stackPtr The position of the current token
69116 * 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
71118 * where the comment started.
72119 *
73120 * @return void
@@ -77,8 +124,10 @@ protected function processReturn(
77124 $ stackPtr ,
78125 $ commentStart
79126 ) {
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+ ) {
82131 return ;
83132 }
84133
@@ -105,7 +154,15 @@ protected function processReturn(
105154 if ($ tokens [$ i ]['code ' ] === T_RETURN
106155 && $ this ->isMatchingReturn ($ tokens , $ i )
107156 ) {
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+
109166 break ;
110167 }
111168 }
0 commit comments