Complex classes like PHPCompatibility_Sniffs_PHP_ForbiddenNamesSniff 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 PHPCompatibility_Sniffs_PHP_ForbiddenNamesSniff, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | * @category PHP |
||
| 22 | * @package PHPCompatibility |
||
| 23 | * @author Wim Godden <[email protected]> |
||
| 24 | * @copyright 2012 Cu.be Solutions bvba |
||
| 25 | */ |
||
| 26 | class ForbiddenNamesSniff extends Sniff |
||
| 27 | { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * A list of keywords that can not be used as function, class and namespace name or constant name. |
||
| 31 | * Mentions since which version it's not allowed. |
||
| 32 | * |
||
| 33 | * @var array(string => string) |
||
| 34 | */ |
||
| 35 | protected $invalidNames = array( |
||
| 36 | 'abstract' => '5.0', |
||
| 37 | 'and' => 'all', |
||
| 38 | 'array' => 'all', |
||
| 39 | 'as' => 'all', |
||
| 40 | 'break' => 'all', |
||
| 41 | 'callable' => '5.4', |
||
| 42 | 'case' => 'all', |
||
| 43 | 'catch' => '5.0', |
||
| 44 | 'class' => 'all', |
||
| 45 | 'clone' => '5.0', |
||
| 46 | 'const' => 'all', |
||
| 47 | 'continue' => 'all', |
||
| 48 | 'declare' => 'all', |
||
| 49 | 'default' => 'all', |
||
| 50 | 'do' => 'all', |
||
| 51 | 'else' => 'all', |
||
| 52 | 'elseif' => 'all', |
||
| 53 | 'enddeclare' => 'all', |
||
| 54 | 'endfor' => 'all', |
||
| 55 | 'endforeach' => 'all', |
||
| 56 | 'endif' => 'all', |
||
| 57 | 'endswitch' => 'all', |
||
| 58 | 'endwhile' => 'all', |
||
| 59 | 'extends' => 'all', |
||
| 60 | 'final' => '5.0', |
||
| 61 | 'finally' => '5.5', |
||
| 62 | 'for' => 'all', |
||
| 63 | 'foreach' => 'all', |
||
| 64 | 'function' => 'all', |
||
| 65 | 'global' => 'all', |
||
| 66 | 'goto' => '5.3', |
||
| 67 | 'if' => 'all', |
||
| 68 | 'implements' => '5.0', |
||
| 69 | 'interface' => '5.0', |
||
| 70 | 'instanceof' => '5.0', |
||
| 71 | 'insteadof' => '5.4', |
||
| 72 | 'namespace' => '5.3', |
||
| 73 | 'new' => 'all', |
||
| 74 | 'or' => 'all', |
||
| 75 | 'private' => '5.0', |
||
| 76 | 'protected' => '5.0', |
||
| 77 | 'public' => '5.0', |
||
| 78 | 'static' => 'all', |
||
| 79 | 'switch' => 'all', |
||
| 80 | 'throw' => '5.0', |
||
| 81 | 'trait' => '5.4', |
||
| 82 | 'try' => '5.0', |
||
| 83 | 'use' => 'all', |
||
| 84 | 'var' => 'all', |
||
| 85 | 'while' => 'all', |
||
| 86 | 'xor' => 'all', |
||
| 87 | '__class__' => 'all', |
||
| 88 | '__dir__' => '5.3', |
||
| 89 | '__file__' => 'all', |
||
| 90 | '__function__' => 'all', |
||
| 91 | '__method__' => 'all', |
||
| 92 | '__namespace__' => '5.3', |
||
| 93 | ); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * A list of keywords that can follow use statements. |
||
| 97 | * |
||
| 98 | * @var array(string => string) |
||
| 99 | */ |
||
| 100 | protected $validUseNames = array( |
||
| 101 | 'const' => true, |
||
| 102 | 'function' => true, |
||
| 103 | ); |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Whether PHPCS 1.x is used or not. |
||
| 107 | * |
||
| 108 | * @var bool |
||
| 109 | */ |
||
| 110 | protected $isLowPHPCS = false; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Scope modifiers and other keywords allowed in trait use statements. |
||
| 114 | * |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | private $allowed_modifiers = array(); |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Targeted tokens. |
||
| 121 | * |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | protected $targetedTokens = array( |
||
| 125 | T_CLASS, |
||
| 126 | T_FUNCTION, |
||
| 127 | T_NAMESPACE, |
||
| 128 | T_STRING, |
||
| 129 | T_CONST, |
||
| 130 | T_USE, |
||
| 131 | T_AS, |
||
| 132 | T_EXTENDS, |
||
| 133 | T_TRAIT, |
||
| 134 | T_INTERFACE, |
||
| 135 | ); |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns an array of tokens this test wants to listen for. |
||
| 139 | * |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | public function register() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Processes this test, when one of its tokens is encountered. |
||
| 161 | * |
||
| 162 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 163 | * @param int $stackPtr The position of the current token in the |
||
| 164 | * stack passed in $tokens. |
||
| 165 | * |
||
| 166 | * @return void |
||
| 167 | */ |
||
| 168 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Processes this test, when one of its tokens is encountered. |
||
| 184 | * |
||
| 185 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 186 | * @param int $stackPtr The position of the current token in the |
||
| 187 | * stack passed in $tokens. |
||
| 188 | * @param array $tokens The stack of tokens that make up |
||
| 189 | * the file. |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | public function processNonString(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Processes this test, when one of its tokens is encountered. |
||
| 328 | * |
||
| 329 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 330 | * @param int $stackPtr The position of the current token in the |
||
| 331 | * stack passed in $tokens. |
||
| 332 | * @param array $tokens The stack of tokens that make up |
||
| 333 | * the file. |
||
| 334 | * |
||
| 335 | * @return void |
||
| 336 | */ |
||
| 337 | public function processString(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) |
||
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * Add the error message. |
||
| 380 | * |
||
| 381 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 382 | * @param int $stackPtr The position of the current token in the |
||
| 383 | * stack passed in $tokens. |
||
| 384 | * @param string $content The token content found. |
||
| 385 | * @param array $data The data to pass into the error message. |
||
| 386 | * |
||
| 387 | * @return void |
||
| 388 | */ |
||
| 389 | protected function addError($phpcsFile, $stackPtr, $content, $data) |
||
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * Check if the current token code is for a token which can be considered |
||
| 399 | * the end of a (partial) use statement. |
||
| 400 | * |
||
| 401 | * @param int $token The current token information. |
||
| 402 | * |
||
| 403 | * @return bool |
||
| 404 | */ |
||
| 405 | protected function isEndOfUseStatement($token) |
||
| 410 |