Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ValidVariableNameSniff often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ValidVariableNameSniff, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class ValidVariableNameSniff extends AbstractVariableSniff |
||
| 44 | { |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * Processes class member variables. |
||
| 49 | * |
||
| 50 | * @param File $phpcsFile The file being scanned. |
||
| 51 | * @param int $stackPtr The position of the current token |
||
| 52 | * in the stack passed in $tokens. |
||
| 53 | * |
||
| 54 | * @return void |
||
| 55 | */ |
||
| 56 | protected function processMemberVar(File $phpcsFile, $stackPtr) |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * Processes normal variables. |
||
| 79 | * |
||
| 80 | * @param File $phpcsFile The file where this token was found. |
||
| 81 | * @param int $stackPtr The position where the token was found. |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | protected function processVariable(File $phpcsFile, $stackPtr) |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Processes variables in double quoted strings. |
||
| 108 | * |
||
| 109 | * @param File $phpcsFile The file where this token was found. |
||
| 110 | * @param int $stackPtr The position where the token was found. |
||
| 111 | * |
||
| 112 | * @return void |
||
| 113 | */ |
||
| 114 | protected function processVariableInString(File $phpcsFile, $stackPtr) |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * Checks that the variable name is all in lower case, else it add an error |
||
| 141 | * to $phpcsFile. Returns true if variable name is all in lower case, false |
||
| 142 | * otherwise. |
||
| 143 | * |
||
| 144 | * @param File $phpcsFile The current file being processed. |
||
| 145 | * @param int $stackPtr The position of the current token |
||
| 146 | * in the stack passed in $tokens. |
||
| 147 | * @param string $varName The name of the variable to |
||
| 148 | * procced without $, { nor }. |
||
| 149 | * |
||
| 150 | * @return bool true if variable name is all in lower case, false otherwise. |
||
| 151 | */ |
||
| 152 | protected function checkLowerCase(File $phpcsFile, $stackPtr, $varName) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Checks that an underscore is used at the beginning of a variable only if |
||
| 176 | * it is about a private variable. If it isn't a private variable, then it |
||
| 177 | * must not be prefixed with an underscore. Returns true if $varName is |
||
| 178 | * properly prefixed according to the variable visibility provided in |
||
| 179 | * $varProps, false otherwise. |
||
| 180 | * |
||
| 181 | * @param File $phpcsFile The current file being processed. |
||
| 182 | * @param int $stackPtr The position of the current token |
||
| 183 | * in the stack passed in $tokens. |
||
| 184 | * @param string $varName The name of the variable to |
||
| 185 | * procced without $, { nor }. |
||
| 186 | * @param array $varProps Member variable properties like |
||
| 187 | * its visibility. |
||
| 188 | * |
||
| 189 | * @return bool true if variable name is prefixed with an underscore only |
||
| 190 | * when it is about a private variable, false otherwise. |
||
| 191 | */ |
||
| 192 | protected function checkVisibilityPrefix(File $phpcsFile, $stackPtr, $varName, $varProps) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Checks that variable name length is not too short. Returns true, if it |
||
| 217 | * meets minimum length requirement, false otherwise. |
||
| 218 | * |
||
| 219 | * A variable name is too short if it is shorter than the minimal |
||
| 220 | * length and it isn't in the list of allowed short names nor declared in a |
||
| 221 | * for loop (in which it would be nested). |
||
| 222 | * The minimal length is defined in the function. It is 3 chars now. |
||
| 223 | * The list of allowed short names is defined in the function. |
||
| 224 | * It is case-sensitive. It contains only 'ci' now. |
||
| 225 | * |
||
| 226 | * @param File $phpcsFile The current file being processed. |
||
| 227 | * @param int $stackPtr The position of the current token |
||
| 228 | * in the stack passed in $tokens. |
||
| 229 | * @param string $varName The name of the variable to |
||
| 230 | * procced without $, { nor }. |
||
| 231 | * |
||
| 232 | * @return bool false if variable name $varName is shorter than the minimal |
||
| 233 | * length and it isn't in the list of allowed short names nor declared in a |
||
| 234 | * for loop (in which it would be nested), otherwise true. |
||
| 235 | */ |
||
| 236 | protected function checkLength(File $phpcsFile, $stackPtr, $varName) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Returns the position of closest previous T_FOR, if token associated with |
||
| 270 | * $stackPtr in $phpcsFile is in a for loop, otherwise false. |
||
| 271 | * |
||
| 272 | * @param File $phpcsFile The current file being processed. |
||
| 273 | * @param int $stackPtr The position of the current token |
||
| 274 | * in the stack passed in $tokens. |
||
| 275 | * @param string $varName The name of the variable to |
||
| 276 | * procced without $, { nor }. |
||
| 277 | * |
||
| 278 | * @return int|bool Position of T_FOR if token associated with $stackPtr in |
||
| 279 | * $phpcsFile is in the head of a for loop, otherwise false. |
||
| 280 | */ |
||
| 281 | private static function _isInForLoop(File $phpcsFile, $stackPtr, $varName) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Returns the position of closest previous T_FOR, if token associated with |
||
| 305 | * $stackPtr in $phpcsFile is in the head of a for loop, otherwise false. |
||
| 306 | * The head is the code placed between parenthesis next to the key word |
||
| 307 | * 'for' : for (<loop_head>) {<loop_body>}. |
||
| 308 | * |
||
| 309 | * @param File $phpcsFile The current file being processed. |
||
| 310 | * @param int $stackPtr The position of the current token |
||
| 311 | * in the stack passed in $tokens. |
||
| 312 | * |
||
| 313 | * @return int|bool Position of T_FOR if token associated with $stackPtr in |
||
| 314 | * $phpcsFile is in the head of a for loop, otherwise false. |
||
| 315 | */ |
||
| 316 | private static function _isInForLoopHead(File $phpcsFile, $stackPtr) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Returns the position of closest previous T_FOR, if token associated with |
||
| 346 | * $stackPtr in $phpcsFile is in the body of a for loop, otherwise false. |
||
| 347 | * The body are the instructions placed after parenthesis of a 'for' |
||
| 348 | * declaration, enclosed with curly brackets usually. |
||
| 349 | * 'for' : for (<loop_head>) {<loop_body>}. |
||
| 350 | * |
||
| 351 | * @param File $phpcsFile The current file being processed. |
||
| 352 | * @param int $stackPtr The position of the current token |
||
| 353 | * in the stack passed in $tokens. |
||
| 354 | * |
||
| 355 | * @return int|bool Position of T_FOR if token associated with $stackPtr in |
||
| 356 | * $phpcsFile is in the body of a for loop, otherwise false. |
||
| 357 | */ |
||
| 358 | private static function _isInForLoopBody(File $phpcsFile, $stackPtr) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Returns true if a variable declared in the head of the for loop pointed |
||
| 439 | * by $forPtr in file $phpcsFile has the name $varName. |
||
| 440 | * |
||
| 441 | * @param File $phpcsFile The current file being processed. |
||
| 442 | * @param int $forPtr The position of the 'for' token |
||
| 443 | * in the stack passed in $tokens. |
||
| 444 | * @param string $varName The name of the variable to |
||
| 445 | * procced without $, { nor }. |
||
| 446 | * |
||
| 447 | * @return int|bool true if a variable declared in the head of the for loop |
||
| 448 | * pointed by $forPtr in file $phpcsFile has the name $varName. |
||
| 449 | */ |
||
| 450 | private static function _isDeclaredInForLoop(File $phpcsFile, $forPtr, $varName) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Returns list of pointers to variables declared in for loop associated to |
||
| 471 | * $forPtr in file $phpcsFile. |
||
| 472 | * |
||
| 473 | * All pointers in the result list are pointing to token with code |
||
| 474 | * T_VARIABLE. An exception is raised, if $forPtr doesn't point a token with |
||
| 475 | * code T_FOR. |
||
| 476 | * |
||
| 477 | * @param File $phpcsFile The current file being processed. |
||
| 478 | * @param int $forPtr The position of the current token |
||
| 479 | * in the stack passed in $tokens. |
||
| 480 | * |
||
| 481 | * @return array List of pointers to variables declared in for loop $forPtr. |
||
| 482 | */ |
||
| 483 | private static function _getVarDeclaredInFor(File $phpcsFile, $forPtr) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Returns the position of first occurrence of a PHP variable starting with |
||
| 512 | * $ in $haystack from $offset. |
||
| 513 | * |
||
| 514 | * @param string $haystack The string to search in. |
||
| 515 | * @param int $offset The optional offset parameter allows you to |
||
| 516 | * specify which character in haystack to start |
||
| 517 | * searching. The returned position is still |
||
| 518 | * relative to the beginning of haystack. |
||
| 519 | * |
||
| 520 | * @return mixed The position as an integer |
||
| 521 | * or the boolean false, if no variable is found. |
||
| 522 | */ |
||
| 523 | private static function _getVariablePosition($haystack, $offset = 0) |
||
| 560 | }//end class |
||
| 561 | |||
| 563 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.