Complex classes like BranchOutputOptimizer 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 BranchOutputOptimizer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class BranchOutputOptimizer |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var integer Number of tokens |
||
| 14 | */ |
||
| 15 | protected $cnt; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var integer Current token index |
||
| 19 | */ |
||
| 20 | protected $i; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var array Tokens from current source |
||
| 24 | */ |
||
| 25 | protected $tokens; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Optimize the code used to output content |
||
| 29 | * |
||
| 30 | * This method will go through the array of tokens, identify if/elseif/else blocks that contain |
||
| 31 | * identical code at the beginning or the end and move the common code outside of the block |
||
| 32 | * |
||
| 33 | * @param array $tokens Array of tokens from token_get_all() |
||
| 34 | * @return string Optimized code |
||
| 35 | */ |
||
| 36 | public function optimize(array $tokens) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Capture the expressions used in any number of consecutive output statements |
||
| 63 | * |
||
| 64 | * Starts looking at current index. Ends at the first token that's not part of an output |
||
| 65 | * statement |
||
| 66 | * |
||
| 67 | * @return string[] |
||
| 68 | */ |
||
| 69 | protected function captureOutput() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Capture an expression used in output at current index |
||
| 86 | * |
||
| 87 | * Ends on "." or ";" |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | protected function captureOutputExpression() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Capture the source of a control structure from its keyword to its opening brace |
||
| 123 | * |
||
| 124 | * Ends after the brace, but the brace itself is not returned |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | protected function captureStructure() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Test whether the token at current index is an if/elseif/else token |
||
| 145 | * |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | protected function isBranchToken() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Merge the branches of an if/elseif/else block |
||
| 155 | * |
||
| 156 | * Returns an array that contains the following: |
||
| 157 | * |
||
| 158 | * - before: array of PHP expressions to be output before the block |
||
| 159 | * - source: PHP code for the if block |
||
| 160 | * - after: array of PHP expressions to be output after the block |
||
| 161 | * |
||
| 162 | * @param array $branches |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | protected function mergeIfBranches(array $branches) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Merge two consecutive series of consecutive output expressions together |
||
| 195 | * |
||
| 196 | * @param array $left First series |
||
| 197 | * @param array $right Second series |
||
| 198 | * @return array Merged series |
||
| 199 | */ |
||
| 200 | protected function mergeOutput(array $left, array $right) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Optimize the "head" part of a series of branches in-place |
||
| 227 | * |
||
| 228 | * @param array &$branches Array of branches, modified in-place |
||
| 229 | * @return string[] PHP expressions removed from the "head" part of the branches |
||
| 230 | */ |
||
| 231 | protected function optimizeBranchesHead(array &$branches) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Optimize the output of given branches |
||
| 254 | * |
||
| 255 | * @param array &$branches Array of branches |
||
| 256 | * @param string $which Which end to optimize ("head" or "tail") |
||
| 257 | * @return string[] PHP expressions removed from the given part of the branches |
||
| 258 | */ |
||
| 259 | protected function optimizeBranchesOutput(array &$branches, $which) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Optimize the "tail" part of a series of branches in-place |
||
| 286 | * |
||
| 287 | * @param array &$branches Array of branches, modified in-place |
||
| 288 | * @return string[] PHP expressions removed from the "tail" part of the branches |
||
| 289 | */ |
||
| 290 | protected function optimizeBranchesTail(array &$branches) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Parse the if, elseif or else branch starting at current index |
||
| 297 | * |
||
| 298 | * Ends at the last } |
||
| 299 | * |
||
| 300 | * @return array Branch's data ("structure", "head", "body", "tail") |
||
| 301 | */ |
||
| 302 | protected function parseBranch() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Parse the if block (including elseif/else branches) starting at current index |
||
| 369 | * |
||
| 370 | * @return void |
||
| 371 | */ |
||
| 372 | protected function parseIfBlock() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Serialize a recorded branch back to PHP |
||
| 389 | * |
||
| 390 | * @param array $branch |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | protected function serializeBranch(array $branch) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Serialize a series of recorded branch back to PHP |
||
| 409 | * |
||
| 410 | * @param array $block |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | protected function serializeIfBlock(array $block) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Serialize a series of output expressions |
||
| 420 | * |
||
| 421 | * @param string[] $expressions Array of PHP expressions |
||
| 422 | * @return string PHP code used to append given expressions to the output |
||
| 423 | */ |
||
| 424 | protected function serializeOutput(array $expressions) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Serialize a token back to PHP |
||
| 436 | * |
||
| 437 | * @param array|string $token Token from token_get_all() |
||
| 438 | * @return string PHP code |
||
| 439 | */ |
||
| 440 | protected function serializeToken($token) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Attempt to move past output assignment at current index |
||
| 447 | * |
||
| 448 | * @return bool Whether if an output assignment was skipped |
||
| 449 | */ |
||
| 450 | protected function skipOutputAssignment() |
||
| 467 | } |