Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FileSystem 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 FileSystem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class FileSystem |
||
16 | { |
||
17 | use \PHPDaemon\Traits\ClassWatchdog; |
||
18 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
19 | use EventLoopContainer; |
||
20 | |||
21 | /** |
||
22 | * @var boolean Is EIO supported? |
||
23 | */ |
||
24 | public static $supported; |
||
25 | |||
26 | /** |
||
27 | * @var Event Main FS event |
||
28 | */ |
||
29 | public static $ev; |
||
30 | |||
31 | /** |
||
32 | * @var resource EIO file descriptor |
||
33 | */ |
||
34 | public static $fd; |
||
35 | |||
36 | /** |
||
37 | * @var array Mode types |
||
38 | */ |
||
39 | public static $modeTypes = [ |
||
40 | 0140000 => 's', |
||
41 | 0120000 => 'l', |
||
42 | 0100000 => 'f', |
||
43 | 0060000 => 'b', |
||
44 | 0040000 => 'd', |
||
45 | 0020000 => 'c', |
||
46 | 0010000 => 'p', |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * @var integer TTL for bad descriptors in seconds |
||
51 | */ |
||
52 | public static $badFDttl = 5; |
||
53 | |||
54 | /** |
||
55 | * @var PHPDaemon\Cache\CappedStorage File descriptor cache |
||
56 | */ |
||
57 | public static $fdCache; |
||
58 | |||
59 | /** |
||
60 | * @var integer Maximum number of open files in cache |
||
61 | */ |
||
62 | public static $fdCacheSize = 128; |
||
63 | |||
64 | /** |
||
65 | * @var string Required EIO version |
||
66 | */ |
||
67 | public static $eioVer = '1.2.1'; |
||
68 | |||
69 | /** |
||
70 | * Initialize FS driver |
||
71 | * @return void |
||
72 | */ |
||
73 | public static function init() |
||
86 | |||
87 | public function __construct() |
||
91 | |||
92 | /** |
||
93 | * Initialize main FS event |
||
94 | * @return void |
||
95 | */ |
||
96 | public static function initEvent() |
||
113 | |||
114 | /** |
||
115 | * Checks if file exists and readable |
||
116 | * @param string $path Path |
||
117 | * @return boolean Exists and readable? |
||
118 | */ |
||
119 | public static function checkFileReadable($path) |
||
123 | |||
124 | /** |
||
125 | * Block until all FS events are completed |
||
126 | * @return void |
||
127 | */ |
||
128 | public static function waitAllEvents() |
||
137 | |||
138 | /** |
||
139 | * Called when config is updated |
||
140 | * @return void |
||
141 | */ |
||
142 | public static function updateConfig() |
||
160 | |||
161 | /** |
||
162 | * Sanitize path |
||
163 | * @param string $path Path |
||
164 | * @return string Sanitized path |
||
165 | */ |
||
166 | public static function sanitizePath($path) |
||
170 | |||
171 | /** |
||
172 | * Prepare value of stat() |
||
173 | * @param mixed $stat Data |
||
174 | * @return array hash |
||
175 | */ |
||
176 | public static function statPrepare($stat) |
||
184 | |||
185 | /** |
||
186 | * Gets stat() information |
||
187 | * @param string $path Path |
||
188 | * @param callable $cb Callback |
||
189 | * @param integer $pri Priority |
||
190 | * @return resource|true |
||
191 | */ |
||
192 | View Code Duplication | public static function stat($path, $cb, $pri = EIO_PRI_DEFAULT) |
|
203 | |||
204 | /** |
||
205 | * Unlink file |
||
206 | * @param string $path Path |
||
207 | * @param callable $cb Callback |
||
208 | * @param integer $pri Priority |
||
209 | * @return resource|boolean |
||
210 | */ |
||
211 | public static function unlink($path, $cb = null, $pri = EIO_PRI_DEFAULT) |
||
223 | |||
224 | /** |
||
225 | * Rename |
||
226 | * @param string $path Path |
||
227 | * @param string $newpath New path |
||
228 | * @param callable $cb Callback |
||
229 | * @param integer $pri Priority |
||
230 | * @return resource|boolean |
||
231 | */ |
||
232 | View Code Duplication | public static function rename($path, $newpath, $cb = null, $pri = EIO_PRI_DEFAULT) |
|
244 | |||
245 | /** |
||
246 | * statvfs() |
||
247 | * @param string $path Path |
||
248 | * @param callable $cb Callback |
||
249 | * @param integer $pri Priority |
||
250 | * @return resource|false |
||
251 | */ |
||
252 | View Code Duplication | public static function statvfs($path, $cb, $pri = EIO_PRI_DEFAULT) |
|
261 | |||
262 | /** |
||
263 | * lstat() |
||
264 | * @param string $path Path |
||
265 | * @param callable $cb Callback |
||
266 | * @param integer $pri Priority |
||
267 | * @return resource|true |
||
268 | */ |
||
269 | View Code Duplication | public static function lstat($path, $cb, $pri = EIO_PRI_DEFAULT) |
|
280 | |||
281 | /** |
||
282 | * realpath() |
||
283 | * @param string $path Path |
||
284 | * @param callable $cb Callback |
||
285 | * @param integer $pri Priority |
||
286 | * @return resource|true |
||
287 | */ |
||
288 | View Code Duplication | public static function realpath($path, $cb, $pri = EIO_PRI_DEFAULT) |
|
297 | |||
298 | /** |
||
299 | * sync() |
||
300 | * @param callable $cb Callback |
||
301 | * @param integer $pri Priority |
||
302 | * @return resource|false |
||
303 | */ |
||
304 | View Code Duplication | public static function sync($cb = null, $pri = EIO_PRI_DEFAULT) |
|
315 | |||
316 | /** |
||
317 | * statfs() |
||
318 | * @param callable $cb Callback |
||
319 | * @param integer $pri Priority |
||
320 | * @return resource|false |
||
321 | */ |
||
322 | View Code Duplication | public static function syncfs($cb = null, $pri = EIO_PRI_DEFAULT) |
|
333 | |||
334 | /** |
||
335 | * touch() |
||
336 | * @param string $path Path |
||
337 | * @param integer $mtime Last modification time |
||
338 | * @param integer $atime Last access time |
||
339 | * @param callable $cb Callback |
||
340 | * @param integer $pri Priority |
||
341 | * @return resource|boolean |
||
342 | */ |
||
343 | View Code Duplication | public static function touch($path, $mtime, $atime = null, $cb = null, $pri = EIO_PRI_DEFAULT) |
|
355 | |||
356 | /** |
||
357 | * Removes empty directory |
||
358 | * @param string $path Path |
||
359 | * @param callable $cb Callback |
||
360 | * @param integer $pri Priority |
||
361 | * @return resource|boolean |
||
362 | */ |
||
363 | public static function rmdir($path, $cb = null, $pri = EIO_PRI_DEFAULT) |
||
375 | |||
376 | /** |
||
377 | * Creates directory |
||
378 | * @param string $path Path |
||
379 | * @param integer $mode Mode (octal) |
||
380 | * @param callable $cb Callback |
||
381 | * @param integer $pri Priority |
||
382 | * @return resource|boolean |
||
383 | */ |
||
384 | View Code Duplication | public static function mkdir($path, $mode, $cb = null, $pri = EIO_PRI_DEFAULT) |
|
396 | |||
397 | /** |
||
398 | * Readdir() |
||
399 | * @param string $path Path |
||
400 | * @param callable $cb = null Callback |
||
401 | * @param integer $flags = null Flags |
||
402 | * @param integer $pri = EIO_PRI_DEFAULT Priority |
||
403 | * @return resource|true |
||
404 | */ |
||
405 | public static function readdir($path, $cb = null, $flags = null, $pri = EIO_PRI_DEFAULT) |
||
417 | |||
418 | /** |
||
419 | * Truncate file |
||
420 | * @param string $path Path |
||
421 | * @param integer $offset Offset |
||
422 | * @param callable $cb Callback |
||
423 | * @param integer $pri Priority |
||
424 | * @return resource|boolean |
||
425 | */ |
||
426 | View Code Duplication | public static function truncate($path, $offset = 0, $cb = null, $pri = EIO_PRI_DEFAULT) |
|
439 | |||
440 | /** |
||
441 | * sendfile() |
||
442 | * @param mixed $outfd File descriptor |
||
443 | * @param string $path Path |
||
444 | * @param callable $cb Callback |
||
445 | * @param callable $startCb Start callback |
||
446 | * @param integer $offset Offset |
||
447 | * @param integer $length Length |
||
448 | * @param integer $pri Priority |
||
449 | * @return true Success |
||
450 | */ |
||
451 | public static function sendfile( |
||
496 | |||
497 | /** |
||
498 | * Changes ownership of file/directory |
||
499 | * @param string $path Path |
||
500 | * @param integer $uid User ID |
||
501 | * @param integer $gid Group ID |
||
502 | * @param callable $cb = null Callback |
||
503 | * @param integer $pri = EIO_PRI_DEFAULT Priority |
||
504 | * @return resource|boolean |
||
505 | */ |
||
506 | View Code Duplication | public static function chown($path, $uid, $gid = -1, $cb = null, $pri = EIO_PRI_DEFAULT) |
|
520 | |||
521 | /** |
||
522 | * Reads whole file |
||
523 | * @param string $path Path |
||
524 | * @param callable $cb Callback (Path, Contents) |
||
525 | * @param integer $pri Priority |
||
526 | * @return resource|true |
||
527 | */ |
||
528 | public static function readfile($path, $cb, $pri = EIO_PRI_DEFAULT) |
||
543 | |||
544 | /** |
||
545 | * Reads file chunk-by-chunk |
||
546 | * @param string $path Path |
||
547 | * @param callable $cb Callback (Path, Success) |
||
548 | * @param callable $chunkcb Chunk callback (Path, Chunk) |
||
549 | * @param integer $pri Priority |
||
550 | * @return resource |
||
551 | */ |
||
552 | public static function readfileChunked($path, $cb, $chunkcb, $pri = EIO_PRI_DEFAULT) |
||
568 | |||
569 | /** |
||
570 | * Returns random temporary file name |
||
571 | * @param string $dir Directory |
||
572 | * @param string $prefix Prefix |
||
573 | * @return string Path |
||
574 | */ |
||
575 | public static function genRndTempnam($dir = null, $prefix = 'php') |
||
593 | |||
594 | /** |
||
595 | * Returns random temporary file name |
||
596 | * @param string $dir Directory |
||
597 | * @param string $prefix Prefix |
||
598 | * @return string Path |
||
599 | */ |
||
600 | public static function genRndTempnamPrefix($dir, $prefix) |
||
607 | |||
608 | /** |
||
609 | * Generates closure tempnam handler |
||
610 | * @param $dir |
||
611 | * @param $prefix |
||
612 | * @param $cb |
||
613 | * @param $tries |
||
614 | */ |
||
615 | protected static function tempnamHandler($dir, $prefix, $cb, &$tries) |
||
631 | |||
632 | /** |
||
633 | * Obtain exclusive temporary file |
||
634 | * @param string $dir Directory |
||
635 | * @param string $prefix Prefix |
||
636 | * @param callable $cb Callback (File) |
||
637 | * @return resource |
||
638 | */ |
||
639 | public static function tempnam($dir, $prefix, $cb) |
||
648 | |||
649 | /** |
||
650 | * Open file |
||
651 | * @param string $path Path |
||
652 | * @param string $flags Flags |
||
653 | * @param callable $cb Callback (File) |
||
654 | * @param integer $mode Mode (see EIO_S_I* constants) |
||
655 | * @param integer $pri Priority |
||
656 | * @return resource |
||
657 | */ |
||
658 | public static function open($path, $flags, $cb, $mode = null, $pri = EIO_PRI_DEFAULT) |
||
718 | } |
||
719 | |||
723 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..