Passed
Push — master ( c3952a...44cd5d )
by Alexander
01:29
created

AssetUtil   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 41
ccs 10
cts 13
cp 0.7692
c 1
b 0
f 0
rs 10
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 23
    public static function isRelative(string $url): bool
20
    {
21 23
        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 23
    public static function resolveAsset(AssetBundle $bundle, string $pathAsset, array $assetMap): ?string
32
    {
33 23
        if (isset($assetMap[$pathAsset])) {
34 1
            return $assetMap[$pathAsset];
35
        }
36
37 22
        if (!empty($bundle->sourcePath) && static::isRelative($pathAsset)) {
38 6
            $pathAsset = $bundle->sourcePath . '/' . $pathAsset;
39
        }
40
41 22
        $n = mb_strlen($pathAsset, 'utf-8');
42
43 22
        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 22
        return null;
51
    }
52
}
53