FileSystem   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 47
ccs 15
cts 18
cp 0.8333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeDirectorySeparator() 0 4 1
C bubble() 0 26 7
1
<?php
2
/*
3
 * Copyright (C) 2017 by TEQneers GmbH & Co. KG
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24
/**
25
 * Git Stream Wrapper for PHP
26
 *
27
 * @category   TQ
28
 * @package    TQ_VCS
29
 * @subpackage VCS
30
 * @copyright  Copyright (C) 2018 by TEQneers GmbH & Co. KG
31
 */
32
33
namespace TQ\Vcs;
34
35
/**
36
 * A collection of methods used for interacting with the file system
37
 *
38
 * @author     Stefan Gehrig <gehrigteqneers.de>
39
 * @category   TQ
40
 * @package    TQ_VCS
41
 * @subpackage VCS
42
 * @copyright  Copyright (C) 2018 by TEQneers GmbH & Co. KG
43
 */
44
class FileSystem
45
{
46
    /**
47
     * Normalizes the directory separator to /
48
     *
49
     * @param   string  $path       The path
50
     * @return  string              The normalized path
51
     */
52 197
    public static function normalizeDirectorySeparator($path)
53
    {
54 197
        return str_replace(array('\\', '/'), '/', $path);
55
    }
56
57
    /**
58
     * Bubbles up a path until $comparator returns true
59
     *
60
     * @param   string      $path              The path
61
     * @param   \Closure    $comparator        The callback used inside when bubbling to determine a finding
62
     * @return  string|null                    The path that is found or NULL otherwise
63
     */
64 197
    public static function bubble($path, \Closure $comparator)
65
    {
66 197
        $found   = null;
67 197
        $path    = self::normalizeDirectorySeparator($path);
68
69 197
        $drive  = null;
70 197
        if (preg_match('~^(\w:)(.+)~', $path, $parts)) {
71
            $drive  = $parts[1];
72
            $path   = $parts[2];
73
        }
74
75 197
        $pathParts  = explode('/', $path);
76 197
        while (count($pathParts) > 0 && $found === null) {
77 197
            $path   = implode('/', $pathParts);
78 197
            if ($comparator($path)) {
79 191
                $found  = $path;
80
            }
81 197
            array_pop($pathParts);
82
        }
83
84 197
        if ($drive && $found) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $drive of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $found of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
85
            $found  = $drive.$found;
86
        }
87
88 197
        return $found;
89
    }
90
}
91