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 JSMin 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 JSMin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 57 | class JSMin { |
||
| 58 | const ORD_LF = 10; |
||
| 59 | const ORD_SPACE = 32; |
||
| 60 | const ACTION_KEEP_A = 1; |
||
| 61 | const ACTION_DELETE_A = 2; |
||
| 62 | const ACTION_DELETE_A_B = 3; |
||
| 63 | |||
| 64 | protected $a = "\n"; |
||
| 65 | protected $b = ''; |
||
| 66 | protected $input = ''; |
||
| 67 | protected $inputIndex = 0; |
||
| 68 | protected $inputLength = 0; |
||
| 69 | protected $lookAhead = null; |
||
| 70 | protected $output = ''; |
||
| 71 | protected $lastByteOut = ''; |
||
| 72 | protected $keptComment = ''; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Minify Javascript. |
||
| 76 | * |
||
| 77 | * @param string $js Javascript to be minified |
||
| 78 | * |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | public static function minify($js) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $input |
||
| 89 | */ |
||
| 90 | public function __construct($input) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Perform minification, return result |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | public function min() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * ACTION_KEEP_A = Output A. Copy B to A. Get the next B. |
||
| 162 | * ACTION_DELETE_A = Copy B to A. Get the next B. |
||
| 163 | * ACTION_DELETE_A_B = Get the next B. |
||
| 164 | * |
||
| 165 | * @param int $command |
||
| 166 | * @throws JSMin_UnterminatedRegExpException|JSMin_UnterminatedStringException |
||
| 167 | */ |
||
| 168 | protected function action($command) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | protected function isRegexpLiteral() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Return the next character from stdin. Watch out for lookahead. If the character is a control character, |
||
| 316 | * translate it to a space or linefeed. |
||
| 317 | * |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | protected function get() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Does $a indicate end of input? |
||
| 344 | * |
||
| 345 | * @param string $a |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | protected function isEOF($a) |
||
| 349 | { |
||
| 350 | return ord($a) <= self::ORD_LF; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get next char (without getting it). If is ctrl character, translate to a space or newline. |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | protected function peek() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Return true if the character is a letter, digit, underscore, dollar sign, or non-ASCII character. |
||
| 366 | * |
||
| 367 | * @param string $c |
||
| 368 | * |
||
| 369 | * @return bool |
||
| 370 | */ |
||
| 371 | protected function isAlphaNum($c) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Consume a single line comment from input (possibly retaining it) |
||
| 378 | */ |
||
| 379 | protected function consumeSingleLineComment() |
||
| 380 | { |
||
| 381 | $comment = ''; |
||
| 382 | while (true) { |
||
| 383 | $get = $this->get(); |
||
| 384 | $comment .= $get; |
||
| 385 | if (ord($get) <= self::ORD_LF) { // end of line reached |
||
| 386 | // if IE conditional comment |
||
| 387 | if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) { |
||
| 388 | $this->keptComment .= "/{$comment}"; |
||
| 389 | } |
||
| 390 | return; |
||
| 391 | } |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Consume a multiple line comment from input (possibly retaining it) |
||
| 397 | * |
||
| 398 | * @throws JSMin_UnterminatedCommentException |
||
| 399 | */ |
||
| 400 | protected function consumeMultipleLineComment() |
||
| 401 | { |
||
| 402 | $this->get(); |
||
| 403 | $comment = ''; |
||
| 404 | for(;;) { |
||
| 405 | $get = $this->get(); |
||
| 406 | if ($get === '*') { |
||
| 407 | if ($this->peek() === '/') { // end of comment reached |
||
| 408 | $this->get(); |
||
| 409 | if (0 === strpos($comment, '!')) { |
||
| 410 | // preserved by YUI Compressor |
||
| 411 | if (!$this->keptComment) { |
||
| 412 | // don't prepend a newline if two comments right after one another |
||
| 413 | $this->keptComment = "\n"; |
||
| 414 | } |
||
| 415 | $this->keptComment .= "/*!" . substr($comment, 1) . "*/\n"; |
||
| 416 | } else if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) { |
||
| 417 | // IE conditional |
||
| 418 | $this->keptComment .= "/*{$comment}*/"; |
||
| 419 | } |
||
| 420 | return; |
||
| 421 | } |
||
| 422 | } elseif ($get === null) { |
||
| 423 | throw new JSMin_UnterminatedCommentException( |
||
| 424 | "JSMin: Unterminated comment at byte {$this->inputIndex}: /*{$comment}"); |
||
| 425 | } |
||
| 426 | $comment .= $get; |
||
| 427 | } |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Get the next character, skipping over comments. Some comments may be preserved. |
||
| 432 | * |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | protected function next() |
||
| 452 | } |
||
| 453 | |||
| 457 |