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 |
||
37 | class ConfigFileStorage |
||
38 | { |
||
39 | /** |
||
40 | * @var Storage |
||
41 | */ |
||
42 | private $storage; |
||
43 | |||
44 | /** |
||
45 | * @var JsonConverter |
||
46 | */ |
||
47 | private $configFileConverter; |
||
48 | |||
49 | /** |
||
50 | * @var JsonEncoder |
||
51 | */ |
||
52 | private $jsonEncoder; |
||
53 | |||
54 | /** |
||
55 | * @var JsonDecoder |
||
56 | */ |
||
57 | private $jsonDecoder; |
||
58 | |||
59 | /** |
||
60 | * @var FactoryManager |
||
61 | */ |
||
62 | private $factoryManager; |
||
63 | |||
64 | /** |
||
65 | * Creates a new configuration file storage. |
||
66 | * |
||
67 | * @param Storage $storage The file storage. |
||
68 | * @param JsonConverter $configFileConverter The JSON converter for |
||
69 | * {@link ConfigFile} |
||
70 | * instances. |
||
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 the |
||
76 | * config file. |
||
77 | */ |
||
78 | 54 | View Code Duplication | public function __construct( |
91 | |||
92 | /** |
||
93 | * Loads a configuration file from a path. |
||
94 | * |
||
95 | * If the path does not exist, an empty configuration file is returned. |
||
96 | * |
||
97 | * @param string $path The path to the configuration file. |
||
98 | * @param Config|null $baseConfig The configuration that the loaded |
||
99 | * configuration will inherit its values |
||
100 | * from. |
||
101 | * |
||
102 | * @return ConfigFile The loaded configuration file. |
||
103 | * |
||
104 | * @throws FileNotFoundException If the file does not exist. |
||
105 | * @throws ReadException If the file cannot be read. |
||
106 | * @throws InvalidConfigException If the file contains invalid configuration. |
||
107 | */ |
||
108 | 50 | View Code Duplication | public function loadConfigFile($path, Config $baseConfig = null) |
126 | |||
127 | /** |
||
128 | * Saves a configuration file. |
||
129 | * |
||
130 | * The configuration file is saved to the same path that it was read from. |
||
131 | * |
||
132 | * @param ConfigFile $configFile The configuration file to save. |
||
133 | * |
||
134 | * @throws WriteException If the file cannot be written. |
||
135 | * @throws InvalidConfigException If the file contains invalid configuration. |
||
136 | */ |
||
137 | 4 | View Code Duplication | public function saveConfigFile(ConfigFile $configFile) |
157 | |||
158 | 3 | View Code Duplication | private function encode(stdClass $jsonData, $path) |
170 | |||
171 | 49 | View Code Duplication | private function decode($json, $path) |
183 | } |
||
184 |
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.