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 |
||
22 | class File |
||
23 | { |
||
24 | /** |
||
25 | * @author Luke Watts <[email protected]> |
||
26 | * |
||
27 | * @since 1.0.0 |
||
28 | * |
||
29 | * @var \DirectoryIterator |
||
30 | */ |
||
31 | private $iterator; |
||
32 | |||
33 | /** |
||
34 | * @author Luke Watts <[email protected]> |
||
35 | * |
||
36 | * @since 1.0.0 |
||
37 | * |
||
38 | * @var |
||
39 | */ |
||
40 | private $pattern; |
||
41 | |||
42 | /** |
||
43 | * @author Luke Watts <[email protected]> |
||
44 | * |
||
45 | * @since 1.0.0 |
||
46 | * |
||
47 | * @var |
||
48 | */ |
||
49 | private $limit = -1; |
||
50 | |||
51 | /** |
||
52 | * @author Luke Watts <[email protected]> |
||
53 | * |
||
54 | * @since 2.0.0 |
||
55 | * |
||
56 | * @var |
||
57 | */ |
||
58 | private $dir; |
||
59 | |||
60 | /** |
||
61 | * @author Luke Watts <[email protected]> |
||
62 | * |
||
63 | * @since 1.0.0 |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | private $regex_delimiters = ['/', '#', '@', '~']; |
||
68 | |||
69 | /** |
||
70 | * @author Luke Watts <[email protected]> |
||
71 | * |
||
72 | * @since 1.0.0 |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | private $file_list = []; |
||
77 | |||
78 | /** |
||
79 | * Set the pattern to search for. |
||
80 | * |
||
81 | * Can be a regex pattern with the delimiters /, #, @ or ~ |
||
82 | * |
||
83 | * Can also be a plain file name to search for only that file |
||
84 | * |
||
85 | * @author Luke Watts <[email protected]> |
||
86 | * |
||
87 | * @since 1.0.0 |
||
88 | * |
||
89 | * @param $pattern |
||
90 | * |
||
91 | * @return File |
||
92 | */ |
||
93 | 13 | public function find($pattern) |
|
99 | |||
100 | /** |
||
101 | * Alias of the upOne() method |
||
102 | * |
||
103 | * @author Luke Watts <[email protected]> |
||
104 | * |
||
105 | * @since 2.0.0 |
||
106 | * |
||
107 | * @return File |
||
108 | */ |
||
109 | 1 | public function parent() |
|
113 | |||
114 | /** |
||
115 | * Alias of the up() method |
||
116 | * |
||
117 | * @author Luke Watts <[email protected]> |
||
118 | * |
||
119 | * @since 2.0.0 |
||
120 | * |
||
121 | * @return File |
||
122 | */ |
||
123 | 1 | public function parents() |
|
127 | |||
128 | /** |
||
129 | * Sets the directory to start search in. |
||
130 | * |
||
131 | * @author Luke Watts <[email protected]> |
||
132 | * |
||
133 | * @since 2.0.0 |
||
134 | * |
||
135 | * @param $dir |
||
136 | * |
||
137 | * @return File |
||
138 | */ |
||
139 | 11 | public function in($dir) |
|
147 | |||
148 | /** |
||
149 | * Search the parent directory. |
||
150 | * |
||
151 | * @author Luke Watts <[email protected]> |
||
152 | * |
||
153 | * @return File |
||
154 | */ |
||
155 | 2 | View Code Duplication | public function upOne() |
169 | |||
170 | /** |
||
171 | * Recursively searches parent directories. |
||
172 | * |
||
173 | * @author Luke Watts <[email protected]> |
||
174 | * |
||
175 | * @return File |
||
176 | */ |
||
177 | 2 | View Code Duplication | public function up() |
192 | |||
193 | /** |
||
194 | * Return specified amount of files |
||
195 | * |
||
196 | * @author Luke Watts <[email protected]> |
||
197 | * |
||
198 | * @param int $limit |
||
199 | * |
||
200 | * @return array|bool|mixed |
||
201 | */ |
||
202 | 10 | public function get($limit = -1) |
|
221 | |||
222 | /** |
||
223 | * Checks existence of file. |
||
224 | * |
||
225 | * @author Luke Watts <[email protected]> |
||
226 | * |
||
227 | * @return bool |
||
228 | */ |
||
229 | 4 | public function has() |
|
233 | |||
234 | /** |
||
235 | * Check is the Regex a valid pattern |
||
236 | * |
||
237 | * @author Luke Watts <[email protected]> |
||
238 | * |
||
239 | * @since 2.1.3 |
||
240 | * |
||
241 | * @param $pattern |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | 12 | public function isValidPattern($pattern) |
|
264 | |||
265 | /** |
||
266 | * Set the file list using by matching pattern |
||
267 | * |
||
268 | * @author Luke Watts <[email protected]> |
||
269 | * |
||
270 | * @since 2.1.4 |
||
271 | * |
||
272 | * @param \DirectoryIterator $iterator |
||
273 | * @param $pattern |
||
274 | */ |
||
275 | 11 | public function setFileListUsingPattern(\DirectoryIterator $iterator, $pattern) |
|
293 | |||
294 | /** |
||
295 | * Make the search |
||
296 | * |
||
297 | * @author Luke Watts <[email protected]> |
||
298 | */ |
||
299 | 11 | public function make() |
|
311 | |||
312 | /** |
||
313 | * Returns the current pattern to search for |
||
314 | * |
||
315 | * @author Luke Watts <[email protected]> |
||
316 | * |
||
317 | * @since 1.0.0 |
||
318 | * |
||
319 | * @return mixed |
||
320 | */ |
||
321 | 6 | public function getPattern() |
|
325 | |||
326 | /** |
||
327 | * Get the current directory. |
||
328 | * |
||
329 | * @author Luke Watts <[email protected]> |
||
330 | * |
||
331 | * @since 2.0.0 |
||
332 | * |
||
333 | * @return mixed |
||
334 | */ |
||
335 | 11 | public function getDir() |
|
339 | |||
340 | /** |
||
341 | * Returns file list array |
||
342 | * |
||
343 | * @author Luke Watts <[email protected]> |
||
344 | * |
||
345 | * @since 1.0.0 |
||
346 | * |
||
347 | * @return array |
||
348 | */ |
||
349 | 8 | public function getFileList() |
|
353 | } |
||
354 |
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.