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 csstidy_print(&$css) { |
||
|
|||
70 | $this->parser = & $css; |
||
71 | $this->css = & $css->css; |
||
72 | $this->template = & $css->template; |
||
73 | $this->tokens = & $css->tokens; |
||
74 | $this->charset = & $css->charset; |
||
75 | $this->import = & $css->import; |
||
76 | $this->namespace = & $css->namespace; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Resets output_css and output_css_plain (new css code) |
||
81 | * @access private |
||
82 | * @version 1.0 |
||
83 | */ |
||
84 | function _reset() { |
||
88 | |||
89 | /** |
||
90 | * Returns the CSS code as plain text |
||
91 | * @param string $default_media default @media to add to selectors without any @media |
||
92 | * @return string |
||
93 | * @access public |
||
94 | * @version 1.0 |
||
95 | */ |
||
96 | function plain($default_media='') { |
||
100 | |||
101 | /** |
||
102 | * Returns the formatted CSS code |
||
103 | * @param string $default_media default @media to add to selectors without any @media |
||
104 | * @return string |
||
105 | * @access public |
||
106 | * @version 1.0 |
||
107 | */ |
||
108 | function formatted($default_media='') { |
||
112 | |||
113 | /** |
||
114 | * Returns the formatted CSS code to make a complete webpage |
||
115 | * @param string $doctype shorthand for the document type |
||
116 | * @param bool $externalcss indicates whether styles to be attached internally or as an external stylesheet |
||
117 | * @param string $title title to be added in the head of the document |
||
118 | * @param string $lang two-letter language code to be added to the output |
||
119 | * @return string |
||
120 | * @access public |
||
121 | * @version 1.4 |
||
122 | */ |
||
123 | function formatted_page($doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') { |
||
124 | switch ($doctype) { |
||
125 | case 'xhtml1.0strict': |
||
126 | $doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
||
127 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; |
||
128 | break; |
||
129 | case 'xhtml1.1': |
||
130 | default: |
||
131 | $doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" |
||
132 | "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'; |
||
133 | break; |
||
134 | } |
||
135 | |||
136 | $output = $cssparsed = ''; |
||
137 | $this->output_css_plain = & $output; |
||
138 | |||
139 | $output .= $doctype_output . "\n" . '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . $lang . '"'; |
||
140 | $output .= ( $doctype === 'xhtml1.1') ? '>' : ' lang="' . $lang . '">'; |
||
141 | $output .= "\n<head>\n <title>$title</title>"; |
||
142 | |||
143 | if ($externalcss) { |
||
144 | $output .= "\n <style type=\"text/css\">\n"; |
||
145 | $cssparsed = file_get_contents('cssparsed.css'); |
||
146 | $output .= $cssparsed; // Adds an invisible BOM or something, but not in css_optimised.php |
||
147 | $output .= "\n</style>"; |
||
148 | } else { |
||
149 | $output .= "\n" . ' <link rel="stylesheet" type="text/css" href="cssparsed.css" />'; |
||
150 | // } |
||
151 | } |
||
152 | $output .= "\n</head>\n<body><code id=\"copytext\">"; |
||
153 | $output .= $this->formatted(); |
||
154 | $output .= '</code>' . "\n" . '</body></html>'; |
||
155 | return $this->output_css_plain; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Returns the formatted CSS Code and saves it into $this->output_css and $this->output_css_plain |
||
160 | * @param bool $plain plain text or not |
||
161 | * @param string $default_media default @media to add to selectors without any @media |
||
162 | * @access private |
||
163 | * @version 2.0 |
||
164 | */ |
||
165 | function _print($plain = false, $default_media='') { |
||
274 | |||
275 | /** |
||
276 | * Gets the next token type which is $move away from $key, excluding comments |
||
277 | * @param integer $key current position |
||
278 | * @param integer $move move this far |
||
279 | * @return mixed a token type |
||
280 | * @access private |
||
281 | * @version 1.0 |
||
282 | */ |
||
283 | function _seeknocomment($key, $move) { |
||
284 | $go = ($move > 0) ? 1 : -1; |
||
285 | for ($i = $key + 1; abs($key - $i) - 1 < abs($move); $i += $go) { |
||
286 | if (!isset($this->tokens[$i])) { |
||
287 | return; |
||
288 | } |
||
289 | if ($this->tokens[$i][0] == COMMENT) { |
||
290 | $move += 1; |
||
291 | continue; |
||
292 | } |
||
293 | return $this->tokens[$i][0]; |
||
294 | } |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Converts $this->css array to a raw array ($this->tokens) |
||
299 | * @param string $default_media default @media to add to selectors without any @media |
||
300 | * @access private |
||
301 | * @version 1.0 |
||
302 | */ |
||
303 | function _convert_raw_css($default_media='') { |
||
304 | $this->tokens = array(); |
||
305 | |||
306 | foreach ($this->css as $medium => $val) { |
||
307 | if ($this->parser->get_cfg('sort_selectors')) |
||
308 | ksort($val); |
||
309 | View Code Duplication | if (intval($medium) < DEFAULT_AT) { |
|
310 | $this->parser->_add_token(AT_START, $medium, true); |
||
311 | } |
||
312 | elseif ($default_media) { |
||
313 | $this->parser->_add_token(AT_START, $default_media, true); |
||
314 | } |
||
315 | |||
316 | foreach ($val as $selector => $vali) { |
||
317 | if ($this->parser->get_cfg('sort_properties')) |
||
318 | ksort($vali); |
||
319 | $this->parser->_add_token(SEL_START, $selector, true); |
||
320 | |||
321 | foreach ($vali as $property => $valj) { |
||
322 | $this->parser->_add_token(PROPERTY, $property, true); |
||
323 | $this->parser->_add_token(VALUE, $valj, true); |
||
324 | } |
||
325 | |||
326 | $this->parser->_add_token(SEL_END, $selector, true); |
||
327 | } |
||
328 | |||
329 | View Code Duplication | if (intval($medium) < DEFAULT_AT) { |
|
330 | $this->parser->_add_token(AT_END, $medium, true); |
||
331 | } |
||
332 | elseif ($default_media) { |
||
333 | $this->parser->_add_token(AT_END, $default_media, true); |
||
334 | } |
||
335 | } |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Same as htmlspecialchars, only that chars are not replaced if $plain !== true. This makes print_code() cleaner. |
||
340 | * @param string $string |
||
341 | * @param bool $plain |
||
342 | * @return string |
||
343 | * @see csstidy_print::_print() |
||
344 | * @access private |
||
345 | * @version 1.0 |
||
346 | */ |
||
347 | function _htmlsp($string, $plain) { |
||
348 | if (!$plain) { |
||
349 | return htmlspecialchars($string, ENT_QUOTES, 'utf-8'); |
||
350 | } |
||
351 | return $string; |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Get compression ratio |
||
356 | * @access public |
||
357 | * @return float |
||
358 | * @version 1.2 |
||
359 | */ |
||
360 | function get_ratio() { |
||
361 | if (!$this->output_css_plain) { |
||
362 | $this->formatted(); |
||
363 | } |
||
364 | return round((strlen($this->input_css) - strlen($this->output_css_plain)) / strlen($this->input_css), 3) * 100; |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Get difference between the old and new code in bytes and prints the code if necessary. |
||
369 | * @access public |
||
370 | * @return string |
||
371 | * @version 1.1 |
||
372 | */ |
||
373 | function get_diff() { |
||
388 | |||
389 | /** |
||
390 | * Get the size of either input or output CSS in KB |
||
391 | * @param string $loc default is "output" |
||
392 | * @access public |
||
393 | * @return integer |
||
394 | * @version 1.0 |
||
395 | */ |
||
396 | function size($loc = 'output') { |
||
407 | |||
408 | } |
||
409 |