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 FlyBase 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 FlyBase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | abstract class FlyBase implements Iface, DirIface, MetaIface |
||
21 | { |
||
22 | private $config; |
||
23 | private $tempdir; |
||
24 | |||
25 | |||
26 | /** |
||
27 | * Initializes the object |
||
28 | * |
||
29 | * @param array $config Configuration options |
||
30 | */ |
||
31 | public function __construct( array $config ) |
||
46 | |||
47 | |||
48 | /** |
||
49 | * Tests if the given path is a directory |
||
50 | * |
||
51 | * @param string $path Path to the file or directory |
||
52 | * @return boolean True if directory, false if not |
||
53 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
54 | */ |
||
55 | public function isdir( $path ) |
||
65 | |||
66 | |||
67 | /** |
||
68 | * Creates a new directory for the given path |
||
69 | * |
||
70 | * @param string $path Path to the directory |
||
71 | * @return void |
||
72 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
73 | */ |
||
74 | public function mkdir( $path ) |
||
80 | |||
81 | |||
82 | /** |
||
83 | * Deletes the directory for the given path |
||
84 | * |
||
85 | * @param string $path Path to the directory |
||
86 | * @return void |
||
87 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
88 | */ |
||
89 | public function rmdir( $path ) |
||
95 | |||
96 | |||
97 | /** |
||
98 | * Returns an iterator over the entries in the given path |
||
99 | * |
||
100 | * {@inheritDoc} |
||
101 | * |
||
102 | * @param string $path Path to the filesystem or directory |
||
103 | * @return \Iterator|array Iterator over the entries or array with entries |
||
104 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
105 | */ |
||
106 | public function scan( $path = null ) |
||
116 | |||
117 | |||
118 | /** |
||
119 | * Returns the file size |
||
120 | * |
||
121 | * @param string $path Path to the file |
||
122 | * @return integer Size in bytes |
||
123 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
124 | */ |
||
125 | View Code Duplication | public function size( $path ) |
|
139 | |||
140 | |||
141 | /** |
||
142 | * Returns the Unix time stamp for the file |
||
143 | * |
||
144 | * @param string $path Path to the file |
||
145 | * @return integer Unix time stamp in seconds |
||
146 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
147 | */ |
||
148 | View Code Duplication | public function time( $path ) |
|
162 | |||
163 | |||
164 | /** |
||
165 | * Deletes the file for the given path |
||
166 | * |
||
167 | * @param string $path Path to the file |
||
168 | * @return void |
||
169 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
170 | */ |
||
171 | public function rm( $path ) |
||
179 | |||
180 | |||
181 | /** |
||
182 | * Tests if a file exists at the given path |
||
183 | * |
||
184 | * @param string $path Path to the file |
||
185 | * @return boolean True if it exists, false if not |
||
186 | */ |
||
187 | public function has( $path ) |
||
191 | |||
192 | |||
193 | /** |
||
194 | * Returns the content of the file |
||
195 | * |
||
196 | * {@inheritDoc} |
||
197 | * |
||
198 | * @param string $path Path to the file |
||
199 | * @return string File content |
||
200 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
201 | */ |
||
202 | View Code Duplication | public function read( $path ) |
|
216 | |||
217 | |||
218 | /** |
||
219 | * Reads the content of the remote file and writes it to a local one |
||
220 | * |
||
221 | * @param string $path Path to the remote file |
||
222 | * @return string Path of the local file |
||
223 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
224 | */ |
||
225 | public function readf( $path ) |
||
246 | |||
247 | |||
248 | /** |
||
249 | * Returns the stream descriptor for the file |
||
250 | * |
||
251 | * {@inheritDoc} |
||
252 | * |
||
253 | * @param string $path Path to the file |
||
254 | * @return resource File stream descriptor |
||
255 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
256 | */ |
||
257 | View Code Duplication | public function reads( $path ) |
|
271 | |||
272 | |||
273 | /** |
||
274 | * Writes the given content to the file |
||
275 | * |
||
276 | * {@inheritDoc} |
||
277 | * |
||
278 | * @param string $path Path to the file |
||
279 | * @param string $content New file content |
||
280 | * @return void |
||
281 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
282 | */ |
||
283 | View Code Duplication | public function write( $path, $content ) |
|
295 | |||
296 | |||
297 | /** |
||
298 | * Writes the content of the local file to the remote path |
||
299 | * |
||
300 | * {@inheritDoc} |
||
301 | * |
||
302 | * @param string $path Path to the remote file |
||
303 | * @param string $file Path to the local file |
||
304 | * @return void |
||
305 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
306 | */ |
||
307 | public function writef( $path, $local ) |
||
319 | |||
320 | |||
321 | /** |
||
322 | * Write the content of the stream descriptor into the remote file |
||
323 | * |
||
324 | * {@inheritDoc} |
||
325 | * |
||
326 | * @param string $path Path to the file |
||
327 | * @param resource $stream File stream descriptor |
||
328 | * @return void |
||
329 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
330 | */ |
||
331 | View Code Duplication | public function writes( $path, $stream ) |
|
343 | |||
344 | |||
345 | /** |
||
346 | * Renames a file, moves it to a new location or both at once |
||
347 | * |
||
348 | * @param string $from Path to the original file |
||
349 | * @param string $to Path to the new file |
||
350 | * @return void |
||
351 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
352 | */ |
||
353 | View Code Duplication | public function move( $from, $to ) |
|
365 | |||
366 | |||
367 | /** |
||
368 | * Copies a file to a new location |
||
369 | * |
||
370 | * @param string $from Path to the original file |
||
371 | * @param string $to Path to the new file |
||
372 | * @return void |
||
373 | * @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
||
374 | */ |
||
375 | View Code Duplication | public function copy( $from, $to ) |
|
387 | |||
388 | |||
389 | /** |
||
390 | * Returns the flysystem adapter |
||
391 | * |
||
392 | * @return \League\Flysystem\AdapterInterface Flysystem adapter |
||
393 | */ |
||
394 | protected function getAdapter() |
||
398 | |||
399 | |||
400 | /** |
||
401 | * Returns the configuration options |
||
402 | * |
||
403 | * @return array Configuration options |
||
404 | */ |
||
405 | protected function getConfig() |
||
409 | |||
410 | |||
411 | /** |
||
412 | * Returns the file system provider |
||
413 | * |
||
414 | * @return \League\Flysystem\FilesystemInterface File system provider |
||
415 | */ |
||
416 | protected abstract function getProvider(); |
||
417 | } |
||
418 |
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.