|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This sniff verifies a Jetpack hook function has a preceding docblock with Jetpack-specific tags. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-coding-standards |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\CodingStandards\Sniffs\InlineDocs; |
|
9
|
|
|
|
|
10
|
|
|
use Automattic\Jetpack\CodingStandards\Sniffs\InlineDocs\HooksMustHaveDocblockSniff as HooksMustHaveDocblockSniff; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class JetpackHooksRequirementsSniff |
|
14
|
|
|
* |
|
15
|
|
|
* Custom hook docblock requirements for Jetpack. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Automattic\Jetpack\CodingStandards\Sniffs\InlineDocs |
|
18
|
|
|
*/ |
|
19
|
|
|
class JetpackHooksRequirementsSniff extends HooksMustHaveDocblockSniff { |
|
20
|
|
|
/** |
|
21
|
|
|
* Process a matched token. |
|
22
|
|
|
* |
|
23
|
|
|
* @since 1.0.0 Logic split off from the `process_token()` method. |
|
24
|
|
|
* |
|
25
|
|
|
* @param int $stack_ptr The position of the current token in the stack. |
|
26
|
|
|
* @param string $group_name The name of the group which was matched. |
|
27
|
|
|
* @param string $matched_content The token content (function name) which was matched. |
|
28
|
|
|
* |
|
29
|
|
|
* @return int|void Integer stack pointer to skip forward or void to continue |
|
30
|
|
|
* normal file processing. |
|
31
|
|
|
*/ |
|
32
|
|
|
public function process_matched_token( $stack_ptr, $group_name, $matched_content ) { |
|
33
|
|
|
if ( ! $this->verify_valid_match( $stack_ptr ) ) { |
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$previous_comment = $this->return_previous_comment( $stack_ptr ); |
|
38
|
|
|
|
|
39
|
|
|
if ( ! $previous_comment ) { |
|
|
|
|
|
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/* |
|
44
|
|
|
* Process docblock tags. |
|
45
|
|
|
*/ |
|
46
|
|
|
$comment_end = $previous_comment; |
|
47
|
|
|
$comment_start = $this->return_comment_start( $comment_end ); |
|
48
|
|
|
$has = array( |
|
49
|
|
|
'module' => false, |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
// The comment isn't a docblock or is documented elsewhere, so we're going to stop here. |
|
53
|
|
|
if ( ! $comment_start || $this->is_previously_documented( $comment_start, $comment_end ) ) { |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
foreach ( $this->tokens[ $comment_start ]['comment_tags'] as $tag ) { |
|
58
|
|
|
// Is the next tag of the docblock the "@module" tag? |
|
59
|
|
|
if ( '@module' === $this->tokens[ $tag ]['content'] ) { |
|
60
|
|
|
// This is used later to determine if we need to throw an error for no module tag. |
|
61
|
|
|
$has['module'] = true; |
|
62
|
|
|
|
|
63
|
|
|
// Find the next string, which will be the text after the @module. |
|
64
|
|
|
$string = $this->phpcsFile->findNext( T_DOC_COMMENT_STRING, $tag, $comment_end ); |
|
65
|
|
|
// If it is false, there is no text or if the text is on the another line, error. |
|
66
|
|
|
if ( false === $string || $this->tokens[ $string ]['line'] !== $this->tokens[ $tag ]['line'] ) { |
|
67
|
|
|
$this->phpcsFile->addError( 'Module tag must have a value.', $tag, 'EmptyModule' ); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
View Code Duplication |
foreach ( $has as $name => $present ) { |
|
73
|
|
|
if ( ! $present ) { |
|
74
|
|
|
$this->phpcsFile->addError( 'Hook documentation is missing a tag: ' . $name, $comment_start, 'No' . ucfirst( $name ) ); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: