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 |
||
8 | class FileSystemConfig |
||
9 | { |
||
10 | /** @var array */ |
||
11 | protected $fileSystems = []; |
||
12 | |||
13 | /** @var array */ |
||
14 | protected $config = []; |
||
15 | |||
16 | /** |
||
17 | * FileSystemConfig constructor. |
||
18 | * |
||
19 | * @param array $config |
||
20 | */ |
||
21 | 12 | public function __construct(array $config) |
|
30 | |||
31 | /** |
||
32 | * Validate the config array |
||
33 | * |
||
34 | * @param $config |
||
35 | */ |
||
36 | 12 | View Code Duplication | public function validateConfig($config) |
50 | |||
51 | /** |
||
52 | * Get the adaptor |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | 11 | public function getAdaptor() |
|
60 | |||
61 | /** |
||
62 | * Get the cache |
||
63 | * |
||
64 | * @return string |
||
65 | */ |
||
66 | 1 | public function getCache() |
|
70 | |||
71 | /** |
||
72 | * Get the plugins |
||
73 | * |
||
74 | * @return array |
||
75 | */ |
||
76 | 1 | public function getPlugins() |
|
80 | |||
81 | /** |
||
82 | * Is this defined as a file manager? |
||
83 | * |
||
84 | * @return bool |
||
85 | */ |
||
86 | 11 | public function isManager() |
|
94 | |||
95 | /** |
||
96 | * Get the configured File Systems. |
||
97 | * Used for manager configuration |
||
98 | * |
||
99 | * @return FileSystemConfig[] |
||
100 | */ |
||
101 | 1 | public function getFileSystems() |
|
105 | |||
106 | /** |
||
107 | * Build out the file system configs for use |
||
108 | * by the manager. |
||
109 | */ |
||
110 | 3 | protected function buildManagerFileSystemConfigs() |
|
122 | } |
||
123 |
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.