Completed
Push — master ( cdcee8...7fc24c )
by recca
05:55 queued 01:13
created

Template::minify()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 22
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 46
ccs 9
cts 9
cp 1
crap 1
rs 8.9411
1
<?php
2
3
namespace Recca0120\LaravelTracy;
4
5
class Template
6
{
7
    protected $attributes = [];
8
9
    /**
10
     * setAttributes.
11
     *
12
     * @param array $attributes
13
     */
14 2
    public function setAttributes($attributes)
15
    {
16 2
        $this->attributes = $attributes;
17 2
    }
18
19
    /**
20
     * render.
21
     *
22
     * @param string $view
23
     *
24
     * @return string
25
     */
26 2
    public function render($view)
27
    {
28 2
        extract($this->attributes);
29
30 2
        ob_start();
31 2
        require $view;
32
33 2
        return $this->minify(ob_get_clean());
34
    }
35
36
    /**
37
     * minify.
38
     *
39
     * @param string $buffer
40
     *
41
     * @return string
42
     */
43 2
    protected function minify($buffer)
44
    {
45
        $search = [
46 2
            '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
47
            '/[^\S ]+\</s',  // strip whitespaces before tags, except space
48
            '/(\s)+/s',       // shorten multiple whitespace sequences
49
        ];
50
51
        $replace = [
52 2
            '>',
53
            '<',
54
            '\\1',
55
        ];
56
57 2
        $buffer = preg_replace($search, $replace, $buffer);
58
59
        //replaces
60 2
        $buffer = str_replace(' type="text/javascript"', '', $buffer);
61 2
        $buffer = str_replace(' type="text/css"', '', $buffer);
62
63
        //replace style elements
64 2
        $buffer = preg_replace_callback("/<style>([\s\S]*?)<\/style>/", function ($matches) {
65
            //minify the css
66
            $css = $matches[1];
67
            $css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);
68
69
            $css = str_replace(["\r\n", "\r", "\n", "\t", '  ', '    ', '     '], '', $css);
70
71
            $css = preg_replace(['(( )+{)', '({( )+)'], '{', $css);
72
            $css = preg_replace(['(( )+})', '(}( )+)', '(;( )*})'], '}', $css);
73
            $css = preg_replace(['(;( )+)', '(( )+;)'], ';', $css);
74
75
            return '<style>'.$css.'</style>';
76 2
        }, $buffer);
77
78
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
51% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
            //replace script elements
80
            $buffer = preg_replace_callback("/<script>([\s\S]*?)<\/script>/", function ($matches) {
81
                $minifiedCode = JSMin::minify($matches[1]);
82
83
                return '<script>'.$minifiedCode.'</script>';
84
            }, $buffer);
85
         */
86
87 2
        return $buffer;
88
    }
89
}
90