Completed
Push — master ( 5ff4ab...0cf65a )
by Alexander
16s queued 10s
created

AssetUtil::resolveAsset()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.9936

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