Complex classes like Assets_processing 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 Assets_processing, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Assets_processing { |
||
| 19 | /** |
||
| 20 | * Do not inline files bigger than 4 KiB |
||
| 21 | */ |
||
| 22 | const MAX_EMBEDDING_SIZE = 4096; |
||
| 23 | protected static $extension_to_mime = [ |
||
| 24 | 'jpeg' => 'image/jpg', |
||
| 25 | 'jpe' => 'image/jpg', |
||
| 26 | 'jpg' => 'image/jpg', |
||
| 27 | 'gif' => 'image/gif', |
||
| 28 | 'png' => 'image/png', |
||
| 29 | 'svg' => 'image/svg+xml', |
||
| 30 | 'svgz' => 'image/svg+xml', |
||
| 31 | 'woff' => 'application/font-woff', |
||
| 32 | //'woff2' => 'application/font-woff2' |
||
| 33 | ]; |
||
| 34 | /** |
||
| 35 | * Analyses file for images, fonts and css links and include they content into single resulting css file. |
||
| 36 | * |
||
| 37 | * Supports next file extensions for possible assets: |
||
| 38 | * jpeg, jpe, jpg, gif, png, ttf, ttc, svg, svgz, woff, css |
||
| 39 | * |
||
| 40 | * @param string $data Content of processed file |
||
| 41 | * @param string $file Path to file, that contains specified in previous parameter content |
||
| 42 | * @param string[] $not_embedded_resources Some resources like images and fonts might not be embedded into resulting CSS because of their size |
||
| 43 | * |
||
| 44 | * @return string $data |
||
| 45 | */ |
||
| 46 | 10 | public static function css ($data, $file, &$not_embedded_resources = []) { |
|
| 131 | /** |
||
| 132 | * Simple and fast JS minification |
||
| 133 | * |
||
| 134 | * @param string $data |
||
| 135 | * |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | 10 | public static function js ($data) { |
|
| 221 | /** |
||
| 222 | * @param string $current_line |
||
| 223 | * @param string $next_line |
||
| 224 | * |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | 10 | protected static function new_line_needed ($current_line, $next_line) { |
|
| 239 | /** |
||
| 240 | * Analyses file for scripts and styles, combines them into resulting files in order to optimize loading process |
||
| 241 | * (files with combined scripts and styles will be created) |
||
| 242 | * |
||
| 243 | * @param string $data Content of processed file |
||
| 244 | * @param string $file Path to file, that contains specified in previous parameter content |
||
| 245 | * @param string $target_directory_path Target directory for resulting combined files |
||
| 246 | * @param bool $vulcanization Whether to put combined files separately or to make included assets built-in (vulcanization) |
||
| 247 | * @param string[] $not_embedded_resources Resources like images/fonts might not be embedded into resulting CSS because of big size or CSS/JS because of CSP |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | 8 | public static function html ($data, $file, $target_directory_path, $vulcanization, &$not_embedded_resources = []) { |
|
| 264 | /** |
||
| 265 | * @param string $data Content of processed file |
||
| 266 | * @param string $file Path to file, that contains specified in previous parameter content |
||
| 267 | * @param string $target_directory_path Target directory for resulting combined files |
||
| 268 | * @param bool $vulcanization Whether to put combined files separately or to make included assets built-in (vulcanization) |
||
| 269 | * @param string[] $not_embedded_resources Resources like images/fonts might not be embedded into resulting CSS because of big size or CSS/JS because of CSP |
||
| 270 | */ |
||
| 271 | 8 | protected static function html_process_scripts (&$data, $file, $target_directory_path, $vulcanization, &$not_embedded_resources) { |
|
| 272 | 8 | if (!preg_match_all('/<script(.*)<\/script>/Uims', $data, $scripts)) { |
|
| 273 | 8 | return; |
|
| 274 | } |
||
| 275 | 8 | $scripts_content = ''; |
|
| 276 | 8 | $scripts_to_replace = []; |
|
| 277 | 8 | $dir = dirname($file); |
|
| 278 | 8 | foreach ($scripts[1] as $index => $script) { |
|
| 279 | 8 | $script = explode('>', $script, 2); |
|
| 280 | 8 | if (preg_match('/src\s*=\s*[\'"](.*)[\'"]/Uims', $script[0], $url)) { |
|
| 281 | 8 | $url = $url[1]; |
|
| 282 | 8 | if (!static::is_relative_path_and_exists($url, $dir)) { |
|
| 283 | 4 | continue; |
|
| 284 | } |
||
| 285 | 8 | $scripts_to_replace[] = $scripts[0][$index]; |
|
| 286 | 8 | $scripts_content .= file_get_contents("$dir/$url").";\n"; |
|
| 287 | } else { |
||
| 288 | 8 | $scripts_to_replace[] = $scripts[0][$index]; |
|
| 289 | 8 | $scripts_content .= "$script[1];\n"; |
|
| 290 | } |
||
| 291 | } |
||
| 292 | 8 | $scripts_content = static::js($scripts_content); |
|
| 293 | 8 | if (!$scripts_to_replace) { |
|
| 294 | 4 | return; |
|
| 295 | } |
||
| 296 | // Remove all scripts |
||
| 297 | 8 | $data = str_replace($scripts_to_replace, '', $data); |
|
| 298 | /** |
||
| 299 | * If vulcanization is not used - put contents into separate file, and put link to it, otherwise put minified content back |
||
| 300 | */ |
||
| 301 | 8 | if (!$vulcanization) { |
|
| 302 | 2 | $hash = md5($scripts_content); |
|
| 303 | // TODO: Remove in 7.x; For backward compatibility, since some modules might use this b specifying file path |
||
| 304 | 2 | if (!is_dir($target_directory_path)) { |
|
| 305 | $target_directory_path = dirname($target_directory_path); |
||
| 306 | } |
||
| 307 | 2 | file_put_contents("$target_directory_path/$hash.js", $scripts_content, LOCK_EX | FILE_BINARY); |
|
| 308 | // Add script with combined content file to the end |
||
| 309 | 2 | $data .= "<script src=\"./$hash.js\"></script>"; |
|
| 310 | 2 | $not_embedded_resources[] = str_replace(getcwd(), '', realpath("$target_directory_path/$hash.js")); |
|
| 311 | } else { |
||
| 312 | // Add combined content inline script to the end |
||
| 313 | 6 | $data .= "<script>$scripts_content</script>"; |
|
| 314 | } |
||
| 315 | 8 | } |
|
| 316 | /** |
||
| 317 | * @param string $data Content of processed file |
||
| 318 | * @param string $file Path to file, that contains specified in previous parameter content |
||
| 319 | * @param string $target_directory_path Target directory for resulting combined files |
||
| 320 | * @param bool $vulcanization Whether to put combined files separately or to make included assets built-in (vulcanization) |
||
| 321 | * @param string[] $not_embedded_resources Resources like images/fonts might not be embedded into resulting CSS because of big size or CSS/JS because of CSP |
||
| 322 | */ |
||
| 323 | 8 | protected static function html_process_links_and_styles (&$data, $file, $target_directory_path, $vulcanization, &$not_embedded_resources) { |
|
| 324 | // Drop Polymer inclusion, since it is already present |
||
| 325 | 8 | $data = str_replace('<link rel="import" href="../polymer/polymer.html">', '', $data); |
|
| 326 | 8 | if (!preg_match_all('/<link(.*)>|<style(.*)<\/style>/Uims', $data, $links_and_styles)) { |
|
| 327 | 8 | return; |
|
| 328 | } |
||
| 329 | 8 | $dir = dirname($file); |
|
| 330 | 8 | foreach ($links_and_styles[1] as $index => $link) { |
|
| 331 | /** |
||
| 332 | * Check for custom styles `is="custom-style"` or styles includes `include=".."` - we'll skip them |
||
| 333 | * Or if content is plain CSS |
||
| 334 | */ |
||
| 335 | if ( |
||
| 336 | 8 | preg_match('/^[^>]*(is="custom-style"|include=)[^>]*>/Uim', $links_and_styles[2][$index]) || |
|
| 337 | 8 | mb_strpos($links_and_styles[0][$index], '</style>') > 0 |
|
| 338 | ) { |
||
| 339 | 8 | $content = explode('>', $links_and_styles[2][$index], 2)[1]; |
|
| 340 | 8 | $data = str_replace( |
|
| 341 | $content, |
||
| 342 | 8 | static::css($content, $file, $not_embedded_resources), |
|
| 343 | $data |
||
| 344 | ); |
||
| 345 | 8 | continue; |
|
| 346 | } |
||
| 347 | 8 | if (!static::has_relative_href($link, $url, $dir)) { |
|
| 348 | 4 | continue; |
|
| 349 | } |
||
| 350 | 8 | $import = preg_match('/rel\s*=\s*[\'"]import[\'"]/Uim', $link); |
|
| 351 | /** |
||
| 352 | * CSS imports are available in Polymer alongside with HTML imports |
||
| 353 | */ |
||
| 354 | 8 | $css_import = $import && preg_match('/type\s*=\s*[\'"]css[\'"]/Uim', $link); |
|
| 355 | 8 | $stylesheet = preg_match('/rel\s*=\s*[\'"]stylesheet[\'"]/Uim', $link); |
|
| 356 | // TODO: Polymer only supports `style[is=custom-style]`, but no `link`-based counterpart, so we can't provide CSP-compatibility for CSS anyway |
||
| 357 | 8 | if ($css_import || $stylesheet) { |
|
| 358 | /** |
||
| 359 | * If content is link to CSS file |
||
| 360 | */ |
||
| 361 | 8 | $css = static::css( |
|
| 362 | 8 | file_get_contents("$dir/$url"), |
|
| 363 | 8 | "$dir/$url", |
|
| 364 | $not_embedded_resources |
||
| 365 | ); |
||
| 366 | 8 | $data = preg_replace( |
|
| 367 | 8 | '/'.$links_and_styles[0][$index].'.*<template>/Uims', |
|
| 368 | 8 | "<template><style>$css</style>", |
|
| 369 | $data |
||
| 370 | ); |
||
| 371 | 4 | } elseif ($import) { |
|
| 372 | /** |
||
| 373 | * If content is HTML import |
||
| 374 | */ |
||
| 375 | 4 | $data = str_replace( |
|
| 376 | 4 | $links_and_styles[0][$index], |
|
| 377 | 4 | static::html( |
|
| 378 | 4 | file_get_contents("$dir/$url"), |
|
| 379 | 8 | "$dir/$url", |
|
| 380 | $target_directory_path, |
||
| 381 | $vulcanization, |
||
| 382 | $not_embedded_resources |
||
| 383 | ), |
||
| 384 | $data |
||
| 385 | ); |
||
| 386 | } |
||
| 387 | } |
||
| 388 | 8 | } |
|
| 389 | /** |
||
| 390 | * @param string $link |
||
| 391 | * @param string $url |
||
| 392 | * @param string $dir |
||
| 393 | * |
||
| 394 | * @return bool |
||
| 395 | */ |
||
| 396 | 8 | protected static function has_relative_href ($link, &$url, $dir) { |
|
| 406 | /** |
||
| 407 | * Simple check for http[s], ftp and absolute links |
||
| 408 | * |
||
| 409 | * @param string $path |
||
| 410 | * @param string $dir |
||
| 411 | * |
||
| 412 | * @return bool |
||
| 413 | */ |
||
| 414 | 10 | protected static function is_relative_path_and_exists ($path, $dir) { |
|
| 417 | } |
||
| 418 |