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 |
||
28 | class FileSystem |
||
29 | { |
||
30 | /** @type int NONE no flag */ |
||
31 | const NONE = 0; |
||
32 | /** @type int ENCRYPTION use encryption */ |
||
33 | const ENCRYPTION = 1; |
||
34 | /** @type int USE_JSON use json for serialization */ |
||
35 | const USE_JSON = 2; |
||
36 | /** @type int APPEND append to file */ |
||
37 | const APPEND = 4; |
||
38 | /** @type int LOCK_NONBLOCKING lock file with nonblocking lock */ |
||
39 | const LOCK_NONBLOCKING = 8; |
||
40 | /** @type int LOCK_SHARE lock file with share lock */ |
||
41 | const LOCK_SHARE = 16; |
||
42 | /** @type int LOCK_EXCLUSIVE lock file with exclusive lock */ |
||
43 | const LOCK_EXCLUSIVE = 32; |
||
44 | |||
45 | |||
46 | /** |
||
47 | * Default variables for io functionality |
||
48 | * |
||
49 | * @type array $defaults array of default variables |
||
50 | */ |
||
51 | public static $defaults = [ |
||
52 | "pathSeparator" => DIRECTORY_SEPARATOR, |
||
53 | "fileReadBuffer" => 4096, |
||
54 | "keyphase" => "scabbia_default" |
||
55 | ]; |
||
56 | |||
57 | |||
58 | /** |
||
59 | * Constructor to prevent new instances of FileSystem class |
||
60 | * |
||
61 | * @return FileSystem |
||
|
|||
62 | */ |
||
63 | final private function __construct() |
||
66 | |||
67 | /** |
||
68 | * Clone method to prevent duplication of FileSystem class |
||
69 | * |
||
70 | * @return FileSystem |
||
71 | */ |
||
72 | final private function __clone() |
||
75 | |||
76 | /** |
||
77 | * Unserialization method to prevent restoration of FileSystem class |
||
78 | * |
||
79 | * @return FileSystem |
||
80 | */ |
||
81 | final private function __wakeup() |
||
84 | |||
85 | /** |
||
86 | * Sets the default variables |
||
87 | * |
||
88 | * @param array $uDefaults variables to be set |
||
89 | * |
||
90 | * @return void |
||
91 | */ |
||
92 | public static function setDefaults($uDefaults) |
||
96 | |||
97 | /** |
||
98 | * Encrypts the plaintext with the given key |
||
99 | * |
||
100 | * @param string $uString the plaintext |
||
101 | * @param string $uKey the key |
||
102 | * |
||
103 | * @return string ciphertext |
||
104 | */ |
||
105 | View Code Duplication | public static function encrypt($uString, $uKey) |
|
117 | |||
118 | /** |
||
119 | * Decrypts the ciphertext with the given key |
||
120 | * |
||
121 | * @param string $uString the ciphertext |
||
122 | * @param string $uKey the key |
||
123 | * |
||
124 | * @return string plaintext |
||
125 | */ |
||
126 | View Code Duplication | public static function decrypt($uString, $uKey) |
|
138 | |||
139 | /** |
||
140 | * Reads from a file |
||
141 | * |
||
142 | * @param string $uPath the file path |
||
143 | * @param int $uFlags flags |
||
144 | * |
||
145 | * @return bool|string the file content |
||
146 | */ |
||
147 | public static function read($uPath, $uFlags = self::LOCK_SHARE) |
||
185 | |||
186 | /** |
||
187 | * Writes to a file |
||
188 | * |
||
189 | * @param string $uPath the file path |
||
190 | * @param string $uContent the file content |
||
191 | * @param int $uFlags flags |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | public static function write($uPath, $uContent, $uFlags = self::LOCK_EXCLUSIVE) |
||
239 | |||
240 | /** |
||
241 | * Reads from a serialized file |
||
242 | * |
||
243 | * @param string $uPath the file path |
||
244 | * @param int $uFlags flags |
||
245 | * |
||
246 | * @return bool|mixed the unserialized object |
||
247 | */ |
||
248 | public static function readSerialize($uPath, $uFlags = self::LOCK_SHARE) |
||
263 | |||
264 | /** |
||
265 | * Serializes an object into a file |
||
266 | * |
||
267 | * @param string $uPath the file path |
||
268 | * @param string $uContent the file content |
||
269 | * @param int $uFlags flags |
||
270 | * |
||
271 | * @return bool |
||
272 | */ |
||
273 | public static function writeSerialize($uPath, $uContent, $uFlags = self::LOCK_EXCLUSIVE) |
||
281 | |||
282 | /** |
||
283 | * Exports an object into a php file |
||
284 | * |
||
285 | * @param string $uPath the file path |
||
286 | * @param string $uContent the file content |
||
287 | * |
||
288 | * @return bool |
||
289 | */ |
||
290 | public static function writePhpFile($uPath, $uContent) |
||
297 | |||
298 | /** |
||
299 | * Checks the path contains invalid chars or not |
||
300 | * |
||
301 | * @param string $uPath the path |
||
302 | * |
||
303 | * @return bool true if the path contains invalid chars |
||
304 | */ |
||
305 | public static function checkInvalidPathChars($uPath) |
||
322 | |||
323 | /** |
||
324 | * Checks the path is path rooted or not |
||
325 | * |
||
326 | * @param string $uPath the path |
||
327 | * |
||
328 | * @throws UnexpectedValueException if path contains invalid chars |
||
329 | * @return bool true if the path is rooted |
||
330 | */ |
||
331 | public static function isPathRooted($uPath) |
||
352 | |||
353 | /** |
||
354 | * Checks the path is path relative or not |
||
355 | * |
||
356 | * @param string $uPath the path |
||
357 | * |
||
358 | * @throws UnexpectedValueException if path contains invalid chars |
||
359 | * @return bool true if the path is relative |
||
360 | */ |
||
361 | public static function isPathRelative($uPath) |
||
386 | |||
387 | /** |
||
388 | * Combines given paths into a single path string |
||
389 | * |
||
390 | * @param array $uPaths paths |
||
391 | * |
||
392 | * @return null|string combined path |
||
393 | */ |
||
394 | public static function combinePaths(...$uPaths) |
||
421 | |||
422 | /** |
||
423 | * Gets the number of lines of given file |
||
424 | * |
||
425 | * @param string $uPath the path |
||
426 | * |
||
427 | * @return int|bool line count |
||
428 | */ |
||
429 | public static function getFileLineCount($uPath) |
||
445 | |||
446 | /** |
||
447 | * Determines the file is if readable and not expired |
||
448 | * |
||
449 | * @param string $uPath the relative path |
||
450 | * @param array $uOptions options |
||
451 | * |
||
452 | * @return bool the result |
||
453 | */ |
||
454 | public static function isReadable($uPath, array $uOptions = []) |
||
471 | |||
472 | /** |
||
473 | * Reads the contents from cache file as long as it is not expired |
||
474 | * If the file is expired, invokes callback method and caches output |
||
475 | * |
||
476 | * @param string $uPath the relative path |
||
477 | * @param mixed $uDefaultValue the default value |
||
478 | * @param array $uOptions options |
||
479 | * |
||
480 | * @return mixed the result |
||
481 | */ |
||
482 | public static function readFromCacheFile($uPath, $uDefaultValue, array $uOptions = []) |
||
495 | |||
496 | /** |
||
497 | * Gets the list of files matching the given pattern |
||
498 | * |
||
499 | * @param string $uPath path to be searched |
||
500 | * @param string|null $uPattern pattern of files will be in the list |
||
501 | * @param bool $uRecursive recursive search |
||
502 | * @param bool $uBasenames use basenames only |
||
503 | * |
||
504 | * @return array the list of files |
||
505 | */ |
||
506 | public static function getFiles($uPath, $uPattern = null, $uRecursive = true, $uBasenames = false) |
||
540 | |||
541 | /** |
||
542 | * Garbage collects the given path |
||
543 | * |
||
544 | * @param string $uPath path |
||
545 | * @param array $uOptions options |
||
546 | * |
||
547 | * @return void |
||
548 | */ |
||
549 | public static function garbageCollect($uPath, array $uOptions = []) |
||
578 | |||
579 | /** |
||
580 | * Apply a function/method to every file matching the given pattern |
||
581 | * |
||
582 | * @param string $uPath path to be searched |
||
583 | * @param string|null $uPattern pattern of files will be in the list |
||
584 | * @param bool $uRecursive recursive search |
||
585 | * @param callable $uCallback callback function/method |
||
586 | * @param mixed $uStateObject parameters will be passed to function |
||
587 | * |
||
588 | * @return void |
||
589 | */ |
||
590 | public static function getFilesWalk($uPath, $uPattern, $uRecursive, /* callable */ $uCallback, $uStateObject = null) |
||
612 | |||
613 | /** |
||
614 | * Returns the mimetype of given extension |
||
615 | * |
||
616 | * @param string $uExtension extension of the file |
||
617 | * @param string $uDefault default mimetype if nothing found |
||
618 | * |
||
619 | * @return string |
||
620 | */ |
||
621 | public static function getMimetype($uExtension, $uDefault = "application/octet-stream") |
||
782 | } |
||
783 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.