Passed
Push — master ( 5a3114...9212c9 )
by Alexander
06:49
created

AssetUtil   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 41
ccs 0
cts 20
cp 0
rs 10
c 1
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B resolveAsset() 0 20 7
A isRelative() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Assets;
6
7
/**
8
 * AssetUtil shared functions.
9
 */
10
final class AssetUtil
11
{
12
    /**
13
     * Returns a value indicating whether a URL is relative.
14
     *
15
     * A relative URL does not have host info part.
16
     * @param string $url the URL to be checked
17
     * @return bool whether the URL is relative
18
     */
19
    public static function isRelative(string $url): bool
20
    {
21
        return strncmp($url, '//', 2) && strpos($url, '://') === false;
22
    }
23
24
    /**
25
     * @param AssetBundle $bundle
26
     * @param string $pathAsset
27
     * @param array $assetMap
28
     *
29
     * @return string|null
30
     */
31
    public static function resolveAsset(AssetBundle $bundle, string $pathAsset, array $assetMap): ?string
32
    {
33
        if (isset($assetMap[$pathAsset])) {
34
            return $assetMap[$pathAsset];
35
        }
36
37
        if (!empty($bundle->sourcePath) && static::isRelative($pathAsset)) {
38
            $pathAsset = $bundle->sourcePath . '/' . $pathAsset;
39
        }
40
41
        $n = mb_strlen($pathAsset, 'utf-8');
42
43
        foreach ($assetMap as $from => $to) {
44
            $n2 = mb_strlen($from, 'utf-8');
45
            if ($n2 <= $n && substr_compare($pathAsset, $from, $n - $n2, $n2) === 0) {
46
                return $to;
47
            }
48
        }
49
50
        return null;
51
    }
52
}
53