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

AssetUtil   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isRelative() 0 3 2
B resolveAsset() 0 20 7
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