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 JsonStorage |
||
39 | { |
||
40 | /** |
||
41 | * @var Storage |
||
42 | */ |
||
43 | private $storage; |
||
44 | |||
45 | /** |
||
46 | * @var JsonConverterProvider |
||
47 | */ |
||
48 | private $converterProvider; |
||
49 | |||
50 | /** |
||
51 | * @var JsonEncoder |
||
52 | */ |
||
53 | private $jsonEncoder; |
||
54 | |||
55 | /** |
||
56 | * @var JsonDecoder |
||
57 | */ |
||
58 | private $jsonDecoder; |
||
59 | |||
60 | /** |
||
61 | * @var FactoryManager |
||
62 | */ |
||
63 | private $factoryManager; |
||
64 | |||
65 | /** |
||
66 | * Creates a new storage. |
||
67 | * |
||
68 | * @param Storage $storage The file storage. |
||
69 | * @param JsonConverterProvider $converterProvider The provider for the JSON |
||
70 | * converters. |
||
71 | * @param JsonEncoder $jsonEncoder The JSON encoder. |
||
72 | * @param JsonDecoder $jsonDecoder The JSON decoder. |
||
73 | * @param FactoryManager|null $factoryManager The manager used to |
||
74 | * regenerate the factory |
||
75 | * class after saving a file. |
||
76 | */ |
||
77 | 68 | public function __construct( |
|
90 | |||
91 | /** |
||
92 | * Loads a configuration file from a path. |
||
93 | * |
||
94 | * @param string $path The path to the configuration file. |
||
95 | * @param Config|null $baseConfig The configuration that the loaded |
||
96 | * configuration will inherit its values |
||
97 | * from. |
||
98 | * |
||
99 | * @return ConfigFile The loaded configuration file. |
||
100 | * |
||
101 | * @throws FileNotFoundException If the file does not exist. |
||
102 | * @throws ReadException If the file cannot be read. |
||
103 | * @throws InvalidConfigException If the file contains invalid configuration. |
||
104 | */ |
||
105 | 50 | public function loadConfigFile($path, Config $baseConfig = null) |
|
111 | |||
112 | /** |
||
113 | * Saves a configuration file. |
||
114 | * |
||
115 | * The configuration file is saved to the same path that it was read from. |
||
116 | * |
||
117 | * @param ConfigFile $configFile The configuration file to save. |
||
118 | * |
||
119 | * @throws WriteException If the file cannot be written. |
||
120 | * @throws InvalidConfigException If the file contains invalid configuration. |
||
121 | */ |
||
122 | 4 | public function saveConfigFile(ConfigFile $configFile) |
|
130 | |||
131 | /** |
||
132 | * Loads a module file from a file path. |
||
133 | * |
||
134 | * @param string $path The path to the module file. |
||
135 | * |
||
136 | * @return ModuleFile The loaded module file. |
||
137 | * |
||
138 | * @throws FileNotFoundException If the file does not exist. |
||
139 | * @throws ReadException If the file cannot be read. |
||
140 | * @throws InvalidConfigException If the file contains invalid configuration. |
||
141 | */ |
||
142 | 17 | public function loadModuleFile($path) |
|
146 | |||
147 | /** |
||
148 | * Saves a module file. |
||
149 | * |
||
150 | * The module file is saved to the same path that it was read from. |
||
151 | * |
||
152 | * @param ModuleFile $moduleFile The module file to save. |
||
153 | * |
||
154 | * @throws WriteException If the file cannot be written. |
||
155 | * @throws InvalidConfigException If the file contains invalid configuration. |
||
156 | */ |
||
157 | 3 | public function saveModuleFile(ModuleFile $moduleFile) |
|
163 | |||
164 | /** |
||
165 | * Loads a root module file from a file path. |
||
166 | * |
||
167 | * @param string $path The path to the module configuration file. |
||
168 | * @param Config $baseConfig The configuration that the module will inherit |
||
169 | * its configuration values from. |
||
170 | * |
||
171 | * @return RootModuleFile The loaded module file. |
||
172 | * |
||
173 | * @throws FileNotFoundException If the file does not exist. |
||
174 | * @throws ReadException If the file cannot be read. |
||
175 | * @throws InvalidConfigException If the file contains invalid configuration. |
||
176 | */ |
||
177 | 30 | public function loadRootModuleFile($path, Config $baseConfig) |
|
183 | |||
184 | /** |
||
185 | * Saves a root module file. |
||
186 | * |
||
187 | * The module file is saved to the same path that it was read from. |
||
188 | * |
||
189 | * @param RootModuleFile $moduleFile The module file to save. |
||
190 | * |
||
191 | * @throws WriteException If the file cannot be written. |
||
192 | * @throws InvalidConfigException If the file contains invalid configuration. |
||
193 | */ |
||
194 | 4 | public function saveRootModuleFile(RootModuleFile $moduleFile) |
|
204 | |||
205 | 57 | View Code Duplication | private function loadFile($path, $className, array $options = array()) |
221 | |||
222 | 11 | View Code Duplication | private function saveFile($file, $path, array $options = array()) |
240 | |||
241 | 8 | private function encode(stdClass $jsonData, $path) |
|
253 | |||
254 | 56 | private function decode($json, $path) |
|
266 | } |
||
267 |
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.