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 |
||
| 72 | class csstidy { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Saves the parsed CSS. This array is empty if preserve_css is on. |
||
| 76 | * @var array |
||
| 77 | * @access public |
||
| 78 | */ |
||
| 79 | public $css = array(); |
||
| 80 | /** |
||
| 81 | * Saves the parsed CSS (raw) |
||
| 82 | * @var array |
||
| 83 | * @access private |
||
| 84 | */ |
||
| 85 | public $tokens = array(); |
||
| 86 | /** |
||
| 87 | * Printer class |
||
| 88 | * @see csstidy_print |
||
| 89 | * @var object |
||
| 90 | * @access public |
||
| 91 | */ |
||
| 92 | public $print; |
||
| 93 | /** |
||
| 94 | * Optimiser class |
||
| 95 | * @see csstidy_optimise |
||
| 96 | * @var object |
||
| 97 | * @access private |
||
| 98 | */ |
||
| 99 | public $optimise; |
||
| 100 | /** |
||
| 101 | * Saves the CSS charset (@charset) |
||
| 102 | * @var string |
||
| 103 | * @access private |
||
| 104 | */ |
||
| 105 | public $charset = ''; |
||
| 106 | /** |
||
| 107 | * Saves all @import URLs |
||
| 108 | * @var array |
||
| 109 | * @access private |
||
| 110 | */ |
||
| 111 | public $import = array(); |
||
| 112 | /** |
||
| 113 | * Saves the namespace |
||
| 114 | * @var string |
||
| 115 | * @access private |
||
| 116 | */ |
||
| 117 | public $namespace = ''; |
||
| 118 | /** |
||
| 119 | * Contains the version of csstidy |
||
| 120 | * @var string |
||
| 121 | * @access private |
||
| 122 | */ |
||
| 123 | public $version = '1.3'; |
||
| 124 | /** |
||
| 125 | * Stores the settings |
||
| 126 | * @var array |
||
| 127 | * @access private |
||
| 128 | */ |
||
| 129 | public $settings = array(); |
||
| 130 | /** |
||
| 131 | * Saves the parser-status. |
||
| 132 | * |
||
| 133 | * Possible values: |
||
| 134 | * - is = in selector |
||
| 135 | * - ip = in property |
||
| 136 | * - iv = in value |
||
| 137 | * - instr = in string (started at " or ' or ( ) |
||
| 138 | * - ic = in comment (ignore everything) |
||
| 139 | * - at = in @-block |
||
| 140 | * |
||
| 141 | * @var string |
||
| 142 | * @access private |
||
| 143 | */ |
||
| 144 | public $status = 'is'; |
||
| 145 | /** |
||
| 146 | * Saves the current at rule (@media) |
||
| 147 | * @var string |
||
| 148 | * @access private |
||
| 149 | */ |
||
| 150 | public $at = ''; |
||
| 151 | /** |
||
| 152 | * Saves the current selector |
||
| 153 | * @var string |
||
| 154 | * @access private |
||
| 155 | */ |
||
| 156 | public $selector = ''; |
||
| 157 | /** |
||
| 158 | * Saves the current property |
||
| 159 | * @var string |
||
| 160 | * @access private |
||
| 161 | */ |
||
| 162 | public $property = ''; |
||
| 163 | /** |
||
| 164 | * Saves the position of , in selectors |
||
| 165 | * @var array |
||
| 166 | * @access private |
||
| 167 | */ |
||
| 168 | public $sel_separate = array(); |
||
| 169 | /** |
||
| 170 | * Saves the current value |
||
| 171 | * @var string |
||
| 172 | * @access private |
||
| 173 | */ |
||
| 174 | public $value = ''; |
||
| 175 | /** |
||
| 176 | * Saves the current sub-value |
||
| 177 | * |
||
| 178 | * Example for a subvalue: |
||
| 179 | * background:url(foo.png) red no-repeat; |
||
| 180 | * "url(foo.png)", "red", and "no-repeat" are subvalues, |
||
| 181 | * separated by whitespace |
||
| 182 | * @var string |
||
| 183 | * @access private |
||
| 184 | */ |
||
| 185 | public $sub_value = ''; |
||
| 186 | /** |
||
| 187 | * Array which saves all subvalues for a property. |
||
| 188 | * @var array |
||
| 189 | * @see sub_value |
||
| 190 | * @access private |
||
| 191 | */ |
||
| 192 | public $sub_value_arr = array(); |
||
| 193 | /** |
||
| 194 | * Saves the stack of characters that opened the current strings |
||
| 195 | * @var array |
||
| 196 | * @access private |
||
| 197 | */ |
||
| 198 | public $str_char = array(); |
||
| 199 | public $cur_string = array(); |
||
| 200 | /** |
||
| 201 | * Status from which the parser switched to ic or instr |
||
| 202 | * @var array |
||
| 203 | * @access private |
||
| 204 | */ |
||
| 205 | public $from = array(); |
||
| 206 | /** |
||
| 207 | /** |
||
| 208 | * =true if in invalid at-rule |
||
| 209 | * @var bool |
||
| 210 | * @access private |
||
| 211 | */ |
||
| 212 | public $invalid_at = false; |
||
| 213 | /** |
||
| 214 | * =true if something has been added to the current selector |
||
| 215 | * @var bool |
||
| 216 | * @access private |
||
| 217 | */ |
||
| 218 | public $added = false; |
||
| 219 | /** |
||
| 220 | * Array which saves the message log |
||
| 221 | * @var array |
||
| 222 | * @access private |
||
| 223 | */ |
||
| 224 | public $log = array(); |
||
| 225 | /** |
||
| 226 | * Saves the line number |
||
| 227 | * @var integer |
||
| 228 | * @access private |
||
| 229 | */ |
||
| 230 | public $line = 1; |
||
| 231 | /** |
||
| 232 | * Marks if we need to leave quotes for a string |
||
| 233 | * @var array |
||
| 234 | * @access private |
||
| 235 | */ |
||
| 236 | public $quoted_string = array(); |
||
| 237 | |||
| 238 | /** |
||
| 239 | * List of tokens |
||
| 240 | * @var string |
||
| 241 | */ |
||
| 242 | public $tokens_list = ""; |
||
| 243 | |||
| 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 | /* Tells csstidy_optimise to keep leading zeros on decimal numbers, e.g., 0.7 */ |
||
| 284 | $this->settings['preserve_leading_zeros'] = false; |
||
| 285 | $this->optimise = new csstidy_optimise($this); |
||
|
|
|||
| 286 | |||
| 287 | $this->tokens_list = & $GLOBALS['csstidy']['tokens']; |
||
| 288 | } |
||
| 289 | |||
| 290 | function csstidy() { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get the value of a setting. |
||
| 296 | * @param string $setting |
||
| 297 | * @access public |
||
| 298 | * @return mixed |
||
| 299 | * @version 1.0 |
||
| 300 | */ |
||
| 301 | function get_cfg($setting) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Load a template |
||
| 310 | * @param string $template used by set_cfg to load a template via a configuration setting |
||
| 311 | * @access private |
||
| 312 | * @version 1.4 |
||
| 313 | */ |
||
| 314 | function _load_template($template) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Set the value of a setting. |
||
| 340 | * @param string $setting |
||
| 341 | * @param mixed $value |
||
| 342 | * @access public |
||
| 343 | * @return bool |
||
| 344 | * @version 1.0 |
||
| 345 | */ |
||
| 346 | function set_cfg($setting, $value=null) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Adds a token to $this->tokens |
||
| 367 | * @param mixed $type |
||
| 368 | * @param string $data |
||
| 369 | * @param bool $do add a token even if preserve_css is off |
||
| 370 | * @access private |
||
| 371 | * @version 1.0 |
||
| 372 | */ |
||
| 373 | function _add_token($type, $data, $do = false) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Add a message to the message log |
||
| 381 | * @param string $message |
||
| 382 | * @param string $type |
||
| 383 | * @param integer $line |
||
| 384 | * @access private |
||
| 385 | * @version 1.0 |
||
| 386 | */ |
||
| 387 | function log($message, $type, $line = -1) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Parse unicode notations and find a replacement character |
||
| 400 | * @param string $string |
||
| 401 | * @param integer $i |
||
| 402 | * @access private |
||
| 403 | * @return string |
||
| 404 | * @version 1.2 |
||
| 405 | */ |
||
| 406 | function _unicode(&$string, &$i) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Write formatted output to a file |
||
| 445 | * @param string $filename |
||
| 446 | * @param string $doctype when printing formatted, is a shorthand for the document type |
||
| 447 | * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet |
||
| 448 | * @param string $title when printing formatted, is the title to be added in the head of the document |
||
| 449 | * @param string $lang when printing formatted, gives a two-letter language code to be added to the output |
||
| 450 | * @access public |
||
| 451 | * @version 1.4 |
||
| 452 | */ |
||
| 453 | function write_page($filename, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Write plain output to a file |
||
| 459 | * @param string $filename |
||
| 460 | * @param bool $formatted whether to print formatted or not |
||
| 461 | * @param string $doctype when printing formatted, is a shorthand for the document type |
||
| 462 | * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet |
||
| 463 | * @param string $title when printing formatted, is the title to be added in the head of the document |
||
| 464 | * @param string $lang when printing formatted, gives a two-letter language code to be added to the output |
||
| 465 | * @param bool $pre_code whether to add pre and code tags around the code (for light HTML formatted templates) |
||
| 466 | * @access public |
||
| 467 | * @version 1.4 |
||
| 468 | */ |
||
| 469 | function write($filename, $formatted=false, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en', $pre_code=true) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Loads a new template |
||
| 492 | * @param string $content either filename (if $from_file == true), content of a template file, "high_compression", "highest_compression", "low_compression", or "default" |
||
| 493 | * @param bool $from_file uses $content as filename if true |
||
| 494 | * @access public |
||
| 495 | * @version 1.1 |
||
| 496 | * @see http://csstidy.sourceforge.net/templates.php |
||
| 497 | */ |
||
| 498 | function load_template($content, $from_file=true) { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Starts parsing from URL |
||
| 519 | * @param string $url |
||
| 520 | * @access public |
||
| 521 | * @version 1.0 |
||
| 522 | */ |
||
| 523 | function parse_from_url($url) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Checks if there is a token at the current position |
||
| 529 | * @param string $string |
||
| 530 | * @param integer $i |
||
| 531 | * @access public |
||
| 532 | * @version 1.11 |
||
| 533 | */ |
||
| 534 | function is_token(&$string, $i) { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Parses CSS in $string. The code is saved as array in $this->css |
||
| 540 | * @param string $string the CSS code |
||
| 541 | * @access public |
||
| 542 | * @return bool |
||
| 543 | * @version 1.1 |
||
| 544 | */ |
||
| 545 | function parse($string) { |
||
| 943 | |||
| 944 | /** |
||
| 945 | * Explodes selectors |
||
| 946 | * @access private |
||
| 947 | * @version 1.0 |
||
| 948 | */ |
||
| 949 | function explode_selectors() { |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Checks if a character is escaped (and returns true if it is) |
||
| 978 | * @param string $string |
||
| 979 | * @param integer $pos |
||
| 980 | * @access public |
||
| 981 | * @return bool |
||
| 982 | * @version 1.02 |
||
| 983 | */ |
||
| 984 | static function escaped(&$string, $pos) { |
||
| 987 | |||
| 988 | /** |
||
| 989 | * Adds a property with value to the existing CSS code |
||
| 990 | * @param string $media |
||
| 991 | * @param string $selector |
||
| 992 | * @param string $property |
||
| 993 | * @param string $new_val |
||
| 994 | * @access private |
||
| 995 | * @version 1.2 |
||
| 996 | */ |
||
| 997 | function css_add_property($media, $selector, $property, $new_val) { |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * Start a new media section. |
||
| 1014 | * Check if the media is not already known, |
||
| 1015 | * else rename it with extra spaces |
||
| 1016 | * to avoid merging |
||
| 1017 | * |
||
| 1018 | * @param string $media |
||
| 1019 | * @return string |
||
| 1020 | */ |
||
| 1021 | function css_new_media_section($media){ |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * Start a new selector. |
||
| 1046 | * If already referenced in this media section, |
||
| 1047 | * rename it with extra space to avoid merging |
||
| 1048 | * except if merging is required, |
||
| 1049 | * or last selector is the same (merge siblings) |
||
| 1050 | * |
||
| 1051 | * never merge @font-face |
||
| 1052 | * |
||
| 1053 | * @param string $media |
||
| 1054 | * @param string $selector |
||
| 1055 | * @return string |
||
| 1056 | */ |
||
| 1057 | function css_new_selector($media,$selector){ |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Start a new propertie. |
||
| 1084 | * If already references in this selector, |
||
| 1085 | * rename it with extra space to avoid override |
||
| 1086 | * |
||
| 1087 | * @param string $media |
||
| 1088 | * @param string $selector |
||
| 1089 | * @param string $property |
||
| 1090 | * @return string |
||
| 1091 | */ |
||
| 1092 | function css_new_property($media, $selector, $property){ |
||
| 1104 | |||
| 1105 | /** |
||
| 1106 | * Adds CSS to an existing media/selector |
||
| 1107 | * @param string $media |
||
| 1108 | * @param string $selector |
||
| 1109 | * @param array $css_add |
||
| 1110 | * @access private |
||
| 1111 | * @version 1.1 |
||
| 1112 | */ |
||
| 1113 | function merge_css_blocks($media, $selector, $css_add) { |
||
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Checks if $value is !important. |
||
| 1121 | * @param string $value |
||
| 1122 | * @return bool |
||
| 1123 | * @access public |
||
| 1124 | * @version 1.0 |
||
| 1125 | */ |
||
| 1126 | static function is_important(&$value) { |
||
| 1129 | |||
| 1130 | /** |
||
| 1131 | * Returns a value without !important |
||
| 1132 | * @param string $value |
||
| 1133 | * @return string |
||
| 1134 | * @access public |
||
| 1135 | * @version 1.0 |
||
| 1136 | */ |
||
| 1137 | static function gvw_important($value) { |
||
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Checks if the next word in a string from pos is a CSS property |
||
| 1151 | * @param string $istring |
||
| 1152 | * @param integer $pos |
||
| 1153 | * @return bool |
||
| 1154 | * @access private |
||
| 1155 | * @version 1.2 |
||
| 1156 | */ |
||
| 1157 | function property_is_next($istring, $pos) { |
||
| 1171 | |||
| 1172 | /** |
||
| 1173 | * Checks if a property is valid |
||
| 1174 | * @param string $property |
||
| 1175 | * @return bool; |
||
| 1176 | * @access public |
||
| 1177 | * @version 1.0 |
||
| 1178 | */ |
||
| 1179 | function property_is_valid($property) { |
||
| 1185 | |||
| 1186 | /** |
||
| 1187 | * Accepts a list of strings (e.g., the argument to format() in a @font-face src property) |
||
| 1188 | * and returns a list of the strings. Converts things like: |
||
| 1189 | * |
||
| 1190 | * format(abc) => format("abc") |
||
| 1191 | * format(abc def) => format("abc","def") |
||
| 1192 | * format(abc "def") => format("abc","def") |
||
| 1193 | * format(abc, def, ghi) => format("abc","def","ghi") |
||
| 1194 | * format("abc",'def') => format("abc","def") |
||
| 1195 | * format("abc, def, ghi") => format("abc, def, ghi") |
||
| 1196 | * |
||
| 1197 | * @param string |
||
| 1198 | * @return array |
||
| 1199 | */ |
||
| 1200 | |||
| 1201 | function parse_string_list($value) { |
||
| 1248 | } |
||
| 1249 |
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: