Completed
Push — master ( 609211...524389 )
by Nicolai
04:58
created

FileUtil::normalizePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Util;
5
6
/**
7
 * Class ArrayUtil
8
 *
9
 * @package SmartWeb\ModuleTesting\Util
10
 */
11
final class FileUtil
12
{
13
    
14
    /**
15
     * @param string $path
16
     * @param string $dir
17
     *
18
     * @return string
19
     */
20
    public static function findParentDir(string $path, string $dir) : string
21
    {
22
        $path = self::normalizePath($path);
23
        
24
        return self::pathContainsDir($path, $dir)
25
            ? self::normalizePath($path . DIRECTORY_SEPARATOR . $dir)
26
            : self::findParentDir(self::parentDir($path), $dir);
27
    }
28
    
29
    /**
30
     * @param string $path
31
     * @param string $dir
32
     *
33
     * @return bool
34
     */
35
    private static function pathContainsDir(string $path, string $dir) : bool
36
    {
37
        return is_dir($path . DIRECTORY_SEPARATOR . $dir);
38
    }
39
    
40
    /**
41
     * @param string $path
42
     *
43
     * @return string
44
     */
45
    private static function parentDir(string $path) : string
46
    {
47
        return self::normalizePath($path . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
48
    }
49
    
50
    /**
51
     * @param string $path
52
     *
53
     * @return string
54
     */
55
    public static function normalizePath(string $path) : string
56
    {
57
        return (string)str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $path);
58
    }
59
}
60