Completed
Push — master ( dd1198...8aecdb )
by frank
03:54
created

autoptimizeSpeedupper.php ➔ ao_css_snippetcache()   C

Complexity

Conditions 11
Paths 23

Size

Total Lines 38
Code Lines 28

Duplication

Lines 14
Ratio 36.84 %

Importance

Changes 0
Metric Value
cc 11
eloc 28
nc 23
nop 2
dl 14
loc 38
rs 5.2653
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
*/
5
6
function ao_js_snippetcache($jsin,$jsfilename) {
7
    $md5hash = "snippet_".md5($jsin);
8
    $ccheck = new autoptimizeCache($md5hash,'js');
9
    if($ccheck->check()) {
10
        $scriptsrc = $ccheck->retrieve();
11
    } else {
12
        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 ) ) {
13
            if(class_exists('JSMin')) {
14
                $tmp_jscode = trim(JSMin::minify($jsin));
15
                if (!empty($tmp_jscode)) {
16
                        $scriptsrc = $tmp_jscode;
17
                        unset($tmp_jscode);
18
                } else {
19
                        $scriptsrc=$jsin;
20
                }
21
            } else {
22
                $scriptsrc=$jsin;
23
            }
24
        } else {
25
            // do some housekeeping here to remove comments & linebreaks and stuff
26
            $scriptsrc=preg_replace("#^\s*\/\/.*$#Um","",$jsin);
27
            $scriptsrc=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Us","",$scriptsrc);
28
            $scriptsrc=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $scriptsrc);
29
30
            if ((substr($scriptsrc,-1,1)!==";")&&(substr($scriptsrc,-1,1)!=="}")) {
31
                $scriptsrc.=";";
32
            }
33
        }
34 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...
35
            // don't cache inline CSS or if filter says no
36
            $ccheck->cache($scriptsrc,'text/javascript');
37
        }
38
    }
39
    unset($ccheck);
40
41
    if (get_option("autoptimize_js_trycatch")==="on") {
42
        $scriptsrc="try{".$scriptsrc."}catch(e){}";
43
    }
44
45
    return $scriptsrc;
46
}
47
48
function ao_css_snippetcache($cssin,$cssfilename) {
49
    $md5hash = "snippet_".md5($cssin);
50
    $ccheck = new autoptimizeCache($md5hash,'css');
51
    if($ccheck->check()) {
52
        $stylesrc = $ccheck->retrieve();
53
    } else {
54
        if ( ( strpos($cssfilename,"min.css") === false ) && ( str_replace( apply_filters('autoptimize_filter_css_consider_minified',false), '', $cssfilename ) === $cssfilename ) ) {
55 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...
56
                $tmp_code = trim(Minify_CSS_Compressor::process($cssin));
57
            } else if(class_exists('CSSmin')) {
58
                $cssmin = new CSSmin();
59
                if (method_exists($cssmin,"run")) {
60
                    $tmp_code = trim($cssmin->run($cssin));
61
                } elseif (@is_callable(array($cssmin,"minify"))) {
62
                    $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...
63
                }
64
            }
65
66
            if (!empty($tmp_code)) {
67
                $stylesrc = $tmp_code;
68
                unset($tmp_code);
69
            } else {
70
                $stylesrc = $cssin;
71
            }
72
        } else {
73
            // .min.css -> no heavy-lifting, just some cleanup
74
            $stylesrc=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Us","",$cssin);
75
            $stylesrc=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $stylesrc);
76
            $stylesrc=autoptimizeStyles::fixurls($cssfilename,$stylesrc);
77
        }
78 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...
79
            // only cache CSS if not inline and allowed by filter
80
            $ccheck->cache($stylesrc,'text/css');
81
        }
82
    }
83
    unset($ccheck);
84
    return $stylesrc;
85
}
86
87
add_filter('autoptimize_css_individual_style','ao_css_snippetcache',10,2);
88
add_filter('autoptimize_js_individual_script','ao_js_snippetcache',10,2);
89