Conditions | 16 |
Paths | 78 |
Total Lines | 69 |
Lines | 5 |
Ratio | 7.25 % |
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 |
||
85 | public function process_matched_token( $stack_ptr, $group_name, $matched_content ) { |
||
86 | |||
87 | if ( ! $this->verify_valid_match( $stack_ptr ) ) { |
||
88 | return; |
||
89 | } |
||
90 | |||
91 | $previous_comment = $this->return_previous_comment( $stack_ptr ); |
||
92 | |||
93 | if ( false !== $previous_comment ) { |
||
94 | /* |
||
95 | * Check to determine if there is a comment immediately preceding the function call. |
||
96 | */ |
||
97 | if ( ( $this->tokens[ $previous_comment ]['line'] + 1 ) !== $this->tokens[ $stack_ptr ]['line'] ) { |
||
98 | $this->phpcsFile->addError( |
||
99 | 'The inline documentation for a hook must be on the line immediately before the function call.', |
||
100 | $stack_ptr, |
||
101 | 'DocMustBePreceding' |
||
102 | ); |
||
103 | } |
||
104 | |||
105 | /* |
||
106 | * Check that the comment starts is a docblock. |
||
107 | */ |
||
108 | if ( \T_DOC_COMMENT_CLOSE_TAG !== $this->tokens[ $previous_comment ]['code'] ) { |
||
109 | $this->phpcsFile->addError( |
||
110 | 'Hooks must include a docblock with /** formatting */', |
||
111 | $stack_ptr, |
||
112 | 'NoDocblockFound' |
||
113 | ); |
||
114 | } |
||
115 | |||
116 | /* |
||
117 | * Process docblock tags. |
||
118 | */ |
||
119 | $comment_end = $previous_comment; |
||
120 | $comment_start = $this->return_comment_start( $comment_end ); |
||
121 | $has = array( |
||
122 | 'since' => false, |
||
123 | ); |
||
124 | |||
125 | // The comment isn't a docblock or is documented elsewhere, so we're going to stop here. |
||
126 | if ( ! $comment_start || $this->is_previously_documented( $comment_start, $comment_end ) ) { |
||
127 | return; |
||
128 | } |
||
129 | |||
130 | foreach ( $this->tokens[ $comment_start ]['comment_tags'] as $tag ) { |
||
131 | // Is the next tag of the docblock the "@since" tag? |
||
132 | if ( '@since' === $this->tokens[ $tag ]['content'] ) { |
||
133 | $has['since'] = true; |
||
134 | // Find the next string, which will be the text after the @since. |
||
135 | $string = $this->phpcsFile->findNext( T_DOC_COMMENT_STRING, $tag, $comment_end ); |
||
136 | // If it is false, there is no text or if the text is on the another line, error. |
||
137 | if ( false === $string || $this->tokens[ $string ]['line'] !== $this->tokens[ $tag ]['line'] ) { |
||
138 | $this->phpcsFile->addError( 'Since tag must have a value.', $tag, 'EmptySince' ); |
||
139 | } elseif ( ! preg_match('/^\d+\.\d+\.\d+/', $this->tokens[ $string ]['content'], $matches ) ) { // Requires X.Y.Z. Trailing 0 is needed for a major release. |
||
140 | if ( empty( $this->allowed_extra_versions ) || ! $this->array_begins_with( $this->tokens[ $string ]['content'], $this->allowed_extra_versions ) ) { |
||
141 | $this->phpcsFile->addError( 'Since tag must have a X.Y.Z version number.' . $this->tokens[ $string ]['content'], $tag, 'InvalidSince' ); |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | |||
147 | View Code Duplication | foreach ( $has as $name => $present ) { |
|
148 | if ( ! $present ) { |
||
149 | $this->phpcsFile->addError( 'Hook documentation is missing a tag: ' . $name, $comment_start, 'No' . ucfirst( $name ) ); |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
228 |