Passed
Push — master ( 8b9763...cd82df )
by Peter
03:30
created

UrlFixer::fixJs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Assets;
6
7
class UrlFixer
8
{
9
    /** @var string[] */
10
    protected $quotes = ['"', "'"];
11
12
    /** @var string */
13
    protected $cacheUrl = '';
14
15
    /**
16
     * UrlFixer constructor.
17
     *
18
     * @param string $cacheUrl
19
     */
20
    public function __construct(string $cacheUrl)
21
    {
22
        $this->cacheUrl = $cacheUrl;
23
    }
24
25
    /**
26
     * FixCSS tries to compensate for minification of CSS stylesheets
27
     * - It looks for URLs
28
     * - Maintains optional quotes used
29
     * - Calls fixUrl to modify the URL if needed
30
     *
31
     * @param string $content
32
     * @param string $path
33
     *
34
     * @return string
35
     */
36
    public function fixCss(string $content, string $path): string
37
    {
38
        if (!preg_match_all('/url\s*\(\s*(\S*)\s*\)/Umsi', $content, $matches)) {
39
            return $content;
40
        }
41
42
        foreach ($matches[1] as $i => $match) {
43
            if (mb_strlen($match) < 2) {
44
                continue;
45
            }
46
47
            $q = mb_substr($match, 0, 1);
48
            $q = in_array($q, $this->quotes, true) ? $q : '';
49
50
            if ($q && mb_substr($match, -1) !== $q) {
51
                continue;
52
            }
53
54
            $url = $q ? mb_substr($match, 1, -1) : $match;
55
56
            $fixedUrl = $this->fixUrl($url, $path);
57
58
            if ($fixedUrl == $url) {
59
                continue;
60
            }
61
62
            $search  = $matches[0][$i];
63
            $replace = 'url(' . $q . $fixedUrl . $q . ')';
64
65
            $content = str_replace($search, $replace, $content);
66
        }
67
68
        return $content;
69
    }
70
71
    /**
72
     * Fix URL will try to turn a URL into a full qualified URI
73
     *
74
     * - Data URLs skipped
75
     * - Already fully qualitied URIs skipped
76
     *
77
     * @param string $url
78
     * @param string $path
79
     *
80
     * @return string
81
     */
82
    protected function fixUrl(string $url, string $path): string
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

82
    protected function fixUrl(string $url, /** @scrutinizer ignore-unused */ string $path): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
    {
84
        if (mb_substr($url, 0, 5) === 'data:') {
85
            return $url;
86
        }
87
88
        if (strpos($url, '://') !== false) {
89
            return $url;
90
        }
91
92
        $ch = mb_substr($url, 0, 1);
93
        if ($ch === '/') {
94
            return $this->cacheUrl . $url;
95
        }
96
97
        while (mb_substr($url, 0, 2) == './') {
98
            $url = mb_substr($url, 2);
99
        }
100
101
        $out = 0;
102
        while (mb_substr($url, 0, 3) == '../') {
103
            $out++;
104
            $url = mb_substr($url, 3);
105
        }
106
107
        $parts = explode(DIRECTORY_SEPARATOR, $url);
108
        if (count($parts) < $out) {
109
            return $this->cacheUrl . DIRECTORY_SEPARATOR . $parts[count($parts) - 1];
110
        }
111
112
        $url = implode(DIRECTORY_SEPARATOR, array_slice($parts, $out));
113
114
        return $this->cacheUrl . DIRECTORY_SEPARATOR . $url;
115
    }
116
}
117