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 | /** |
||
| 64 | * Constructor |
||
| 65 | * @param array $css contains the class csstidy |
||
| 66 | * @access private |
||
| 67 | * @version 1.0 |
||
| 68 | */ |
||
| 69 | function __construct(&$css) { |
||
| 78 | |||
| 79 | function csstidy_print(&$css) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Resets output_css and output_css_plain (new css code) |
||
| 85 | * @access private |
||
| 86 | * @version 1.0 |
||
| 87 | */ |
||
| 88 | function _reset() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Returns the CSS code as plain text |
||
| 95 | * @param string $default_media default @media to add to selectors without any @media |
||
| 96 | * @return string |
||
| 97 | * @access public |
||
| 98 | * @version 1.0 |
||
| 99 | */ |
||
| 100 | function plain($default_media='') { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns the formatted CSS code |
||
| 107 | * @param string $default_media default @media to add to selectors without any @media |
||
| 108 | * @return string |
||
| 109 | * @access public |
||
| 110 | * @version 1.0 |
||
| 111 | */ |
||
| 112 | function formatted($default_media='') { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Returns the formatted CSS code to make a complete webpage |
||
| 119 | * @param string $doctype shorthand for the document type |
||
| 120 | * @param bool $externalcss indicates whether styles to be attached internally or as an external stylesheet |
||
| 121 | * @param string $title title to be added in the head of the document |
||
| 122 | * @param string $lang two-letter language code to be added to the output |
||
| 123 | * @return string |
||
| 124 | * @access public |
||
| 125 | * @version 1.4 |
||
| 126 | */ |
||
| 127 | function formatted_page($doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns the formatted CSS Code and saves it into $this->output_css and $this->output_css_plain |
||
| 164 | * @param bool $plain plain text or not |
||
| 165 | * @param string $default_media default @media to add to selectors without any @media |
||
| 166 | * @access private |
||
| 167 | * @version 2.0 |
||
| 168 | */ |
||
| 169 | function _print($plain = false, $default_media='') { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Gets the next token type which is $move away from $key, excluding comments |
||
| 281 | * @param integer $key current position |
||
| 282 | * @param integer $move move this far |
||
| 283 | * @return mixed a token type |
||
| 284 | * @access private |
||
| 285 | * @version 1.0 |
||
| 286 | */ |
||
| 287 | function _seeknocomment($key, $move) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Converts $this->css array to a raw array ($this->tokens) |
||
| 303 | * @param string $default_media default @media to add to selectors without any @media |
||
| 304 | * @access private |
||
| 305 | * @version 1.0 |
||
| 306 | */ |
||
| 307 | function _convert_raw_css($default_media='') { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Same as htmlspecialchars, only that chars are not replaced if $plain !== true. This makes print_code() cleaner. |
||
| 344 | * @param string $string |
||
| 345 | * @param bool $plain |
||
| 346 | * @return string |
||
| 347 | * @see csstidy_print::_print() |
||
| 348 | * @access private |
||
| 349 | * @version 1.0 |
||
| 350 | */ |
||
| 351 | function _htmlsp($string, $plain) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Get compression ratio |
||
| 360 | * @access public |
||
| 361 | * @return float |
||
| 362 | * @version 1.2 |
||
| 363 | */ |
||
| 364 | function get_ratio() { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get difference between the old and new code in bytes and prints the code if necessary. |
||
| 373 | * @access public |
||
| 374 | * @return string |
||
| 375 | * @version 1.1 |
||
| 376 | */ |
||
| 377 | function get_diff() { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Get the size of either input or output CSS in KB |
||
| 395 | * @param string $loc default is "output" |
||
| 396 | * @access public |
||
| 397 | * @return integer |
||
| 398 | * @version 1.0 |
||
| 399 | */ |
||
| 400 | function size($loc = 'output') { |
||
| 411 | |||
| 412 | } |
||
| 413 |
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: