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 csstidy_print 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 csstidy_print, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class csstidy_print { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Saves the input CSS string |
||
| 46 | * @var string |
||
| 47 | * @access private |
||
| 48 | */ |
||
| 49 | public $input_css = ''; |
||
| 50 | /** |
||
| 51 | * Saves the formatted CSS string |
||
| 52 | * @var string |
||
| 53 | * @access public |
||
| 54 | */ |
||
| 55 | public $output_css = ''; |
||
| 56 | /** |
||
| 57 | * Saves the formatted CSS string (plain text) |
||
| 58 | * @var string |
||
| 59 | * @access public |
||
| 60 | */ |
||
| 61 | public $output_css_plain = ''; |
||
| 62 | |||
| 63 | function csstidy_print(&$css) { |
||
| 66 | /** |
||
| 67 | * Constructor |
||
| 68 | * @param array $css contains the class csstidy |
||
| 69 | * @access private |
||
| 70 | * @version 1.0 |
||
| 71 | */ |
||
| 72 | function __construct(&$css) { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Resets output_css and output_css_plain (new css code) |
||
| 84 | * @access private |
||
| 85 | * @version 1.0 |
||
| 86 | */ |
||
| 87 | function _reset() { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns the CSS code as plain text |
||
| 94 | * @param string $default_media default @media to add to selectors without any @media |
||
| 95 | * @return string |
||
| 96 | * @access public |
||
| 97 | * @version 1.0 |
||
| 98 | */ |
||
| 99 | function plain($default_media='') { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Returns the formatted CSS code |
||
| 106 | * @param string $default_media default @media to add to selectors without any @media |
||
| 107 | * @return string |
||
| 108 | * @access public |
||
| 109 | * @version 1.0 |
||
| 110 | */ |
||
| 111 | function formatted($default_media='') { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns the formatted CSS code to make a complete webpage |
||
| 118 | * @param string $doctype shorthand for the document type |
||
| 119 | * @param bool $externalcss indicates whether styles to be attached internally or as an external stylesheet |
||
| 120 | * @param string $title title to be added in the head of the document |
||
| 121 | * @param string $lang two-letter language code to be added to the output |
||
| 122 | * @return string |
||
| 123 | * @access public |
||
| 124 | * @version 1.4 |
||
| 125 | */ |
||
| 126 | function formatted_page($doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Returns the formatted CSS Code and saves it into $this->output_css and $this->output_css_plain |
||
| 163 | * @param bool $plain plain text or not |
||
| 164 | * @param string $default_media default @media to add to selectors without any @media |
||
| 165 | * @access private |
||
| 166 | * @version 2.0 |
||
| 167 | */ |
||
| 168 | function _print($plain = false, $default_media='') { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Gets the next token type which is $move away from $key, excluding comments |
||
| 280 | * @param integer $key current position |
||
| 281 | * @param integer $move move this far |
||
| 282 | * @return mixed a token type |
||
| 283 | * @access private |
||
| 284 | * @version 1.0 |
||
| 285 | */ |
||
| 286 | function _seeknocomment($key, $move) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Converts $this->css array to a raw array ($this->tokens) |
||
| 302 | * @param string $default_media default @media to add to selectors without any @media |
||
| 303 | * @access private |
||
| 304 | * @version 1.0 |
||
| 305 | */ |
||
| 306 | function _convert_raw_css($default_media='') { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Same as htmlspecialchars, only that chars are not replaced if $plain !== true. This makes print_code() cleaner. |
||
| 343 | * @param string $string |
||
| 344 | * @param bool $plain |
||
| 345 | * @return string |
||
| 346 | * @see csstidy_print::_print() |
||
| 347 | * @access private |
||
| 348 | * @version 1.0 |
||
| 349 | */ |
||
| 350 | function _htmlsp($string, $plain) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get compression ratio |
||
| 359 | * @access public |
||
| 360 | * @return float |
||
| 361 | * @version 1.2 |
||
| 362 | */ |
||
| 363 | function get_ratio() { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Get difference between the old and new code in bytes and prints the code if necessary. |
||
| 372 | * @access public |
||
| 373 | * @return string |
||
| 374 | * @version 1.1 |
||
| 375 | */ |
||
| 376 | function get_diff() { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get the size of either input or output CSS in KB |
||
| 394 | * @param string $loc default is "output" |
||
| 395 | * @access public |
||
| 396 | * @return integer |
||
| 397 | * @version 1.0 |
||
| 398 | */ |
||
| 399 | function size($loc = 'output') { |
||
| 410 | |||
| 411 | } |
||
| 412 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: