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 |
||
7 | final class Folder |
||
8 | { |
||
9 | /** |
||
10 | * Ensure a folder exists by creating it if missing and throw an exception on failure. |
||
11 | * |
||
12 | * @param string $path |
||
13 | * @param int $mode Optional, defaults to 0777. |
||
14 | * |
||
15 | * @throws \RuntimeException |
||
16 | */ |
||
17 | View Code Duplication | public static function ensure($path, $mode = 0777) |
|
31 | |||
32 | /** |
||
33 | * Delete a directory and all of its contents recursively. |
||
34 | * |
||
35 | * @param string $path Path of folder to delete |
||
36 | * |
||
37 | * @throws \RuntimeException Thrown when something could not be deleted. |
||
38 | */ |
||
39 | View Code Duplication | public static function delete($path) |
|
53 | |||
54 | /** |
||
55 | * deleteContents of a folder recursively, but not the folder itself. |
||
56 | * |
||
57 | * On Windows systems this will try to remove the read-only attribute if needed. |
||
58 | * |
||
59 | * @param string $path Path of folder whose contents will be deleted |
||
60 | * |
||
61 | * @throws \RuntimeException Thrown when something could not be deleted. |
||
62 | */ |
||
63 | public static function deleteContents($path) |
||
99 | } |
||
100 |
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.