Skip to content

Commit 4576d54

Browse files
Merge pull request #27 from woocommerce/tweak/comment-hooks
Tweak/comment hooks
2 parents 5566270 + 6083198 commit 4576d54

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

src/WooCommerce/Sniffs/Commenting/CommentHooksSniff.php

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,36 +50,61 @@ public function register(): array
5050
* @param int $stackPtr The position of the current token
5151
* in the stack passed in $tokens.
5252
*/
53-
public function process(File $phpcsFile, $stack_ptr)
53+
public function process(File $phpcsFile, $stackPtr)
5454
{
5555
$tokens = $phpcsFile->getTokens();
5656

57-
if (! in_array($tokens[ $stack_ptr ]['content'], $this->hooks)) {
57+
if (! in_array($tokens[$stackPtr]['content'], $this->hooks)) {
5858
return;
5959
}
6060

61-
$previous_comment = $phpcsFile->findPrevious( Tokens::$commentTokens, ( $stack_ptr - 1 ) );
61+
$previous_comment = $phpcsFile->findPrevious(Tokens::$commentTokens, ( $stackPtr - 1 ));
6262

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+
);
6895

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.
7196
return;
7297
}
73-
unset( $next_non_whitespace );
7498
}
7599
}
76100

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'
82106
);
107+
83108
return;
84109
}
85110
}

0 commit comments

Comments
 (0)