| Conditions | 20 |
| Paths | 330 |
| Total Lines | 90 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 67 | public function process_matched_token( $stack_ptr, $group_name, $matched_content ) { |
||
| 68 | |||
| 69 | $func_open_paren_token = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stack_ptr + 1 ), null, true ); |
||
| 70 | if ( false === $func_open_paren_token |
||
| 71 | || \T_OPEN_PARENTHESIS !== $this->tokens[ $func_open_paren_token ]['code'] |
||
| 72 | || ! isset( $this->tokens[ $func_open_paren_token ]['parenthesis_closer'] ) |
||
| 73 | ) { |
||
| 74 | // Live coding, parse error or not a function call. |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | |||
| 78 | $previous_comment = $this->phpcsFile->findPrevious( Tokens::$commentTokens, ( $stack_ptr - 1 ) ); |
||
| 79 | |||
| 80 | if ( false !== $previous_comment ) { |
||
| 81 | /* |
||
| 82 | * Check to determine if there is a comment immediately preceding the function call. |
||
| 83 | */ |
||
| 84 | if ( ( $this->tokens[ $previous_comment ]['line'] + 1 ) !== $this->tokens[ $stack_ptr ]['line'] ) { |
||
| 85 | $this->phpcsFile->addError( |
||
| 86 | 'The inline documentation for a hook must be on the line immediately before the function call.', |
||
| 87 | $stack_ptr, |
||
| 88 | 'DocMustBePreceding' |
||
| 89 | ); |
||
| 90 | } |
||
| 91 | |||
| 92 | /* |
||
| 93 | * Check that the comment starts is a docblock. |
||
| 94 | */ |
||
| 95 | if ( \T_DOC_COMMENT_CLOSE_TAG !== $this->tokens[ $previous_comment ]['code'] ) { |
||
| 96 | $this->phpcsFile->addError( |
||
| 97 | 'Hooks must include a docblock with /** formatting */', |
||
| 98 | $stack_ptr, |
||
| 99 | 'NoDocblockFound' |
||
| 100 | ); |
||
| 101 | // return; Do we need to return here? Can we keep going? |
||
| 102 | } |
||
| 103 | |||
| 104 | /* |
||
| 105 | * Process docblock tags. |
||
| 106 | */ |
||
| 107 | $comment_end = $previous_comment; |
||
| 108 | $comment_start = ( isset( $this->tokens[ $comment_end ]['comment_opener'] ) ) ? $this->tokens[ $comment_end ]['comment_opener'] : false; |
||
| 109 | $has = array( |
||
| 110 | 'module' => false, |
||
| 111 | 'since' => false, |
||
| 112 | ); |
||
| 113 | |||
| 114 | // The comment isn't a docblock, so we're going to stop here. |
||
| 115 | if ( ! $comment_start ) { |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | |||
| 119 | $string = $this->phpcsFile->findNext( T_DOC_COMMENT_STRING, $comment_start, $comment_end ); |
||
| 120 | // If the call is documented elsewhere, stop here. |
||
| 121 | if ( 0 === strpos( $this->tokens[ $string ]['content'], 'This filter is documented in' ) ) { |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | |||
| 125 | foreach ( $this->tokens[ $comment_start ]['comment_tags'] as $tag ) { |
||
| 126 | // Is the next tag of the docblock the "@module" tag? |
||
| 127 | if ( '@module' === $this->tokens[ $tag ]['content'] ) { |
||
| 128 | // This is used later to determine if we need to throw an error for no module tag. |
||
| 129 | $has['module'] = true; |
||
| 130 | |||
| 131 | // Find the next string, which will be the text after the @module. |
||
| 132 | $string = $this->phpcsFile->findNext( T_DOC_COMMENT_STRING, $tag, $comment_end ); |
||
| 133 | // If it is false, there is no text or if the text is on the another line, error. |
||
| 134 | if ( false === $string || $this->tokens[ $string ]['line'] !== $this->tokens[ $tag ]['line'] ) { |
||
| 135 | $this->phpcsFile->addError( 'Module tag must have a value.', $tag, 'EmptyModule' ); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | if ( '@since' === $this->tokens[ $tag ]['content'] ) { |
||
| 140 | $has['since'] = true; |
||
| 141 | $string = $this->phpcsFile->findNext( T_DOC_COMMENT_STRING, $tag, $comment_end ); |
||
| 142 | if ( false === $string || $this->tokens[ $string ]['line'] !== $this->tokens[ $tag ]['line'] ) { |
||
| 143 | $this->phpcsFile->addError( 'Since tag must have a value.', $tag, 'EmptySince' ); |
||
| 144 | } elseif ( ! preg_match( '\'/^\d+\.\d+\.\d+/\'', $string ) ) { // Requires X.Y.Z. Trailing 0 is needed for a major release. |
||
| 145 | $this->phpcsFile->addError( 'Since tag must have a X.Y.Z. version number.', $tag, 'InvalidSince' ); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | foreach ( $has as $name => $present ) { |
||
| 151 | if ( ! $present ) { |
||
| 152 | $this->phpcsFile->addError( 'Hook documentation is missing a tag: ' . $name, $comment_start, 'No' . ucfirst( $name ) ); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 |