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 |
||
38 | class ModuleFileStorage |
||
39 | { |
||
40 | /** |
||
41 | * @var Storage |
||
42 | */ |
||
43 | private $storage; |
||
44 | |||
45 | /** |
||
46 | * @var JsonConverter |
||
47 | */ |
||
48 | private $moduleFileConverter; |
||
49 | |||
50 | /** |
||
51 | * @var JsonConverter |
||
52 | */ |
||
53 | private $rootModuleFileConverter; |
||
54 | |||
55 | /** |
||
56 | * @var JsonEncoder |
||
57 | */ |
||
58 | private $jsonEncoder; |
||
59 | |||
60 | /** |
||
61 | * @var JsonDecoder |
||
62 | */ |
||
63 | private $jsonDecoder; |
||
64 | |||
65 | /** |
||
66 | * @var FactoryManager |
||
67 | */ |
||
68 | private $factoryManager; |
||
69 | |||
70 | /** |
||
71 | * Creates a new storage. |
||
72 | * |
||
73 | * @param Storage $storage The file storage. |
||
74 | * @param JsonConverter $moduleFileConverter The JSON converter for |
||
75 | * {@link ModuleFile} |
||
76 | * instances. |
||
77 | * @param JsonConverter $rootModuleFileConverter The JSON converter |
||
78 | * for {@link RootModuleFile} |
||
79 | * instances. |
||
80 | * @param JsonEncoder $jsonEncoder The JSON encoder. |
||
81 | * @param JsonDecoder $jsonDecoder The JSON decoder. |
||
82 | * @param FactoryManager|null $factoryManager The manager used to |
||
83 | * regenerate the factory |
||
84 | * class after saving |
||
85 | * the root module file. |
||
86 | */ |
||
87 | 40 | View Code Duplication | public function __construct( |
102 | |||
103 | /** |
||
104 | * Loads a module file from a file path. |
||
105 | * |
||
106 | * If the file does not exist, an empty configuration is returned. |
||
107 | * |
||
108 | * Loaded module files must have a module name set. If none is set, an |
||
109 | * exception is thrown. |
||
110 | * |
||
111 | * @param string $path The path to the module file. |
||
112 | * |
||
113 | * @return ModuleFile The loaded module file. |
||
114 | * |
||
115 | * @throws FileNotFoundException If the file does not exist. |
||
116 | * @throws ReadException If the file cannot be read. |
||
117 | * @throws InvalidConfigException If the file contains invalid configuration. |
||
118 | */ |
||
119 | 17 | View Code Duplication | public function loadModuleFile($path) |
136 | |||
137 | /** |
||
138 | * Saves a module file. |
||
139 | * |
||
140 | * The module file is saved to the same path that it was read from. |
||
141 | * |
||
142 | * @param ModuleFile $moduleFile The module file to save. |
||
143 | * |
||
144 | * @throws WriteException If the file cannot be written. |
||
145 | * @throws InvalidConfigException If the file contains invalid configuration. |
||
146 | */ |
||
147 | 3 | public function saveModuleFile(ModuleFile $moduleFile) |
|
165 | |||
166 | /** |
||
167 | * Loads a root module file from a file path. |
||
168 | * |
||
169 | * If the file does not exist, an empty configuration is returned. |
||
170 | * |
||
171 | * @param string $path The path to the module configuration file. |
||
172 | * @param Config $baseConfig The configuration that the module will inherit |
||
173 | * its configuration values from. |
||
174 | * |
||
175 | * @return RootModuleFile The loaded module file. |
||
176 | * |
||
177 | * @throws FileNotFoundException If the file does not exist. |
||
178 | * @throws ReadException If the file cannot be read. |
||
179 | * @throws InvalidConfigException If the file contains invalid configuration. |
||
180 | */ |
||
181 | 30 | View Code Duplication | public function loadRootModuleFile($path, Config $baseConfig) |
199 | |||
200 | /** |
||
201 | * Saves a root module file. |
||
202 | * |
||
203 | * The module file is saved to the same path that it was read from. |
||
204 | * |
||
205 | * @param RootModuleFile $moduleFile The module file to save. |
||
206 | * |
||
207 | * @throws WriteException If the file cannot be written. |
||
208 | * @throws InvalidConfigException If the file contains invalid configuration. |
||
209 | */ |
||
210 | 4 | View Code Duplication | public function saveRootModuleFile(RootModuleFile $moduleFile) |
232 | |||
233 | 5 | View Code Duplication | private function encode(stdClass $jsonData, $path) |
245 | |||
246 | 32 | View Code Duplication | private function decode($json, $path) |
258 | } |
||
259 |
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.