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

AssetUtil::resolveAsset()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 10
c 1
b 0
f 0
nc 7
nop 3
dl 0
loc 20
ccs 0
cts 16
cp 0
crap 56
rs 8.8333
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