Completed
Push — add/cs-package ( b423a9...64f35d )
by
unknown
21:19 queued 14:28
created

process_matched_token()   B

Complexity

Conditions 11
Paths 15

Size

Total Lines 46

Duplication

Lines 5
Ratio 10.87 %

Importance

Changes 0
Metric Value
cc 11
nc 15
nop 3
dl 5
loc 46
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $previous_comment of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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