Conditions | 30 |
Paths | 26 |
Total Lines | 108 |
Code Lines | 60 |
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 |
||
136 | protected function isCallTimePassByReferenceParam(\PHP_CodeSniffer_File $phpcsFile, $parameter, $nestingLevel) |
||
137 | { |
||
138 | $tokens = $phpcsFile->getTokens(); |
||
139 | |||
140 | $searchStartToken = $parameter['start'] - 1; |
||
141 | $searchEndToken = $parameter['end'] + 1; |
||
142 | $nextVariable = $searchStartToken; |
||
143 | do { |
||
144 | $nextVariable = $phpcsFile->findNext(T_VARIABLE, ($nextVariable + 1), $searchEndToken); |
||
145 | if ($nextVariable === false) { |
||
146 | return false; |
||
147 | } |
||
148 | |||
149 | // Make sure the variable belongs directly to this function call |
||
150 | // and is not inside a nested function call or array. |
||
151 | if (isset($tokens[$nextVariable]['nested_parenthesis']) === false |
||
152 | || (count($tokens[$nextVariable]['nested_parenthesis']) !== $nestingLevel) |
||
153 | ) { |
||
154 | continue; |
||
155 | } |
||
156 | |||
157 | |||
158 | // Checking this: $value = my_function(...[*]$arg...). |
||
159 | $tokenBefore = $phpcsFile->findPrevious( |
||
160 | \PHP_CodeSniffer_Tokens::$emptyTokens, |
||
161 | ($nextVariable - 1), |
||
162 | $searchStartToken, |
||
163 | true |
||
164 | ); |
||
165 | |||
166 | if ($tokenBefore === false || $tokens[$tokenBefore]['code'] !== T_BITWISE_AND) { |
||
167 | // Nothing before the token or no &. |
||
168 | continue; |
||
169 | } |
||
170 | |||
171 | // Checking this: $value = my_function(...[*]&$arg...). |
||
172 | $tokenBefore = $phpcsFile->findPrevious( |
||
173 | \PHP_CodeSniffer_Tokens::$emptyTokens, |
||
174 | ($tokenBefore - 1), |
||
175 | $searchStartToken, |
||
176 | true |
||
177 | ); |
||
178 | |||
179 | // We have to exclude all uses of T_BITWISE_AND that are not |
||
180 | // references. We use a blacklist approach as we prefer false |
||
181 | // positives to not identifying a pass-by-reference call at all. |
||
182 | // The blacklist may not yet be complete. |
||
183 | switch ($tokens[$tokenBefore]['code']) { |
||
184 | // In these cases T_BITWISE_AND represents |
||
185 | // the bitwise and operator. |
||
186 | case T_LNUMBER: |
||
187 | case T_VARIABLE: |
||
188 | case T_CLOSE_SQUARE_BRACKET: |
||
189 | case T_CLOSE_PARENTHESIS: |
||
190 | break; |
||
191 | |||
192 | // Prevent false positive on assign by reference and compare with reference |
||
193 | // with function call parameters. |
||
194 | case T_EQUAL: |
||
195 | case T_AND_EQUAL: |
||
196 | case T_OR_EQUAL: |
||
197 | case T_CONCAT_EQUAL: |
||
198 | case T_DIV_EQUAL: |
||
199 | case T_MINUS_EQUAL: |
||
200 | case T_MOD_EQUAL: |
||
201 | case T_MUL_EQUAL: |
||
202 | case T_PLUS_EQUAL: |
||
203 | case T_XOR_EQUAL: |
||
204 | case T_SL_EQUAL: |
||
205 | case T_SR_EQUAL: |
||
206 | case T_IS_EQUAL: |
||
207 | case T_IS_IDENTICAL: |
||
208 | break; |
||
209 | |||
210 | // Unfortunately the tokenizer fails to recognize global constants, |
||
211 | // class-constants and -attributes. Any of these are returned is |
||
212 | // treated as T_STRING. |
||
213 | // So we step back another token and check if it is a class |
||
214 | // operator (-> or ::), which means we have a false positive. |
||
215 | // Global constants still remain uncovered. |
||
216 | case T_STRING: |
||
217 | $tokenBeforePlus = $phpcsFile->findPrevious( |
||
218 | \PHP_CodeSniffer_Tokens::$emptyTokens, |
||
219 | ($tokenBefore - 1), |
||
220 | $searchStartToken, |
||
221 | true |
||
222 | ); |
||
223 | if ($tokens[$tokenBeforePlus]['code'] === T_DOUBLE_COLON |
||
224 | || $tokens[$tokenBeforePlus]['code'] === T_OBJECT_OPERATOR |
||
225 | ) { |
||
226 | break; |
||
227 | } |
||
228 | // If not a class constant: fall through. |
||
229 | |||
230 | default: |
||
231 | // Deal with T_POW_EQUAL which doesn't exist in PHPCS 1.x. |
||
232 | if (defined('T_POW_EQUAL') && $tokens[$tokenBefore]['type'] === 'T_POW_EQUAL') { |
||
233 | break; |
||
234 | } |
||
235 | |||
236 | // The found T_BITWISE_AND represents a pass-by-reference. |
||
237 | return true; |
||
238 | } |
||
239 | } while ($nextVariable < $searchEndToken); |
||
240 | |||
241 | // This code should never be reached, but here in case of weird bugs ;-) |
||
242 | return false; |
||
243 | } |
||
244 | |||
246 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.