Completed
Push — master ( c9ea6f...c80560 )
by Christian
02:50
created

PathTools::normalizeDirectorySeparator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Scheb\Tombstone\Analyzer;
4
5
class PathTools
6
{
7 17
    public static function makePathAbsolute(string $path, string $rootDir): string
8
    {
9 17
        if (self::isAbsolutePath($path)) {
10 8
            return $path;
11
        }
12
13
        // Path is root
14 9
        if (!strlen($path) || '.' === $path) {
15 2
            return self::normalizeDirectorySeparator($rootDir);
16
        }
17
18
        // Remove leading "./"
19 7
        if ('.' === $path[0]) {
20 5
            $path = preg_replace('#^(\\.[/\\\\])+#', '', $path);
21
        }
22
23 7
        return self::normalizeDirectorySeparator($rootDir.($path ? DIRECTORY_SEPARATOR.$path : ''));
24
    }
25
26 9
    private static function normalizeDirectorySeparator(string $path): string
27
    {
28 9
        return strtr($path, [
29 9
            '/' => DIRECTORY_SEPARATOR,
30 9
            '\\' => DIRECTORY_SEPARATOR,
31
        ]);
32
    }
33
34 17
    private static function isAbsolutePath(string $path): bool
35
    {
36 17
        if (!strlen($path)) {
37 1
            return false;
38
        }
39
40 16
        return '/' === $path[0]
41 15
            || '\\' === $path[0]
42 16
            || (strlen($path) >= 3 && preg_match('#^[a-zA-Z]:[/\\\\]#', substr($path, 0, 3)));
43
    }
44
}
45