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 PackageFileStorage |
||
39 | { |
||
40 | /** |
||
41 | * @var Storage |
||
42 | */ |
||
43 | private $storage; |
||
44 | |||
45 | /** |
||
46 | * @var JsonConverter |
||
47 | */ |
||
48 | private $packageFileConverter; |
||
49 | |||
50 | /** |
||
51 | * @var JsonConverter |
||
52 | */ |
||
53 | private $rootPackageFileConverter; |
||
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 $packageFileConverter The JSON converter for |
||
75 | * {@link PackageFile} |
||
76 | * instances. |
||
77 | * @param JsonConverter $rootPackageFileConverter The JSON converter |
||
78 | * for {@link RootPackageFile} |
||
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 package file. |
||
86 | */ |
||
87 | 40 | View Code Duplication | public function __construct( |
102 | |||
103 | /** |
||
104 | * Loads a package file from a file path. |
||
105 | * |
||
106 | * If the file does not exist, an empty configuration is returned. |
||
107 | * |
||
108 | * Loaded package files must have a package name set. If none is set, an |
||
109 | * exception is thrown. |
||
110 | * |
||
111 | * @param string $path The path to the package file. |
||
112 | * |
||
113 | * @return PackageFile The loaded package 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 loadPackageFile($path) |
136 | |||
137 | /** |
||
138 | * Saves a package file. |
||
139 | * |
||
140 | * The package file is saved to the same path that it was read from. |
||
141 | * |
||
142 | * @param PackageFile $packageFile The package 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 savePackageFile(PackageFile $packageFile) |
|
165 | |||
166 | /** |
||
167 | * Loads a root package 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 package configuration file. |
||
172 | * @param Config $baseConfig The configuration that the package will inherit |
||
173 | * its configuration values from. |
||
174 | * |
||
175 | * @return RootPackageFile The loaded package 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 loadRootPackageFile($path, Config $baseConfig) |
199 | |||
200 | /** |
||
201 | * Saves a root package file. |
||
202 | * |
||
203 | * The package file is saved to the same path that it was read from. |
||
204 | * |
||
205 | * @param RootPackageFile $packageFile The package 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 saveRootPackageFile(RootPackageFile $packageFile) |
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.