| Conditions | 63 |
| Paths | > 20000 |
| Total Lines | 253 |
| Code Lines | 166 |
| Lines | 22 |
| Ratio | 8.7 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 199 | public function minify() {
|
||
| 200 | foreach($this->css as $group) {
|
||
| 201 | list($media,$css) = $group; |
||
| 202 | if(preg_match('#^INLINE;#',$css)) {
|
||
| 203 | // <style> |
||
| 204 | $css = preg_replace('#^INLINE;#','',$css);
|
||
| 205 | $css = $this->fixurls(ABSPATH.'/index.php',$css); |
||
| 206 | $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, "" ); |
||
| 207 | if ( has_filter('autoptimize_css_individual_style') && !empty($tmpstyle) ) {
|
||
| 208 | $css=$tmpstyle; |
||
| 209 | $this->alreadyminified=true; |
||
| 210 | } |
||
| 211 | } else {
|
||
| 212 | //<link> |
||
| 213 | if($css !== false && file_exists($css) && is_readable($css)) {
|
||
| 214 | $cssPath = $css; |
||
| 215 | $css = $this->fixurls($cssPath,file_get_contents($cssPath)); |
||
| 216 | $css = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$css);
|
||
| 217 | $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, $cssPath ); |
||
| 218 | View Code Duplication | if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
|
|
| 219 | $css=$tmpstyle; |
||
| 220 | $this->alreadyminified=true; |
||
| 221 | } else if ($this->can_inject_late($cssPath,$css)) {
|
||
| 222 | $css="/*!%%INJECTLATER%%".base64_encode($cssPath)."|".md5($css)."%%INJECTLATER%%*/"; |
||
| 223 | } |
||
| 224 | } else {
|
||
| 225 | // Couldn't read CSS. Maybe getpath isn't working? |
||
| 226 | $css = ''; |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | foreach($media as $elem) {
|
||
| 231 | if(!isset($this->csscode[$elem])) |
||
| 232 | $this->csscode[$elem] = ''; |
||
| 233 | $this->csscode[$elem] .= "\n/*FILESTART*/".$css; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | // Check for duplicate code |
||
| 238 | $md5list = array(); |
||
| 239 | $tmpcss = $this->csscode; |
||
| 240 | foreach($tmpcss as $media => $code) {
|
||
| 241 | $md5sum = md5($code); |
||
| 242 | $medianame = $media; |
||
| 243 | foreach($md5list as $med => $sum) {
|
||
| 244 | // If same code |
||
| 245 | if($sum === $md5sum) {
|
||
| 246 | //Add the merged code |
||
| 247 | $medianame = $med.', '.$media; |
||
| 248 | $this->csscode[$medianame] = $code; |
||
| 249 | $md5list[$medianame] = $md5list[$med]; |
||
| 250 | unset($this->csscode[$med], $this->csscode[$media]); |
||
| 251 | unset($md5list[$med]); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | $md5list[$medianame] = $md5sum; |
||
| 255 | } |
||
| 256 | unset($tmpcss); |
||
| 257 | |||
| 258 | // Manage @imports, while is for recursive import management |
||
| 259 | foreach ($this->csscode as &$thiscss) {
|
||
| 260 | // Flag to trigger import reconstitution and var to hold external imports |
||
| 261 | $fiximports = false; |
||
| 262 | $external_imports = ""; |
||
| 263 | |||
| 264 | // remove comments to avoid importing commented-out imports |
||
| 265 | $thiscss_nocomments = preg_replace('#/\*.*\*/#Us','',$thiscss);
|
||
| 266 | |||
| 267 | while(preg_match_all('#@import.*(?:;|$)#Um',$thiscss_nocomments,$matches)) {
|
||
| 268 | foreach($matches[0] as $import) {
|
||
| 269 | if ($this->isremovable($import,$this->cssremovables)) {
|
||
| 270 | $thiscss = str_replace($import,'',$thiscss); |
||
| 271 | $import_ok = true; |
||
| 272 | } else {
|
||
| 273 | $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#','$1',trim($import))," \t\n\r\0\x0B\"'");
|
||
| 274 | $path = $this->getpath($url); |
||
| 275 | $import_ok = false; |
||
| 276 | if (file_exists($path) && is_readable($path)) {
|
||
| 277 | $code = addcslashes($this->fixurls($path,file_get_contents($path)),"\\"); |
||
| 278 | $code = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$code);
|
||
| 279 | $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $code, "" ); |
||
| 280 | View Code Duplication | if ( has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
|
|
| 281 | $code=$tmpstyle; |
||
| 282 | $this->alreadyminified=true; |
||
| 283 | } else if ($this->can_inject_late($path,$code)) {
|
||
| 284 | $code="/*!%%INJECTLATER%%".base64_encode($path)."|".md5($code)."%%INJECTLATER%%*/"; |
||
| 285 | } |
||
| 286 | |||
| 287 | if(!empty($code)) {
|
||
| 288 | $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import,'#').'#Us','/*FILESTART2*/'.$code.'$1',$thiscss);
|
||
| 289 | if (!empty($tmp_thiscss)) {
|
||
| 290 | $thiscss = $tmp_thiscss; |
||
| 291 | $import_ok = true; |
||
| 292 | unset($tmp_thiscss); |
||
| 293 | } |
||
| 294 | unset($code); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | if (!$import_ok) {
|
||
| 300 | // external imports and general fall-back |
||
| 301 | $external_imports .= $import; |
||
| 302 | $thiscss = str_replace($import,'',$thiscss); |
||
| 303 | $fiximports = true; |
||
| 304 | } |
||
| 305 | } |
||
| 306 | $thiscss = preg_replace('#/\*FILESTART\*/#','',$thiscss);
|
||
| 307 | $thiscss = preg_replace('#/\*FILESTART2\*/#','/*FILESTART*/',$thiscss);
|
||
| 308 | |||
| 309 | // and update $thiscss_nocomments before going into next iteration in while loop |
||
| 310 | $thiscss_nocomments=preg_replace('#/\*.*\*/#Us','',$thiscss);
|
||
| 311 | } |
||
| 312 | unset($thiscss_nocomments); |
||
| 313 | |||
| 314 | // add external imports to top of aggregated CSS |
||
| 315 | if($fiximports) {
|
||
| 316 | $thiscss=$external_imports.$thiscss; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | unset($thiscss); |
||
| 320 | |||
| 321 | // $this->csscode has all the uncompressed code now. |
||
| 322 | foreach($this->csscode as &$code) {
|
||
| 323 | // Check for already-minified code |
||
| 324 | $hash = md5($code); |
||
| 325 | $ccheck = new autoptimizeCache($hash,'css'); |
||
| 326 | if($ccheck->check()) {
|
||
| 327 | $code = $ccheck->retrieve(); |
||
| 328 | $this->hashmap[md5($code)] = $hash; |
||
| 329 | continue; |
||
| 330 | } |
||
| 331 | unset($ccheck); |
||
| 332 | |||
| 333 | // Do the imaging! |
||
| 334 | $imgreplace = array(); |
||
| 335 | preg_match_all( self::ASSETS_REGEX, $code, $matches ); |
||
| 336 | |||
| 337 | if ( ($this->datauris == true) && (function_exists('base64_encode')) && (is_array($matches)) ) {
|
||
| 338 | foreach($matches[1] as $count => $quotedurl) {
|
||
| 339 | $iurl = trim($quotedurl," \t\n\r\0\x0B\"'"); |
||
| 340 | |||
| 341 | // if querystring, remove it from url |
||
| 342 | if (strpos($iurl,'?') !== false) { $iurl = strtok($iurl,'?'); }
|
||
| 343 | |||
| 344 | $ipath = $this->getpath($iurl); |
||
| 345 | |||
| 346 | $datauri_max_size = 4096; |
||
| 347 | $datauri_max_size = (int) apply_filters( 'autoptimize_filter_css_datauri_maxsize', $datauri_max_size ); |
||
| 348 | $datauri_exclude = apply_filters( 'autoptimize_filter_css_datauri_exclude', ""); |
||
| 349 | if (!empty($datauri_exclude)) {
|
||
| 350 | $no_datauris=array_filter(array_map('trim',explode(",",$datauri_exclude)));
|
||
| 351 | foreach ($no_datauris as $no_datauri) {
|
||
| 352 | if (strpos($iurl,$no_datauri)!==false) {
|
||
| 353 | $ipath=false; |
||
| 354 | break; |
||
| 355 | } |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | if($ipath != false && preg_match('#\.(jpe?g|png|gif|bmp)$#i',$ipath) && file_exists($ipath) && is_readable($ipath) && filesize($ipath) <= $datauri_max_size) {
|
||
| 360 | $ihash=md5($ipath); |
||
| 361 | $icheck = new autoptimizeCache($ihash,'img'); |
||
| 362 | if($icheck->check()) {
|
||
| 363 | // we have the base64 image in cache |
||
| 364 | $headAndData=$icheck->retrieve(); |
||
| 365 | $_base64data=explode(";base64,",$headAndData);
|
||
| 366 | $base64data=$_base64data[1]; |
||
| 367 | } else {
|
||
| 368 | // It's an image and we don't have it in cache, get the type |
||
| 369 | $explA=explode('.',$ipath);
|
||
| 370 | $type=end($explA); |
||
| 371 | |||
| 372 | switch($type) {
|
||
| 373 | case 'jpeg': |
||
| 374 | $dataurihead = 'data:image/jpeg;base64,'; |
||
| 375 | break; |
||
| 376 | case 'jpg': |
||
| 377 | $dataurihead = 'data:image/jpeg;base64,'; |
||
| 378 | break; |
||
| 379 | case 'gif': |
||
| 380 | $dataurihead = 'data:image/gif;base64,'; |
||
| 381 | break; |
||
| 382 | case 'png': |
||
| 383 | $dataurihead = 'data:image/png;base64,'; |
||
| 384 | break; |
||
| 385 | case 'bmp': |
||
| 386 | $dataurihead = 'data:image/bmp;base64,'; |
||
| 387 | break; |
||
| 388 | default: |
||
| 389 | $dataurihead = 'data:application/octet-stream;base64,'; |
||
| 390 | } |
||
| 391 | |||
| 392 | // Encode the data |
||
| 393 | $base64data = base64_encode(file_get_contents($ipath)); |
||
| 394 | $headAndData=$dataurihead.$base64data; |
||
| 395 | |||
| 396 | // Save in cache |
||
| 397 | $icheck->cache($headAndData,"text/plain"); |
||
| 398 | } |
||
| 399 | unset($icheck); |
||
| 400 | |||
| 401 | // Add it to the list for replacement |
||
| 402 | $imgreplace[$matches[0][$count]] = str_replace($quotedurl,$headAndData,$matches[0][$count]); |
||
| 403 | } else {
|
||
| 404 | // just cdn the URL if applicable |
||
| 405 | if (!empty($this->cdn_url)) {
|
||
| 406 | $imgreplace[$matches[0][$count]] = str_replace($quotedurl,$this->maybe_cdn_urls($quotedurl),$matches[0][$count]); |
||
| 407 | } |
||
| 408 | } |
||
| 409 | } |
||
| 410 | } else if ((is_array($matches)) && (!empty($this->cdn_url))) {
|
||
| 411 | // change urls to cdn-url |
||
| 412 | foreach($matches[1] as $count => $quotedurl) {
|
||
| 413 | $imgreplace[$matches[0][$count]] = str_replace($quotedurl,$this->maybe_cdn_urls($quotedurl),$matches[0][$count]); |
||
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 417 | if(!empty($imgreplace)) {
|
||
| 418 | $code = str_replace(array_keys($imgreplace),array_values($imgreplace),$code); |
||
| 419 | } |
||
| 420 | |||
| 421 | // Minify |
||
| 422 | if (($this->alreadyminified!==true) && (apply_filters( "autoptimize_css_do_minify", true))) {
|
||
| 423 | View Code Duplication | if (class_exists('Minify_CSS_Compressor')) {
|
|
| 424 | $tmp_code = trim(Minify_CSS_Compressor::process($code)); |
||
| 425 | } else if(class_exists('CSSmin')) {
|
||
| 426 | $cssmin = new CSSmin(); |
||
| 427 | if (method_exists($cssmin,"run")) {
|
||
| 428 | $tmp_code = trim($cssmin->run($code)); |
||
| 429 | } elseif (@is_callable(array($cssmin,"minify"))) {
|
||
| 430 | $tmp_code = trim(CssMin::minify($code)); |
||
| 431 | } |
||
| 432 | } |
||
| 433 | if (!empty($tmp_code)) {
|
||
| 434 | $code = $tmp_code; |
||
| 435 | unset($tmp_code); |
||
| 436 | } |
||
| 437 | } |
||
| 438 | |||
| 439 | $code = $this->inject_minified($code); |
||
| 440 | |||
| 441 | $tmp_code = apply_filters( 'autoptimize_css_after_minify', $code ); |
||
| 442 | if (!empty($tmp_code)) {
|
||
| 443 | $code = $tmp_code; |
||
| 444 | unset($tmp_code); |
||
| 445 | } |
||
| 446 | |||
| 447 | $this->hashmap[md5($code)] = $hash; |
||
| 448 | } |
||
| 449 | unset($code); |
||
| 450 | return true; |
||
| 451 | } |
||
| 452 | |||
| 691 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..