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:
1 | <?php |
||
12 | class File |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * Read file content from local storage |
||
17 | * @param $path |
||
18 | * @return bool|string |
||
19 | */ |
||
20 | public static function read($path) |
||
21 | { |
||
22 | $path = Normalize::diskFullPath($path); |
||
23 | |||
24 | if (!self::exist($path)) { |
||
25 | return false; |
||
26 | } |
||
27 | |||
28 | return @file_get_contents($path); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Check if $path is exist and readable in filesystem |
||
33 | * @param string $path |
||
34 | * @return bool |
||
35 | */ |
||
36 | public static function exist($path) |
||
37 | { |
||
38 | $path = Normalize::diskFullPath($path); |
||
39 | return (file_exists($path) && is_readable($path) && is_file($path)); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Alias for exist method |
||
44 | * @param string $path |
||
45 | * @return bool |
||
46 | */ |
||
47 | public static function readable($path) { |
||
50 | |||
51 | /** |
||
52 | * Check is file writable |
||
53 | * @param string $path |
||
54 | * @return bool |
||
55 | */ |
||
56 | View Code Duplication | public static function writable($path) |
|
66 | |||
67 | /** |
||
68 | * Check is file executable |
||
69 | * @param string $path |
||
70 | * @return bool |
||
71 | */ |
||
72 | public static function executable($path) |
||
82 | |||
83 | /** |
||
84 | * @param string $path |
||
85 | * @param null|string $content |
||
86 | * @param null|int $flags |
||
87 | * @return int |
||
88 | */ |
||
89 | public static function write($path, $content = null, $flags = null) |
||
102 | |||
103 | /** |
||
104 | * Remove file |
||
105 | * @param string $path |
||
106 | * @return bool |
||
107 | */ |
||
108 | public static function remove($path) |
||
117 | |||
118 | |||
119 | /** |
||
120 | * Alternative of functions include, require, include_once and etc in 1 function |
||
121 | * @param string $path |
||
122 | * @param bool|false $return |
||
123 | * @param bool|false $once |
||
124 | * @return bool|mixed |
||
125 | */ |
||
126 | public static function inc($path, $return = false, $once = false) |
||
140 | |||
141 | /** |
||
142 | * Get file make time in unix timestamp |
||
143 | * @param string $path |
||
144 | * @return int |
||
145 | */ |
||
146 | public static function mTime($path) |
||
155 | |||
156 | /** |
||
157 | * Recursive scan directory, based on $path and allowed extensions $ext or without it |
||
158 | * @param string $path |
||
159 | * @param array $ext |
||
160 | * @param bool $returnRelative |
||
161 | * @param $files |
||
162 | * @return array |
||
163 | */ |
||
164 | public static function listFiles($path, array $ext = null, $returnRelative = false, &$files = []) |
||
188 | |||
189 | /** |
||
190 | * Get file size in bytes |
||
191 | * @param string $path |
||
192 | * @return int |
||
193 | */ |
||
194 | public static function size($path) |
||
204 | |||
205 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.