@@ -50,36 +50,61 @@ public function register(): array
50
50
* @param int $stackPtr The position of the current token
51
51
* in the stack passed in $tokens.
52
52
*/
53
- public function process (File $ phpcsFile , $ stack_ptr )
53
+ public function process (File $ phpcsFile , $ stackPtr )
54
54
{
55
55
$ tokens = $ phpcsFile ->getTokens ();
56
56
57
- if (! in_array ($ tokens [ $ stack_ptr ]['content ' ], $ this ->hooks )) {
57
+ if (! in_array ($ tokens [$ stackPtr ]['content ' ], $ this ->hooks )) {
58
58
return ;
59
59
}
60
60
61
- $ previous_comment = $ phpcsFile ->findPrevious ( Tokens::$ commentTokens , ( $ stack_ptr - 1 ) );
61
+ $ previous_comment = $ phpcsFile ->findPrevious (Tokens::$ commentTokens , ( $ stackPtr - 1 ));
62
62
63
- if ( false !== $ previous_comment ) {
64
- if ( ( $ tokens [ $ previous_comment ]['line ' ] + 1 ) === $ tokens [ $ stack_ptr ]['line ' ] ) {
65
- return ;
66
- } else {
67
- $ next_non_whitespace = $ phpcsFile ->findNext ( \T_WHITESPACE , ( $ previous_comment + 1 ), $ stack_ptr , true );
63
+ if (false !== $ previous_comment ) {
64
+ $ correctly_placed = false ;
65
+
66
+ if (( $ tokens [ $ previous_comment ]['line ' ] + 1 ) === $ tokens [ $ stackPtr ]['line ' ]) {
67
+ $ correctly_placed = true ;
68
+ }
69
+
70
+ if (true === $ correctly_placed ) {
71
+
72
+ if (\T_COMMENT === $ tokens [ $ previous_comment ]['code ' ]) {
73
+ $ phpcsFile ->addError (
74
+ 'A "hook" comment must be a "/**" style docblock comment. ' ,
75
+ $ stackPtr ,
76
+ 'HookCommentWrongStyle '
77
+ );
78
+
79
+ return ;
80
+ } elseif (\T_DOC_COMMENT_CLOSE_TAG === $ tokens [ $ previous_comment ]['code ' ]) {
81
+ $ comment_start = $ phpcsFile ->findPrevious (\T_DOC_COMMENT_OPEN_TAG , ( $ previous_comment - 1 ));
82
+
83
+ // Iterate through each comment to check for "@since" tag.
84
+ foreach ($ tokens [ $ comment_start ]['comment_tags ' ] as $ tag ) {
85
+ if ($ tokens [$ tag ]['content ' ] === '@since ' ) {
86
+ return ;
87
+ }
88
+ }
89
+
90
+ $ phpcsFile ->addError (
91
+ 'Docblock comment was found for the hook but does not contain a "@since" versioning. ' ,
92
+ $ stackPtr ,
93
+ 'MissingSinceComment '
94
+ );
68
95
69
- if ( false === $ next_non_whitespace || $ tokens [ $ next_non_whitespace ]['line ' ] === $ tokens [ $ stack_ptr ]['line ' ] ) {
70
- // No non-whitespace found or next non-whitespace is on same line as hook call.
71
96
return ;
72
97
}
73
- unset( $ next_non_whitespace );
74
98
}
75
99
}
76
100
77
- // Found hooks but no doc comment.
78
- $ phpcsFile ->addWarning (
79
- sprintf ( ' Documentation/comment needed for hook to explain what the hook does: %s ' , $ tokens [ $ stack_ptr ][ ' content ' ] ) ,
80
- $ stack_ptr ,
81
- 'MissingHooksComment '
101
+ // Found hook but no docblock comment.
102
+ $ phpcsFile ->addError (
103
+ ' A hook was found, but was not accompanied by a docblock comment on the line above to clarify the meaning of the hook. ' ,
104
+ $ stackPtr ,
105
+ 'MissingHookComment '
82
106
);
107
+
83
108
return ;
84
109
}
85
110
}
0 commit comments