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 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, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 73 | class csstidy { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Saves the parsed CSS. This array is empty if preserve_css is on. |
||
| 77 | * @var array |
||
| 78 | * @access public |
||
| 79 | */ |
||
| 80 | public $css = array(); |
||
| 81 | /** |
||
| 82 | * Saves the parsed CSS (raw) |
||
| 83 | * @var array |
||
| 84 | * @access private |
||
| 85 | */ |
||
| 86 | public $tokens = array(); |
||
| 87 | /** |
||
| 88 | * Printer class |
||
| 89 | * @see csstidy_print |
||
| 90 | * @var object |
||
| 91 | * @access public |
||
| 92 | */ |
||
| 93 | public $print; |
||
| 94 | /** |
||
| 95 | * Optimiser class |
||
| 96 | * @see csstidy_optimise |
||
| 97 | * @var object |
||
| 98 | * @access private |
||
| 99 | */ |
||
| 100 | public $optimise; |
||
| 101 | /** |
||
| 102 | * Saves the CSS charset (@charset) |
||
| 103 | * @var string |
||
| 104 | * @access private |
||
| 105 | */ |
||
| 106 | public $charset = ''; |
||
| 107 | /** |
||
| 108 | * Saves all @import URLs |
||
| 109 | * @var array |
||
| 110 | * @access private |
||
| 111 | */ |
||
| 112 | public $import = array(); |
||
| 113 | /** |
||
| 114 | * Saves the namespace |
||
| 115 | * @var string |
||
| 116 | * @access private |
||
| 117 | */ |
||
| 118 | public $namespace = ''; |
||
| 119 | /** |
||
| 120 | * Contains the version of csstidy |
||
| 121 | * @var string |
||
| 122 | * @access private |
||
| 123 | */ |
||
| 124 | public $version = '1.3'; |
||
| 125 | /** |
||
| 126 | * Stores the settings |
||
| 127 | * @var array |
||
| 128 | * @access private |
||
| 129 | */ |
||
| 130 | public $settings = array(); |
||
| 131 | /** |
||
| 132 | * Saves the parser-status. |
||
| 133 | * |
||
| 134 | * Possible values: |
||
| 135 | * - is = in selector |
||
| 136 | * - ip = in property |
||
| 137 | * - iv = in value |
||
| 138 | * - instr = in string (started at " or ' or ( ) |
||
| 139 | * - ic = in comment (ignore everything) |
||
| 140 | * - at = in @-block |
||
| 141 | * |
||
| 142 | * @var string |
||
| 143 | * @access private |
||
| 144 | */ |
||
| 145 | public $status = 'is'; |
||
| 146 | /** |
||
| 147 | * Saves the current at rule (@media) |
||
| 148 | * @var string |
||
| 149 | * @access private |
||
| 150 | */ |
||
| 151 | public $at = ''; |
||
| 152 | /** |
||
| 153 | * Saves the current selector |
||
| 154 | * @var string |
||
| 155 | * @access private |
||
| 156 | */ |
||
| 157 | public $selector = ''; |
||
| 158 | /** |
||
| 159 | * Saves the current property |
||
| 160 | * @var string |
||
| 161 | * @access private |
||
| 162 | */ |
||
| 163 | public $property = ''; |
||
| 164 | /** |
||
| 165 | * Saves the position of , in selectors |
||
| 166 | * @var array |
||
| 167 | * @access private |
||
| 168 | */ |
||
| 169 | public $sel_separate = array(); |
||
| 170 | /** |
||
| 171 | * Saves the current value |
||
| 172 | * @var string |
||
| 173 | * @access private |
||
| 174 | */ |
||
| 175 | public $value = ''; |
||
| 176 | /** |
||
| 177 | * Saves the current sub-value |
||
| 178 | * |
||
| 179 | * Example for a subvalue: |
||
| 180 | * background:url(foo.png) red no-repeat; |
||
| 181 | * "url(foo.png)", "red", and "no-repeat" are subvalues, |
||
| 182 | * separated by whitespace |
||
| 183 | * @var string |
||
| 184 | * @access private |
||
| 185 | */ |
||
| 186 | public $sub_value = ''; |
||
| 187 | /** |
||
| 188 | * Array which saves all subvalues for a property. |
||
| 189 | * @var array |
||
| 190 | * @see sub_value |
||
| 191 | * @access private |
||
| 192 | */ |
||
| 193 | public $sub_value_arr = array(); |
||
| 194 | /** |
||
| 195 | * Saves the stack of characters that opened the current strings |
||
| 196 | * @var array |
||
| 197 | * @access private |
||
| 198 | */ |
||
| 199 | public $str_char = array(); |
||
| 200 | public $cur_string = array(); |
||
| 201 | /** |
||
| 202 | * Status from which the parser switched to ic or instr |
||
| 203 | * @var array |
||
| 204 | * @access private |
||
| 205 | */ |
||
| 206 | public $from = array(); |
||
| 207 | /** |
||
| 208 | /** |
||
| 209 | * =true if in invalid at-rule |
||
| 210 | * @var bool |
||
| 211 | * @access private |
||
| 212 | */ |
||
| 213 | public $invalid_at = false; |
||
| 214 | /** |
||
| 215 | * =true if something has been added to the current selector |
||
| 216 | * @var bool |
||
| 217 | * @access private |
||
| 218 | */ |
||
| 219 | public $added = false; |
||
| 220 | /** |
||
| 221 | * Array which saves the message log |
||
| 222 | * @var array |
||
| 223 | * @access private |
||
| 224 | */ |
||
| 225 | public $log = array(); |
||
| 226 | /** |
||
| 227 | * Saves the line number |
||
| 228 | * @var integer |
||
| 229 | * @access private |
||
| 230 | */ |
||
| 231 | public $line = 1; |
||
| 232 | /** |
||
| 233 | * Marks if we need to leave quotes for a string |
||
| 234 | * @var array |
||
| 235 | * @access private |
||
| 236 | */ |
||
| 237 | public $quoted_string = array(); |
||
| 238 | |||
| 239 | /** |
||
| 240 | * List of tokens |
||
| 241 | * @var string |
||
| 242 | */ |
||
| 243 | public $tokens_list = ""; |
||
| 244 | /** |
||
| 245 | * Loads standard template and sets default settings |
||
| 246 | * @access private |
||
| 247 | * @version 1.3 |
||
| 248 | */ |
||
| 249 | function __construct() { |
||
| 250 | $this->settings['remove_bslash'] = true; |
||
| 251 | $this->settings['compress_colors'] = true; |
||
| 252 | $this->settings['compress_font-weight'] = true; |
||
| 253 | $this->settings['lowercase_s'] = false; |
||
| 254 | /* |
||
| 255 | 1 common shorthands optimization |
||
| 256 | 2 + font property optimization |
||
| 257 | 3 + background property optimization |
||
| 258 | */ |
||
| 259 | $this->settings['optimise_shorthands'] = 1; |
||
| 260 | $this->settings['remove_last_;'] = true; |
||
| 261 | /* rewrite all properties with low case, better for later gzip OK, safe*/ |
||
| 262 | $this->settings['case_properties'] = 1; |
||
| 263 | /* sort properties in alpabetic order, better for later gzip |
||
| 264 | * but can cause trouble in case of overiding same propertie or using hack |
||
| 265 | */ |
||
| 266 | $this->settings['sort_properties'] = false; |
||
| 267 | /* |
||
| 268 | 1, 3, 5, etc -- enable sorting selectors inside @media: a{}b{}c{} |
||
| 269 | 2, 5, 8, etc -- enable sorting selectors inside one CSS declaration: a,b,c{} |
||
| 270 | preserve order by default cause it can break functionnality |
||
| 271 | */ |
||
| 272 | $this->settings['sort_selectors'] = 0; |
||
| 273 | /* is dangeroues to be used: CSS is broken sometimes */ |
||
| 274 | $this->settings['merge_selectors'] = 0; |
||
| 275 | /* preserve or not browser hacks */ |
||
| 276 | $this->settings['discard_invalid_selectors'] = false; |
||
| 277 | $this->settings['discard_invalid_properties'] = false; |
||
| 278 | $this->settings['css_level'] = 'CSS2.1'; |
||
| 279 | $this->settings['preserve_css'] = false; |
||
| 280 | $this->settings['timestamp'] = false; |
||
| 281 | $this->settings['template'] = ''; // say that propertie exist |
||
| 282 | $this->set_cfg('template','default'); // call load_template |
||
| 283 | $this->optimise = new csstidy_optimise($this); |
||
|
|
|||
| 284 | |||
| 285 | $this->tokens_list = & $GLOBALS['csstidy']['tokens']; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get the value of a setting. |
||
| 290 | * @param string $setting |
||
| 291 | * @access public |
||
| 292 | * @return mixed |
||
| 293 | * @version 1.0 |
||
| 294 | */ |
||
| 295 | function get_cfg($setting) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Load a template |
||
| 304 | * @param string $template used by set_cfg to load a template via a configuration setting |
||
| 305 | * @access private |
||
| 306 | * @version 1.4 |
||
| 307 | */ |
||
| 308 | function _load_template($template) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Set the value of a setting. |
||
| 334 | * @param string $setting |
||
| 335 | * @param mixed $value |
||
| 336 | * @access public |
||
| 337 | * @return bool |
||
| 338 | * @version 1.0 |
||
| 339 | */ |
||
| 340 | function set_cfg($setting, $value=null) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Adds a token to $this->tokens |
||
| 361 | * @param mixed $type |
||
| 362 | * @param string $data |
||
| 363 | * @param bool $do add a token even if preserve_css is off |
||
| 364 | * @access private |
||
| 365 | * @version 1.0 |
||
| 366 | */ |
||
| 367 | function _add_token($type, $data, $do = false) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Add a message to the message log |
||
| 375 | * @param string $message |
||
| 376 | * @param string $type |
||
| 377 | * @param integer $line |
||
| 378 | * @access private |
||
| 379 | * @version 1.0 |
||
| 380 | */ |
||
| 381 | function log($message, $type, $line = -1) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Parse unicode notations and find a replacement character |
||
| 394 | * @param string $string |
||
| 395 | * @param integer $i |
||
| 396 | * @access private |
||
| 397 | * @return string |
||
| 398 | * @version 1.2 |
||
| 399 | */ |
||
| 400 | function _unicode(&$string, &$i) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Write formatted output to a file |
||
| 439 | * @param string $filename |
||
| 440 | * @param string $doctype when printing formatted, is a shorthand for the document type |
||
| 441 | * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet |
||
| 442 | * @param string $title when printing formatted, is the title to be added in the head of the document |
||
| 443 | * @param string $lang when printing formatted, gives a two-letter language code to be added to the output |
||
| 444 | * @access public |
||
| 445 | * @version 1.4 |
||
| 446 | */ |
||
| 447 | function write_page($filename, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Write plain output to a file |
||
| 453 | * @param string $filename |
||
| 454 | * @param bool $formatted whether to print formatted or not |
||
| 455 | * @param string $doctype when printing formatted, is a shorthand for the document type |
||
| 456 | * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet |
||
| 457 | * @param string $title when printing formatted, is the title to be added in the head of the document |
||
| 458 | * @param string $lang when printing formatted, gives a two-letter language code to be added to the output |
||
| 459 | * @param bool $pre_code whether to add pre and code tags around the code (for light HTML formatted templates) |
||
| 460 | * @access public |
||
| 461 | * @version 1.4 |
||
| 462 | */ |
||
| 463 | function write($filename, $formatted=false, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en', $pre_code=true) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Loads a new template |
||
| 486 | * @param string $content either filename (if $from_file == true), content of a template file, "high_compression", "highest_compression", "low_compression", or "default" |
||
| 487 | * @param bool $from_file uses $content as filename if true |
||
| 488 | * @access public |
||
| 489 | * @version 1.1 |
||
| 490 | * @see http://csstidy.sourceforge.net/templates.php |
||
| 491 | */ |
||
| 492 | function load_template($content, $from_file=true) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Starts parsing from URL |
||
| 513 | * @param string $url |
||
| 514 | * @access public |
||
| 515 | * @version 1.0 |
||
| 516 | */ |
||
| 517 | function parse_from_url($url) { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Checks if there is a token at the current position |
||
| 523 | * @param string $string |
||
| 524 | * @param integer $i |
||
| 525 | * @access public |
||
| 526 | * @version 1.11 |
||
| 527 | */ |
||
| 528 | function is_token(&$string, $i) { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Parses CSS in $string. The code is saved as array in $this->css |
||
| 534 | * @param string $string the CSS code |
||
| 535 | * @access public |
||
| 536 | * @return bool |
||
| 537 | * @version 1.1 |
||
| 538 | */ |
||
| 539 | function parse($string) { |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Explodes selectors |
||
| 940 | * @access private |
||
| 941 | * @version 1.0 |
||
| 942 | */ |
||
| 943 | function explode_selectors() { |
||
| 969 | |||
| 970 | /** |
||
| 971 | * Checks if a character is escaped (and returns true if it is) |
||
| 972 | * @param string $string |
||
| 973 | * @param integer $pos |
||
| 974 | * @access public |
||
| 975 | * @return bool |
||
| 976 | * @version 1.02 |
||
| 977 | */ |
||
| 978 | static function escaped(&$string, $pos) { |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Adds a property with value to the existing CSS code |
||
| 984 | * @param string $media |
||
| 985 | * @param string $selector |
||
| 986 | * @param string $property |
||
| 987 | * @param string $new_val |
||
| 988 | * @access private |
||
| 989 | * @version 1.2 |
||
| 990 | */ |
||
| 991 | function css_add_property($media, $selector, $property, $new_val) { |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Start a new media section. |
||
| 1008 | * Check if the media is not already known, |
||
| 1009 | * else rename it with extra spaces |
||
| 1010 | * to avoid merging |
||
| 1011 | * |
||
| 1012 | * @param string $media |
||
| 1013 | * @return string |
||
| 1014 | */ |
||
| 1015 | function css_new_media_section($media){ |
||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * Start a new selector. |
||
| 1040 | * If already referenced in this media section, |
||
| 1041 | * rename it with extra space to avoid merging |
||
| 1042 | * except if merging is required, |
||
| 1043 | * or last selector is the same (merge siblings) |
||
| 1044 | * |
||
| 1045 | * never merge @font-face |
||
| 1046 | * |
||
| 1047 | * @param string $media |
||
| 1048 | * @param string $selector |
||
| 1049 | * @return string |
||
| 1050 | */ |
||
| 1051 | function css_new_selector($media,$selector){ |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Start a new propertie. |
||
| 1078 | * If already references in this selector, |
||
| 1079 | * rename it with extra space to avoid override |
||
| 1080 | * |
||
| 1081 | * @param string $media |
||
| 1082 | * @param string $selector |
||
| 1083 | * @param string $property |
||
| 1084 | * @return string |
||
| 1085 | */ |
||
| 1086 | function css_new_property($media, $selector, $property){ |
||
| 1098 | |||
| 1099 | /** |
||
| 1100 | * Adds CSS to an existing media/selector |
||
| 1101 | * @param string $media |
||
| 1102 | * @param string $selector |
||
| 1103 | * @param array $css_add |
||
| 1104 | * @access private |
||
| 1105 | * @version 1.1 |
||
| 1106 | */ |
||
| 1107 | function merge_css_blocks($media, $selector, $css_add) { |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * Checks if $value is !important. |
||
| 1115 | * @param string $value |
||
| 1116 | * @return bool |
||
| 1117 | * @access public |
||
| 1118 | * @version 1.0 |
||
| 1119 | */ |
||
| 1120 | static function is_important(&$value) { |
||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * Returns a value without !important |
||
| 1126 | * @param string $value |
||
| 1127 | * @return string |
||
| 1128 | * @access public |
||
| 1129 | * @version 1.0 |
||
| 1130 | */ |
||
| 1131 | static function gvw_important($value) { |
||
| 1142 | |||
| 1143 | /** |
||
| 1144 | * Checks if the next word in a string from pos is a CSS property |
||
| 1145 | * @param string $istring |
||
| 1146 | * @param integer $pos |
||
| 1147 | * @return bool |
||
| 1148 | * @access private |
||
| 1149 | * @version 1.2 |
||
| 1150 | */ |
||
| 1151 | function property_is_next($istring, $pos) { |
||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Checks if a property is valid |
||
| 1168 | * @param string $property |
||
| 1169 | * @return bool; |
||
| 1170 | * @access public |
||
| 1171 | * @version 1.0 |
||
| 1172 | */ |
||
| 1173 | function property_is_valid($property) { |
||
| 1179 | |||
| 1180 | /** |
||
| 1181 | * Accepts a list of strings (e.g., the argument to format() in a @font-face src property) |
||
| 1182 | * and returns a list of the strings. Converts things like: |
||
| 1183 | * |
||
| 1184 | * format(abc) => format("abc") |
||
| 1185 | * format(abc def) => format("abc","def") |
||
| 1186 | * format(abc "def") => format("abc","def") |
||
| 1187 | * format(abc, def, ghi) => format("abc","def","ghi") |
||
| 1188 | * format("abc",'def') => format("abc","def") |
||
| 1189 | * format("abc, def, ghi") => format("abc, def, ghi") |
||
| 1190 | * |
||
| 1191 | * @param string |
||
| 1192 | * @return array |
||
| 1193 | */ |
||
| 1194 | |||
| 1195 | function parse_string_list($value) { |
||
| 1242 | } |
||
| 1243 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: