Passed
Push — master ( 64da0c...c6d718 )
by Jean-Christophe
11:31
created

UFileSystem   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Test Coverage

Coverage 85%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 47
eloc 93
c 1
b 0
f 0
dl 0
loc 243
ccs 85
cts 100
cp 0.85
rs 8.64

14 Methods

Rating   Name   Duplication   Size   Complexity  
A glob_recursive() 0 6 2
A load() 0 5 2
A safeMkdir() 0 4 2
A cleanFilePathname() 0 9 3
A deleteFile() 0 4 2
A cleanPathname() 0 12 4
A lastModified() 0 2 1
A getDirFromNamespace() 0 2 1
A delTree() 0 6 3
A tryToRequire() 0 6 2
A deleteAllFilesFromFolder() 0 5 3
B getLines() 0 29 9
B getLinesByLine() 0 26 9
A relativePath() 0 11 4

How to fix   Complexity   

Complex Class

Complex classes like UFileSystem often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use UFileSystem, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ubiquity\utils\base;
4
5
use Ubiquity\utils\base\traits\UFileSystemWriter;
6
7
/**
8
 * File system utilities
9
 * Ubiquity\utils\base$UFileSystem
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.4
14
 *
15
 */
16
class UFileSystem {
17
	use UFileSystemWriter;
18
19
	/**
20
	 * Find recursively pathnames matching a pattern
21
	 *
22
	 * @param string $pattern
23
	 * @param integer $flags
24
	 * @return array
25
	 */
26 27
	public static function glob_recursive($pattern, $flags = 0) {
27 27
		$files = \glob ( $pattern, $flags );
28 27
		foreach ( \glob ( \dirname ( $pattern ) . '/*', GLOB_ONLYDIR | GLOB_NOSORT ) as $dir ) {
29 21
			$files = \array_merge ( $files, self::glob_recursive ( $dir . '/' . \basename ( $pattern ), $flags ) );
30
		}
31 27
		return $files;
32
	}
33
34
	/**
35
	 * Deletes all files from a folder (not in subfolders)
36
	 *
37
	 * @param string $folder
38
	 * @param string $mask
39
	 */
40 2
	public static function deleteAllFilesFromFolder($folder, $mask = '*') {
41 2
		$files = \glob ( $folder . \DS . $mask );
42 2
		foreach ( $files as $file ) {
43 2
			if (\is_file ( $file ))
44 2
				\unlink ( $file );
45
		}
46 2
	}
47
48
	/**
49
	 * Deletes a file, in safe mode
50
	 *
51
	 * @param string $filename
52
	 * @return boolean
53
	 */
54 1
	public static function deleteFile($filename) {
55 1
		if (\file_exists ( $filename ))
56 1
			return \unlink ( $filename );
57
		return false;
58
	}
59
60
	/**
61
	 * Tests the existance and eventually creates a directory
62
	 *
63
	 * @param string $dir
64
	 * @param int $mode
65
	 * @param boolean $recursive
66
	 * @return boolean
67
	 */
68 36
	public static function safeMkdir($dir, $mode = 0777, $recursive = true) {
69 36
		if (! \is_dir ( $dir ))
70 10
			return \mkdir ( $dir, $mode, $recursive );
71 31
		return true;
72
	}
73
74
	/**
75
	 * Cleans a directory path by removing double backslashes or slashes and using DIRECTORY_SEPARATOR
76
	 *
77
	 * @param string $path
78
	 * @return string
79
	 */
80 14
	public static function cleanPathname($path) {
81 14
		if (UString::isNotNull ( $path )) {
82 14
			if (\DS === "/")
83 14
				$path = \str_replace ( "\\", \DS, $path );
84
			else
85
				$path = \str_replace ( "/", \DS, $path );
86 14
			$path = \str_replace ( \DS . \DS, \DS, $path );
87 14
			if (! UString::endswith ( $path, \DS )) {
88 5
				$path = $path . \DS;
89
			}
90
		}
91 14
		return $path;
92
	}
93
94
	/**
95
	 * Cleans a file path by removing double backslashes or slashes and using DIRECTORY_SEPARATOR
96
	 *
97
	 * @param string $path
98
	 * @return string
99
	 */
100 16
	public static function cleanFilePathname($path) {
101 16
		if (UString::isNotNull ( $path )) {
102 16
			if (\DS === "/")
103 16
				$path = \str_replace ( "\\", \DS, $path );
104
			else
105
				$path = \str_replace ( "/", \DS, $path );
106 16
			$path = \str_replace ( \DS . \DS, \DS, $path );
107
		}
108 16
		return $path;
109
	}
110
111
	/**
112
	 * Try to require a file, in safe mode
113
	 *
114
	 * @param string $file
115
	 * @return boolean
116
	 */
117
	public static function tryToRequire($file) {
118
		if (\file_exists ( $file )) {
119
			require_once ($file);
120
			return true;
121
		}
122
		return false;
123
	}
124
125
	/**
126
	 * Gets file modification time
127
	 *
128
	 * @param string $filename
129
	 * @return number
130
	 */
131 1
	public static function lastModified($filename) {
132 1
		return \filemtime ( $filename );
133
	}
134
135
	/**
136
	 * Reads entire file into a string in safe mode
137
	 *
138
	 * @param string $filename
139
	 * @return string|boolean
140
	 */
141 2
	public static function load($filename) {
142 2
		if (\file_exists ( $filename )) {
143 2
			return \file_get_contents ( $filename );
144
		}
145
		return false;
146
	}
147
148
	/**
149
	 * Returns the directory base on ROOT, corresponding to a namespace
150
	 *
151
	 * @param string $ns
152
	 * @return string
153
	 */
154 4
	public static function getDirFromNamespace($ns) {
155 4
		return \ROOT . \DS . \str_replace ( "\\", \DS, $ns );
156
	}
157
158
	/**
159
	 * Deletes recursivly a folder and its content
160
	 *
161
	 * @param string $dir
162
	 * @return boolean
163
	 */
164 2
	public static function delTree($dir) {
165 2
		$files = \array_diff ( scandir ( $dir ), array ('.','..' ) );
166 2
		foreach ( $files as $file ) {
167 2
			(\is_dir ( "$dir/$file" )) ? self::delTree ( "$dir/$file" ) : \unlink ( "$dir/$file" );
168
		}
169 2
		return \rmdir ( $dir );
170
	}
171
172
	/**
173
	 * Returns the lines of a file in an array
174
	 *
175
	 * @param string $filename
176
	 * @param boolean $reverse
177
	 * @param null|int $maxLines
178
	 * @param callback $lineCallback
179
	 * @return array
180
	 */
181 2
	public static function getLines($filename, $reverse = false, $maxLines = null, $lineCallback = null) {
182 2
		if (\file_exists ( $filename )) {
183 2
			if ($reverse && isset ( $maxLines )) {
184 2
				$result = [ ];
185 2
				$fl = \fopen ( $filename, "r" );
186 2
				for($x_pos = 0, $ln = 0, $lines = [ ]; \fseek ( $fl, $x_pos, SEEK_END ) !== - 1; $x_pos --) {
187 2
					$char = \fgetc ( $fl );
188 2
					if ($char === "\n") {
189 2
						if (\is_callable ( $lineCallback )) {
190 2
							$lineCallback ( $result, $lines [$ln] );
191
						} else {
192
							$result [] = $lines [$ln];
193
						}
194 2
						if (isset ( $maxLines ) && \sizeof ( $result ) >= $maxLines) {
195 2
							\fclose ( $fl );
196 2
							return $result;
197
						}
198 2
						$ln ++;
199 2
						continue;
200
					}
201 2
					$lines [$ln] = $char . ($lines [$ln] ?? '');
202
				}
203
				\fclose ( $fl );
204
				return $result;
205
			} else {
206 1
				return self::getLinesByLine ( $filename, $reverse, $maxLines, $lineCallback );
207
			}
208
		}
209
		return [ ];
210
	}
211
212
	/**
213
	 * Returns relative path between two sources
214
	 *
215
	 * @param $from
216
	 * @param $to
217
	 * @param string $separator
218
	 * @return string
219
	 */
220 1
	public static function relativePath($from, $to, $separator = DIRECTORY_SEPARATOR) {
221 1
		$from = self::cleanPathname ( $from );
222 1
		$to = self::cleanPathname ( $to );
223
224 1
		$arFrom = \explode ( $separator, \rtrim ( $from, $separator ) );
225 1
		$arTo = \explode ( $separator, \rtrim ( $to, $separator ) );
226 1
		while ( \count ( $arFrom ) && \count ( $arTo ) && ($arFrom [0] == $arTo [0]) ) {
227 1
			\array_shift ( $arFrom );
228 1
			\array_shift ( $arTo );
229
		}
230 1
		return str_pad ( "", \count ( $arTo ) * 3, '..' . $separator ) . \implode ( $separator, $arFrom );
231
	}
232
233 1
	protected static function getLinesByLine($filename, $reverse, $maxLines, $lineCallback) {
234 1
		$result = [ ];
235 1
		$handle = \fopen ( $filename, "r" );
236 1
		if ($handle) {
237 1
			while ( ($line = \fgets ( $handle )) !== false ) {
238 1
				if (\is_callable ( $lineCallback )) {
239 1
					$lineCallback ( $result, $line );
240
				} else {
241
					$result [] = $line;
242
				}
243 1
				if (isset ( $maxLines ) && \sizeof ( $result ) >= $maxLines) {
244 1
					\fclose ( $handle );
245 1
					if (is_array ( $result ) && $reverse) {
246
						$result = \array_reverse ( $result );
247
					}
248 1
					return $result;
249
				}
250
			}
251 1
			\fclose ( $handle );
252
		} else {
253
			// error opening the file.
254
		}
255 1
		if ($reverse) {
256 1
			$result = \array_reverse ( $result );
257
		}
258 1
		return $result;
259
	}
260
}
261