Complex classes like Optimizer 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 Optimizer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Optimizer |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var BranchOutputOptimizer |
||
| 18 | */ |
||
| 19 | public $branchOutputOptimizer; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var integer Number of tokens in $this->tokens |
||
| 23 | */ |
||
| 24 | protected $cnt; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var integer Current token index |
||
| 28 | */ |
||
| 29 | protected $i; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var integer Maximum number iterations over the optimization passes |
||
| 33 | */ |
||
| 34 | public $maxLoops = 10; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array Array of tokens from token_get_all() |
||
| 38 | */ |
||
| 39 | protected $tokens; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Constructor |
||
| 43 | */ |
||
| 44 | public function __construct() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Optimize the code generated by the PHP renderer generator |
||
| 51 | * |
||
| 52 | * @param string $php Original code |
||
| 53 | * @return string Optimized code |
||
| 54 | */ |
||
| 55 | public function optimize($php) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Test whether current token is between two htmlspecialchars() calls |
||
| 112 | * |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | protected function isBetweenHtmlspecialcharCalls() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Test whether current token is at the beginning of an htmlspecialchars()-safe var |
||
| 126 | * |
||
| 127 | * Tests whether current var is either $node->localName or $node->nodeName |
||
| 128 | * |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | protected function isHtmlspecialcharSafeVar() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Test whether the cursor is at the beginning of an output assignment |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | protected function isOutputAssignment() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Test whether the cursor is immediately after the output variable |
||
| 157 | * |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | protected function isPrecededByOutputVar() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Merge concatenated htmlspecialchars() calls together |
||
| 169 | * |
||
| 170 | * Must be called when the cursor is at the concatenation operator |
||
| 171 | * |
||
| 172 | * @return bool Whether calls were merged |
||
| 173 | */ |
||
| 174 | protected function mergeConcatenatedHtmlSpecialChars() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Merge concatenated strings together |
||
| 231 | * |
||
| 232 | * Must be called when the cursor is at the concatenation operator |
||
| 233 | * |
||
| 234 | * @return bool Whether strings were merged |
||
| 235 | */ |
||
| 236 | protected function mergeConcatenatedStrings() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Optimize T_CONCAT_EQUAL assignments in an array of PHP tokens |
||
| 261 | * |
||
| 262 | * Will only optimize $this->out.= assignments |
||
| 263 | * |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | protected function optimizeOutConcatEqual() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Optimize concatenations in an array of PHP tokens |
||
| 301 | * |
||
| 302 | * - Will precompute the result of the concatenation of constant strings |
||
| 303 | * - Will replace the concatenation of two compatible htmlspecialchars() calls with one call to |
||
| 304 | * htmlspecialchars() on the concatenation of their first arguments |
||
| 305 | * |
||
| 306 | * @return void |
||
| 307 | */ |
||
| 308 | protected function optimizeConcatenations() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Optimize htmlspecialchars() calls |
||
| 319 | * |
||
| 320 | * - The result of htmlspecialchars() on literals is precomputed |
||
| 321 | * - By default, the generator escapes all values, including variables that cannot contain |
||
| 322 | * special characters such as $node->localName. This pass removes those calls |
||
| 323 | * |
||
| 324 | * @return void |
||
| 325 | */ |
||
| 326 | protected function optimizeHtmlspecialchars() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Remove htmlspecialchars() calls on variables that are known to be safe |
||
| 342 | * |
||
| 343 | * Must be called when the cursor is at the first argument of the call |
||
| 344 | * |
||
| 345 | * @return bool Whether the call was removed |
||
| 346 | */ |
||
| 347 | protected function removeHtmlspecialcharsSafeVar() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Precompute the result of a htmlspecialchars() call on a string literal |
||
| 369 | * |
||
| 370 | * Must be called when the cursor is at the first argument of the call |
||
| 371 | * |
||
| 372 | * @return bool Whether the call was replaced |
||
| 373 | */ |
||
| 374 | protected function replaceHtmlspecialcharsLiteral() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Move the cursor past given token |
||
| 406 | * |
||
| 407 | * @param array|string $token Target token |
||
| 408 | * @return bool Whether a matching token was found and the cursor is within bounds |
||
| 409 | */ |
||
| 410 | protected function skipPast($token) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Move the cursor until it reaches given token |
||
| 417 | * |
||
| 418 | * @param array|string $token Target token |
||
| 419 | * @return bool Whether a matching token was found |
||
| 420 | */ |
||
| 421 | protected function skipTo($token) |
||
| 433 | } |