Passed
Push — master ( 48c4ea...d64961 )
by Tom
02:50
created

LibFs::normalizePathSegments()   B

Complexity

Conditions 10
Paths 15

Size

Total Lines 52
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 29
c 1
b 0
f 0
nc 15
nop 1
dl 0
loc 52
ccs 30
cts 30
cp 1
crap 10
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines;
6
7
use UnexpectedValueException;
8
9
/**
10
 * Class LibFs - Low level file-system utility functions
11
 *
12
 * @covers \Ktomk\Pipelines\LibFs
13
 */
14
class LibFs
15
{
16
    /**
17
     * @param string $path
18
     * @param string $mode [optional]
19
     *
20
     * @return bool
21
     */
22 2
    public static function canFopen($path, $mode = null)
23
    {
24 2
        if (null === $mode) {
25 1
            $mode = 'rb';
26
        }
27
28 2
        $handle = @fopen($path, $mode);
29 2
        if (false === $handle) {
30 1
            return false;
31
        }
32
33
        /** @scrutinizer ignore-unhandled */
34 1
        @fclose($handle);
35
36 1
        return true;
37
    }
38
39
    /**
40
     * locate (readable) file by basename upward all parent directories
41
     *
42
     * @param string $basename
43
     * @param string $directory [optional] directory to operate from, defaults
44
     *               to "." (relative path of present working directory)
45
     *
46
     * @return null|string
47
     */
48 4
    public static function fileLookUp($basename, $directory = null)
49
    {
50 4
        if ('' === $directory || null === $directory) {
51 1
            $directory = '.';
52
        }
53
54
        for (
55 4
            $dirName = $directory, $old = null;
56 4
            $old !== $dirName;
57 2
            $old = $dirName, $dirName = dirname($dirName)
58
        ) {
59 4
            $test = $dirName . '/' . $basename;
60 4
            if (self::isReadableFile($test)) {
61 3
                return $test;
62
            }
63
        }
64
65 1
        return null;
66
    }
67
68
    /**
69
     * see 2.2 Standards permit the exclusion of bad filenames / POSIX.1-2008
70
     *
71
     * @link https://dwheeler.com/essays/fixing-unix-linux-filenames.html
72
     *
73
     * @param string $filename
74
     *
75
     * @return bool
76
     */
77 10
    public static function isPortableFilename($filename)
78
    {
79
        # A-Z, a-z, 0-9, <period>, <underscore>, and <hyphen>)
80 10
        $result = preg_match('(^(?!-)[A-Za-z0-9._-]+$)', $filename);
81 10
        if (false === $result) {
82
            // @codeCoverageIgnoreStart
83
            throw new UnexpectedValueException('preg_match pattern failed');
84
            // @codeCoverageIgnoreEnd
85
        }
86
87 10
        return 1 === $result;
88
    }
89
90
    /**
91
     * @param string $path
92
     *
93
     * @return bool
94
     */
95 11
    public static function isReadableFile($path)
96
    {
97 11
        if (!is_file($path)) {
98 4
            return false;
99
        }
100
101 9
        return is_readable($path) ?: self::canFopen($path, 'rb');
102
    }
103
104
    /**
105
     * create directory if not yet exists
106
     *
107
     * @param string $path
108
     * @param int $mode [optional]
109
     *
110
     * @return string
111
     */
112 7
    public static function mkDir($path, $mode = 0777)
113
    {
114 7
        if (!is_dir($path)) {
115
            /** @noinspection NestedPositiveIfStatementsInspection */
116 6
            if (!mkdir($path, $mode, true) && !is_dir($path)) {
117
                // @codeCoverageIgnoreStart
118
                throw new \RuntimeException(
119
                    sprintf('Directory "%s" was not created', $path)
120
                );
121
                // @codeCoverageIgnoreEnd
122
            }
123
        }
124
125 7
        return $path;
126
    }
127
128
    /**
129
     * rename a file
130
     *
131
     * @param string $old
132
     * @param string $new
133
     *
134
     * @return string new file-name
135
     */
136 1
    public static function rename($old, $new)
137
    {
138 1
        if (!@rename($old, $new)) {
139 1
            throw new \RuntimeException(sprintf('Failed to rename "%s" to "%s"', $old, $new));
140
        }
141
142 1
        return $new;
143
    }
144
145
    /**
146
     * @param string $file
147
     *
148
     * @return string
149
     */
150 5
    public static function rm($file)
151
    {
152 5
        if (self::isReadableFile($file)) {
153 5
            unlink($file);
154
        }
155
156 5
        return $file;
157
    }
158
159
    /**
160
     * @param string $dir
161
     *
162
     * @throws UnexpectedValueException
163
     *
164
     * @return void
165
     */
166 3
    public static function rmDir($dir)
167
    {
168 3
        $result = @lstat($dir);
169 3
        if (false === $result) {
170 1
            return;
171
        }
172
173 3
        $dirs = array();
174 3
        $dirs[] = $dir;
175 3
        for ($i = 0; isset($dirs[$i]); $i++) {
176 3
            $current = $dirs[$i];
177 3
            $result = @scandir($current);
178 3
            if (false === $result) {
179 1
                throw new UnexpectedValueException(sprintf('Failed to open directory: %s', $current));
180
            }
181 2
            $files = array_diff($result, array('.', '..'));
182 2
            foreach ($files as $file) {
183 1
                $path = "${current}/${file}";
184 1
                if (is_dir($path)) {
185 1
                    $dirs[] = $path;
186 1
                } elseif (is_file($path)) {
187 1
                    self::rm($path);
188
                }
189
            }
190
        }
191
192 2
        while (null !== ($pop = array_pop($dirs))) {
193
            /* @scrutinizer ignore-unhandled */
194 2
            @rmdir($pop);
195
        }
196 2
    }
197
198
    /**
199
     * create symbolic link, recreate if it exists
200
     *
201
     * @param string $target
202
     * @param string $link
203
     *
204
     * @return void
205
     */
206
    public static function symlink($target, $link)
207
    {
208 1
        self::unlink($link);
209 1
        symlink($target, $link);
210 1
    }
211
212
    /**
213
     * @param string $link
214
     *
215
     * @return void
216
     */
217
    public static function unlink($link)
218
    {
219 1
        if (is_link($link)) {
220 1
            unlink($link);
221
        }
222 1
    }
223
}
224