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 |
||
59 | class JSMin { |
||
60 | const ORD_LF = 10; |
||
61 | const ORD_SPACE = 32; |
||
62 | const ACTION_KEEP_A = 1; |
||
63 | const ACTION_DELETE_A = 2; |
||
64 | const ACTION_DELETE_A_B = 3; |
||
65 | |||
66 | protected $a = "\n"; |
||
67 | protected $b = ''; |
||
68 | protected $input = ''; |
||
69 | protected $inputIndex = 0; |
||
70 | protected $inputLength = 0; |
||
71 | protected $lookAhead = null; |
||
72 | protected $output = ''; |
||
73 | protected $lastByteOut = ''; |
||
74 | protected $keptComment = ''; |
||
75 | |||
76 | /** |
||
77 | * Minify Javascript. |
||
78 | * |
||
79 | * @param string $js Javascript to be minified |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | public static function minify($js) |
||
88 | |||
89 | /** |
||
90 | * @param string $input |
||
91 | */ |
||
92 | public function __construct($input) |
||
96 | |||
97 | /** |
||
98 | * Perform minification, return result |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | public function min() |
||
161 | |||
162 | /** |
||
163 | * ACTION_KEEP_A = Output A. Copy B to A. Get the next B. |
||
164 | * ACTION_DELETE_A = Copy B to A. Get the next B. |
||
165 | * ACTION_DELETE_A_B = Get the next B. |
||
166 | * |
||
167 | * @param int $command |
||
168 | * @throws JSMin_UnterminatedRegExpException|JSMin_UnterminatedStringException |
||
169 | */ |
||
170 | protected function action($command) |
||
277 | |||
278 | /** |
||
279 | * @return bool |
||
280 | */ |
||
281 | protected function isRegexpLiteral() |
||
318 | |||
319 | /** |
||
320 | * Return the next character from stdin. Watch out for lookahead. If the character is a control character, |
||
321 | * translate it to a space or linefeed. |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | protected function get() |
||
346 | |||
347 | /** |
||
348 | * Does $a indicate end of input? |
||
349 | * |
||
350 | * @param string $a |
||
351 | * @return bool |
||
352 | */ |
||
353 | protected function isEOF($a) |
||
354 | { |
||
355 | return ord($a) <= self::ORD_LF; |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Get next char (without getting it). If is ctrl character, translate to a space or newline. |
||
360 | * |
||
361 | * @return string |
||
362 | */ |
||
363 | protected function peek() |
||
368 | |||
369 | /** |
||
370 | * Return true if the character is a letter, digit, underscore, dollar sign, or non-ASCII character. |
||
371 | * |
||
372 | * @param string $c |
||
373 | * |
||
374 | * @return bool |
||
375 | */ |
||
376 | protected function isAlphaNum($c) |
||
380 | |||
381 | /** |
||
382 | * Consume a single line comment from input (possibly retaining it) |
||
383 | */ |
||
384 | protected function consumeSingleLineComment() |
||
385 | { |
||
386 | $comment = ''; |
||
387 | while (true) { |
||
388 | $get = $this->get(); |
||
389 | $comment .= $get; |
||
390 | if (ord($get) <= self::ORD_LF) { // end of line reached |
||
391 | // if IE conditional comment |
||
392 | if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) { |
||
393 | $this->keptComment .= "/{$comment}"; |
||
394 | } |
||
395 | return; |
||
396 | } |
||
397 | } |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Consume a multiple line comment from input (possibly retaining it) |
||
402 | * |
||
403 | * @throws JSMin_UnterminatedCommentException |
||
404 | */ |
||
405 | protected function consumeMultipleLineComment() |
||
406 | { |
||
407 | $this->get(); |
||
408 | $comment = ''; |
||
409 | for(;;) { |
||
410 | $get = $this->get(); |
||
411 | if ($get === '*') { |
||
412 | if ($this->peek() === '/') { // end of comment reached |
||
413 | $this->get(); |
||
414 | if (0 === strpos($comment, '!')) { |
||
415 | // preserved by YUI Compressor |
||
416 | if (!$this->keptComment) { |
||
417 | // don't prepend a newline if two comments right after one another |
||
418 | $this->keptComment = "\n"; |
||
419 | } |
||
420 | $this->keptComment .= "/*!" . substr($comment, 1) . "*/\n"; |
||
421 | } else if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) { |
||
422 | // IE conditional |
||
423 | $this->keptComment .= "/*{$comment}*/"; |
||
424 | } |
||
425 | return; |
||
426 | } |
||
427 | } elseif ($get === null) { |
||
428 | throw new JSMin_UnterminatedCommentException( |
||
429 | "JSMin: Unterminated comment at byte {$this->inputIndex}: /*{$comment}"); |
||
430 | } |
||
431 | $comment .= $get; |
||
432 | } |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Get the next character, skipping over comments. Some comments may be preserved. |
||
437 | * |
||
438 | * @return string |
||
439 | */ |
||
440 | protected function next() |
||
457 | } |
||
458 | |||
459 | class JSMin_UnterminatedStringException extends Exception {} |
||
460 | class JSMin_UnterminatedCommentException extends Exception {} |
||
462 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.