Test Failed
Push — master ( 62e8aa...e74fb5 )
by Julien
04:23
created

Url::getAbsolutePath()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 17
c 1
b 1
f 0
dl 0
loc 27
ccs 0
cts 17
cp 0
rs 8.8333
cc 7
nc 7
nop 1
crap 56
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit;
13
14
/**
15
 * {@inheritDoc}
16
 */
17
class Url extends \Phalcon\Url
18
{
19
    /**
20
     * {@inheritdoc}
21
     *
22
     * @param array|string|null $uri
23
     * @param mixed $args
24
     * @param bool|null $local
25
     * @param mixed $baseUri
26
     *
27
     * @return string
28
     */
29
    public function get($uri = null, $args = null, bool $local = null, $baseUri = null): string
30
    {
31
        return self::getAbsolutePath(parent::get($uri, $args, $local, $baseUri));
32
    }
33
    
34
    /**
35
     * @param string $path
36
     *
37
     * @return string
38
     */
39
    public static function getAbsolutePath(string $path): string
40
    {
41
        if (strpos($path, 'https://') === 0) {
42
            return $path;
43
        }
44
        if (strpos($path, 'http://') === 0) {
45
            return $path;
46
        }
47
        if (strpos($path, '//') === 0) {
48
            return $path;
49
        }
50
        
51
        $path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
52
        $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'mb_strlen');
53
        $absolutes = [];
54
        foreach ($parts as $part) {
55
            if ('.' === $part) {
56
                continue;
57
            }
58
            if ('..' === $part) {
59
                array_pop($absolutes);
60
            }
61
            else {
62
                $absolutes[] = $part;
63
            }
64
        }
65
        return '/' . implode(DIRECTORY_SEPARATOR, $absolutes);
66
    }
67
}
68