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 |
||
41 | class csstidy_print { |
||
42 | |||
43 | /** |
||
44 | * Saves the input CSS string |
||
45 | * @var string |
||
46 | * @access private |
||
47 | */ |
||
48 | public $input_css = ''; |
||
49 | /** |
||
50 | * Saves the formatted CSS string |
||
51 | * @var string |
||
52 | * @access public |
||
53 | */ |
||
54 | public $output_css = ''; |
||
55 | /** |
||
56 | * Saves the formatted CSS string (plain text) |
||
57 | * @var string |
||
58 | * @access public |
||
59 | */ |
||
60 | public $output_css_plain = ''; |
||
61 | |||
62 | /** |
||
63 | * Constructor |
||
64 | * @param array $css contains the class csstidy |
||
65 | * @access private |
||
66 | * @version 1.0 |
||
67 | */ |
||
68 | function __construct(&$css) { |
||
77 | |||
78 | function csstidy_print(&$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='') { |
||
276 | |||
277 | /** |
||
278 | * Gets the next token type which is $move away from $key, excluding comments |
||
279 | * @param integer $key current position |
||
280 | * @param integer $move move this far |
||
281 | * @return mixed a token type |
||
282 | * @access private |
||
283 | * @version 1.0 |
||
284 | */ |
||
285 | function _seeknocomment($key, $move) { |
||
298 | |||
299 | /** |
||
300 | * Converts $this->css array to a raw array ($this->tokens) |
||
301 | * @param string $default_media default @media to add to selectors without any @media |
||
302 | * @access private |
||
303 | * @version 1.0 |
||
304 | */ |
||
305 | function _convert_raw_css($default_media='') { |
||
306 | $this->tokens = array(); |
||
307 | |||
308 | foreach ($this->css as $medium => $val) { |
||
309 | if ($this->parser->get_cfg('sort_selectors')) |
||
310 | ksort($val); |
||
311 | View Code Duplication | if ( (int) $medium < DEFAULT_AT ) { |
|
312 | $this->parser->_add_token(AT_START, $medium, true); |
||
313 | } |
||
314 | elseif ($default_media) { |
||
315 | $this->parser->_add_token(AT_START, $default_media, true); |
||
316 | } |
||
317 | |||
318 | foreach ($val as $selector => $vali) { |
||
319 | if ($this->parser->get_cfg('sort_properties')) |
||
320 | ksort($vali); |
||
321 | $this->parser->_add_token(SEL_START, $selector, true); |
||
322 | |||
323 | foreach ($vali as $property => $valj) { |
||
324 | $this->parser->_add_token(PROPERTY, $property, true); |
||
325 | $this->parser->_add_token(VALUE, $valj, true); |
||
326 | } |
||
327 | |||
328 | $this->parser->_add_token(SEL_END, $selector, true); |
||
329 | } |
||
330 | |||
331 | View Code Duplication | if ( (int) $medium < DEFAULT_AT ) { |
|
332 | $this->parser->_add_token(AT_END, $medium, true); |
||
333 | } |
||
334 | elseif ($default_media) { |
||
335 | $this->parser->_add_token(AT_END, $default_media, true); |
||
336 | } |
||
337 | } |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Same as htmlspecialchars, only that chars are not replaced if $plain !== true. This makes print_code() cleaner. |
||
342 | * @param string $string |
||
343 | * @param bool $plain |
||
344 | * @return string |
||
345 | * @see csstidy_print::_print() |
||
346 | * @access private |
||
347 | * @version 1.0 |
||
348 | */ |
||
349 | function _htmlsp($string, $plain) { |
||
355 | |||
356 | /** |
||
357 | * Get compression ratio |
||
358 | * @access public |
||
359 | * @return float |
||
360 | * @version 1.2 |
||
361 | */ |
||
362 | function get_ratio() { |
||
368 | |||
369 | /** |
||
370 | * Get difference between the old and new code in bytes and prints the code if necessary. |
||
371 | * @access public |
||
372 | * @return string |
||
373 | * @version 1.1 |
||
374 | */ |
||
375 | function get_diff() { |
||
390 | |||
391 | /** |
||
392 | * Get the size of either input or output CSS in KB |
||
393 | * @param string $loc default is "output" |
||
394 | * @access public |
||
395 | * @return integer |
||
396 | * @version 1.0 |
||
397 | */ |
||
398 | function size($loc = 'output') { |
||
409 | |||
410 | } |
||
411 |
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: