Completed
Push — master ( d27635...8b725f )
by frank
02:03
created

autoptimizeSpeedupper.php ➔ ao_js_snippetcacher()   C

Complexity

Conditions 12
Paths 22

Size

Total Lines 41
Code Lines 28

Duplication

Lines 4
Ratio 9.76 %

Importance

Changes 0
Metric Value
cc 12
eloc 28
nc 22
nop 2
dl 4
loc 41
rs 5.1612
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
/*
3
* Autoptimize SpeedUp; minify & cache each JS/ CSS separately
4
* new in Autoptimize 2.2
5
*/
6
7
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
8
9
function ao_js_snippetcacher($jsin,$jsfilename) {
10
    $md5hash = "snippet_".md5($jsin);
11
    $ccheck = new autoptimizeCache($md5hash,'js');
12
    if($ccheck->check()) {
13
        $scriptsrc = $ccheck->retrieve();
14
    } else {
15
        if ( (strpos($jsfilename,"min.js") === false) && ( strpos($jsfilename,"js/jquery/jquery.js") === false ) && ( str_replace(apply_filters('autoptimize_filter_js_consider_minified',false), '', $jsfilename) === $jsfilename ) ) {
16
            if(class_exists('JSMin')) {
17
                $tmp_jscode = trim(JSMin::minify($jsin));
18
                if (!empty($tmp_jscode)) {
19
                        $scriptsrc = $tmp_jscode;
20
                        unset($tmp_jscode);
21
                } else {
22
                        $scriptsrc=$jsin;
23
                }
24
            } else {
25
                $scriptsrc=$jsin;
26
            }
27
        } else {
28
            // do some housekeeping here to remove comments & linebreaks and stuff
29
            $scriptsrc=preg_replace("#^\s*\/\/.*$#Um","",$jsin);
30
            $scriptsrc=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Us","",$scriptsrc);
31
            $scriptsrc=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $scriptsrc);
32
33
            if ((substr($scriptsrc,-1,1)!==";")&&(substr($scriptsrc,-1,1)!=="}")) {
34
                $scriptsrc.=";";
35
            }
36
        }
37 View Code Duplication
        if ( !empty($jsfilename) && str_replace( apply_filters('autoptimize_filter_js_speedup_cache',false), '', $jsfilename ) === $jsfilename ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
            // don't cache inline CSS or if filter says no
39
            $ccheck->cache($scriptsrc,'text/javascript');
40
        }
41
    }
42
    unset($ccheck);
43
44
    if (get_option("autoptimize_js_trycatch")==="on") {
45
        $scriptsrc="try{".$scriptsrc."}catch(e){}";
46
    }
47
48
    return $scriptsrc;
49
}
50
51
function ao_css_snippetcacher($cssin,$cssfilename) {
52
    $md5hash = "snippet_".md5($cssin);
53
    $ccheck = new autoptimizeCache($md5hash,'css');
54
    if($ccheck->check()) {
55
        $stylesrc = $ccheck->retrieve();
56
    } else {
57
        if ( ( strpos($cssfilename,"min.css") === false ) && ( str_replace( apply_filters('autoptimize_filter_css_consider_minified',false), '', $cssfilename ) === $cssfilename ) ) {
58 View Code Duplication
            if (class_exists('Minify_CSS_Compressor')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
                $tmp_code = trim(Minify_CSS_Compressor::process($cssin));
60
            } else if(class_exists('CSSmin')) {
61
                $cssmin = new CSSmin();
62
                if (method_exists($cssmin,"run")) {
63
                    $tmp_code = trim($cssmin->run($cssin));
64
                } elseif (@is_callable(array($cssmin,"minify"))) {
65
                    $tmp_code = trim(CssMin::minify($cssin));
0 ignored issues
show
Bug introduced by
The method minify() cannot be called from this context as it is declared private in class CSSmin.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
Bug introduced by
The call to minify() misses a required argument $linebreak_pos.

This check looks for function calls that miss required arguments.

Loading history...
66
                }
67
            }
68
69
            if (!empty($tmp_code)) {
70
                $stylesrc = $tmp_code;
71
                unset($tmp_code);
72
            } else {
73
                $stylesrc = $cssin;
74
            }
75
        } else {
76
            // .min.css -> no heavy-lifting, just some cleanup
77
            $stylesrc=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Us","",$cssin);
78
            $stylesrc=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $stylesrc);
79
            $stylesrc=autoptimizeStyles::fixurls($cssfilename,$stylesrc);
80
        }
81 View Code Duplication
        if ( !empty($cssfilename) && ( str_replace( apply_filters('autoptimize_filter_css_speedup_cache',false), '', $cssfilename ) === $cssfilename ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
            // only cache CSS if not inline and allowed by filter
83
            $ccheck->cache($stylesrc,'text/css');
84
        }
85
    }
86
    unset($ccheck);
87
    return $stylesrc;
88
}
89
90
function ao_css_speedup_cleanup($cssin) {
91
	// speedupper results in aggregated CSS not being minified, so the filestart-marker AO adds when aggregating need to be removed
92
	return trim(str_replace(array('/*FILESTART*/','/*FILESTART2*/'),'',$cssin));
93
}
94
95
function ao_js_speedup_cleanup($jsin) {
96
	// cleanup
97
	return trim($jsin);
98
}
99
100
add_filter('autoptimize_css_individual_style','ao_css_snippetcacher',10,2);
101
add_filter('autoptimize_js_individual_script','ao_js_snippetcacher',10,2);
102
add_filter('autoptimize_css_after_minify','ao_css_speedup_cleanup',10,1);
103
add_filter('autoptimize_js_after_minify','ao_js_speedup_cleanup',10,1);
104