Complex classes like Minify 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 Minify, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Minify { |
||
| 26 | |||
| 27 | const VERSION = '2.2.0'; |
||
| 28 | const TYPE_CSS = 'text/css'; |
||
| 29 | const TYPE_HTML = 'text/html'; |
||
| 30 | // there is some debate over the ideal JS Content-Type, but this is the |
||
| 31 | // Apache default and what Yahoo! uses.. |
||
| 32 | const TYPE_JS = 'application/x-javascript'; |
||
| 33 | const URL_DEBUG = 'http://code.google.com/p/minify/wiki/Debugging'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * How many hours behind are the file modification times of uploaded files? |
||
| 37 | * |
||
| 38 | * If you upload files from Windows to a non-Windows server, Windows may report |
||
| 39 | * incorrect mtimes for the files. Immediately after modifying and uploading a |
||
| 40 | * file, use the touch command to update the mtime on the server. If the mtime |
||
| 41 | * jumps ahead by a number of hours, set this variable to that number. If the mtime |
||
| 42 | * moves back, this should not be needed. |
||
| 43 | * |
||
| 44 | * @var int $uploaderHoursBehind |
||
| 45 | */ |
||
| 46 | public static $uploaderHoursBehind = 0; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * If this string is not empty AND the serve() option 'bubbleCssImports' is |
||
| 50 | * NOT set, then serve() will check CSS files for @import declarations that |
||
| 51 | * appear too late in the combined stylesheet. If found, serve() will prepend |
||
| 52 | * the output with this warning. |
||
| 53 | * |
||
| 54 | * @var string $importWarning |
||
| 55 | */ |
||
| 56 | public static $importWarning = "/* See http://code.google.com/p/minify/wiki/CommonProblems#@imports_can_appear_in_invalid_locations_in_combined_CSS_files */\n"; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Has the DOCUMENT_ROOT been set in user code? |
||
| 60 | * |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | public static $isDocRootSet = false; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Specify a cache object (with identical interface as Minify_Cache_File) or |
||
| 67 | * a path to use with Minify_Cache_File. |
||
| 68 | * |
||
| 69 | * If not called, Minify will not use a cache and, for each 200 response, will |
||
| 70 | * need to recombine files, minify and encode the output. |
||
| 71 | * |
||
| 72 | * @param mixed $cache object with identical interface as Minify_Cache_File or |
||
| 73 | * a directory path, or null to disable caching. (default = '') |
||
| 74 | * |
||
| 75 | * @param bool $fileLocking (default = true) This only applies if the first |
||
| 76 | * parameter is a string. |
||
| 77 | * |
||
| 78 | * @return null |
||
| 79 | */ |
||
| 80 | public static function setCache($cache = '', $fileLocking = true) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Serve a request for a minified file. |
||
| 91 | * |
||
| 92 | * Here are the available options and defaults in the base controller: |
||
| 93 | * |
||
| 94 | * 'isPublic' : send "public" instead of "private" in Cache-Control |
||
| 95 | * headers, allowing shared caches to cache the output. (default true) |
||
| 96 | * |
||
| 97 | * 'quiet' : set to true to have serve() return an array rather than sending |
||
| 98 | * any headers/output (default false) |
||
| 99 | * |
||
| 100 | * 'encodeOutput' : set to false to disable content encoding, and not send |
||
| 101 | * the Vary header (default true) |
||
| 102 | * |
||
| 103 | * 'encodeMethod' : generally you should let this be determined by |
||
| 104 | * HTTP_Encoder (leave null), but you can force a particular encoding |
||
| 105 | * to be returned, by setting this to 'gzip' or '' (no encoding) |
||
| 106 | * |
||
| 107 | * 'encodeLevel' : level of encoding compression (0 to 9, default 9) |
||
| 108 | * |
||
| 109 | * 'contentTypeCharset' : appended to the Content-Type header sent. Set to a falsey |
||
| 110 | * value to remove. (default 'utf-8') |
||
| 111 | * |
||
| 112 | * 'maxAge' : set this to the number of seconds the client should use its cache |
||
| 113 | * before revalidating with the server. This sets Cache-Control: max-age and the |
||
| 114 | * Expires header. Unlike the old 'setExpires' setting, this setting will NOT |
||
| 115 | * prevent conditional GETs. Note this has nothing to do with server-side caching. |
||
| 116 | * |
||
| 117 | * 'rewriteCssUris' : If true, serve() will automatically set the 'currentDir' |
||
| 118 | * minifier option to enable URI rewriting in CSS files (default true) |
||
| 119 | * |
||
| 120 | * 'bubbleCssImports' : If true, all @import declarations in combined CSS |
||
| 121 | * files will be move to the top. Note this may alter effective CSS values |
||
| 122 | * due to a change in order. (default false) |
||
| 123 | * |
||
| 124 | * 'debug' : set to true to minify all sources with the 'Lines' controller, which |
||
| 125 | * eases the debugging of combined files. This also prevents 304 responses. |
||
| 126 | * @see Minify_Lines::minify() |
||
| 127 | * |
||
| 128 | * 'concatOnly' : set to true to disable minification and simply concatenate the files. |
||
| 129 | * For JS, no minifier will be used. For CSS, only URI rewriting is still performed. |
||
| 130 | * |
||
| 131 | * 'minifiers' : to override Minify's default choice of minifier function for |
||
| 132 | * a particular content-type, specify your callback under the key of the |
||
| 133 | * content-type: |
||
| 134 | * <code> |
||
| 135 | * // call customCssMinifier($css) for all CSS minification |
||
| 136 | * $options['minifiers'][Minify::TYPE_CSS] = 'customCssMinifier'; |
||
| 137 | * |
||
| 138 | * // don't minify Javascript at all |
||
| 139 | * $options['minifiers'][Minify::TYPE_JS] = ''; |
||
| 140 | * </code> |
||
| 141 | * |
||
| 142 | * 'minifierOptions' : to send options to the minifier function, specify your options |
||
| 143 | * under the key of the content-type. E.g. To send the CSS minifier an option: |
||
| 144 | * <code> |
||
| 145 | * // give CSS minifier array('optionName' => 'optionValue') as 2nd argument |
||
| 146 | * $options['minifierOptions'][Minify::TYPE_CSS]['optionName'] = 'optionValue'; |
||
| 147 | * </code> |
||
| 148 | * |
||
| 149 | * 'contentType' : (optional) this is only needed if your file extension is not |
||
| 150 | * js/css/html. The given content-type will be sent regardless of source file |
||
| 151 | * extension, so this should not be used in a Groups config with other |
||
| 152 | * Javascript/CSS files. |
||
| 153 | * |
||
| 154 | * Any controller options are documented in that controller's setupSources() method. |
||
| 155 | * |
||
| 156 | * @param mixed $controller instance of subclass of Minify_Controller_Base or string |
||
| 157 | * name of controller. E.g. 'Files' |
||
| 158 | * |
||
| 159 | * @param array $options controller/serve options |
||
| 160 | * |
||
| 161 | * @return null|array if the 'quiet' option is set to true, an array |
||
| 162 | * with keys "success" (bool), "statusCode" (int), "content" (string), and |
||
| 163 | * "headers" (array). |
||
| 164 | * |
||
| 165 | * @throws Exception |
||
| 166 | */ |
||
| 167 | public static function serve($controller, $options = array()) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Return combined minified content for a set of sources |
||
| 369 | * |
||
| 370 | * No internal caching will be used and the content will not be HTTP encoded. |
||
| 371 | * |
||
| 372 | * @param array $sources array of filepaths and/or Minify_Source objects |
||
| 373 | * |
||
| 374 | * @param array $options (optional) array of options for serve. By default |
||
| 375 | * these are already set: quiet = true, encodeMethod = '', lastModifiedTime = 0. |
||
| 376 | * |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | public static function combine($sources, $options = array()) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Set $_SERVER['DOCUMENT_ROOT']. On IIS, the value is created from SCRIPT_FILENAME and SCRIPT_NAME. |
||
| 396 | * |
||
| 397 | * @param string $docRoot value to use for DOCUMENT_ROOT |
||
| 398 | */ |
||
| 399 | public static function setDocRoot($docRoot = '') |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Any Minify_Cache_* object or null (i.e. no server cache is used) |
||
| 416 | * |
||
| 417 | * @var Minify_Cache_File |
||
| 418 | */ |
||
| 419 | private static $_cache = null; |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Active controller for current request |
||
| 423 | * |
||
| 424 | * @var Minify_Controller_Base |
||
| 425 | */ |
||
| 426 | protected static $_controller = null; |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Options for current request |
||
| 430 | * |
||
| 431 | * @var array |
||
| 432 | */ |
||
| 433 | protected static $_options = null; |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param string $header |
||
| 437 | * |
||
| 438 | * @param string $url |
||
| 439 | */ |
||
| 440 | protected static function _errorExit($header, $url) |
||
| 441 | { |
||
| 442 | $url = htmlspecialchars($url, ENT_QUOTES); |
||
| 443 | list(,$h1) = explode(' ', $header, 2); |
||
| 444 | $h1 = htmlspecialchars($h1); |
||
| 445 | // FastCGI environments require 3rd arg to header() to be set |
||
| 446 | list(, $code) = explode(' ', $header, 3); |
||
| 447 | header($header, true, $code); |
||
| 448 | header('Content-Type: text/html; charset=utf-8'); |
||
| 449 | echo "<h1>$h1</h1>"; |
||
| 450 | echo "<p>Please see <a href='$url'>$url</a>.</p>"; |
||
| 451 | exit; |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Set up sources to use Minify_Lines |
||
| 456 | * |
||
| 457 | * @param Minify_Source[] $sources Minify_Source instances |
||
| 458 | */ |
||
| 459 | protected static function _setupDebug($sources) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Combines sources and minifies the result. |
||
| 472 | * |
||
| 473 | * @return string |
||
| 474 | * |
||
| 475 | * @throws Exception |
||
| 476 | */ |
||
| 477 | protected static function _combineMinify() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Make a unique cache id for for this request. |
||
| 570 | * |
||
| 571 | * Any settings that could affect output are taken into consideration |
||
| 572 | * |
||
| 573 | * @param string $prefix |
||
| 574 | * |
||
| 575 | * @return string |
||
| 576 | */ |
||
| 577 | protected static function _getCacheId($prefix = 'minify') |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Bubble CSS @imports to the top or prepend a warning if an import is detected not at the top. |
||
| 595 | * |
||
| 596 | * @param string $css |
||
| 597 | * |
||
| 598 | * @return string |
||
| 599 | */ |
||
| 600 | protected static function _handleCssImports($css) |
||
| 621 | } |
||
| 622 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: