Passed
Push — master ( 42c4d1...80535d )
by Allan
02:39 queued 12s
created

PathResolver::getAncestorDirForFile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 19
rs 9.9666
c 1
b 0
f 0
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\FileSystem;
7
8
use Vaimo\ComposerPatches\Utils\PathUtils;
9
10
class PathResolver
11
{
12
    /**
13
     * Find closest file path for specified file name while iterating upwards in the file-tree
14
     *
15
     * @param string $path
16
     * @param string $fileName
17
     * @return bool|string
18
     */
19
    public static function getClosestDirForFile($path, $fileName)
20
    {
21
        while (true) {
22
            if (\is_dir($path) && \file_exists(PathUtils::composePath($path, $fileName))) {
23
                return $path;
24
            }
25
26
            $parent = \dirname($path);
27
28
            if ($parent === $path) {
29
                break;
30
            }
31
32
            $path = $parent;
33
        }
34
35
        return false;
36
    }
37
38
    public static function getAncestorDirForFile($filePath, $fileName)
39
    {
40
        $path = $filePath;
41
42
        $rootPath = '';
43
44
        while (true) {
45
            if ($path === \DIRECTORY_SEPARATOR) {
46
                break;
47
            }
48
49
            if (\is_file(PathUtils::composePath($path, $fileName))) {
50
                $rootPath = $path;
51
            }
52
53
            $path = \dirname($path);
54
        }
55
56
        return $rootPath;
57
    }
58
}
59