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 |
||
17 | class Config |
||
18 | { |
||
19 | /** |
||
20 | * Config. |
||
21 | * |
||
22 | * @var Data |
||
23 | */ |
||
24 | protected $data; |
||
25 | /** |
||
26 | * Source directory. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $sourceDir; |
||
31 | /** |
||
32 | * Destination directory. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $destinationDir; |
||
37 | |||
38 | /** |
||
39 | * Config constructor. |
||
40 | * |
||
41 | * @param Config|array|null $config |
||
42 | */ |
||
43 | public function __construct($config = null) |
||
78 | |||
79 | /** |
||
80 | * Import array config to current config. |
||
81 | * |
||
82 | * @param array $config |
||
83 | */ |
||
84 | public function import($config) |
||
94 | |||
95 | /** |
||
96 | * Set config data. |
||
97 | * |
||
98 | * @param Data $data |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | protected function setFromData(Data $data) |
||
110 | |||
111 | /** |
||
112 | * Get config data. |
||
113 | * |
||
114 | * @return Data |
||
115 | */ |
||
116 | public function getAll() |
||
120 | |||
121 | /** |
||
122 | * Get data as array. |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | public function getAllAsArray() |
||
130 | |||
131 | /** |
||
132 | * Return a config value. |
||
133 | * |
||
134 | * @param string $key |
||
135 | * @param string $default |
||
136 | * |
||
137 | * @return array|mixed|null |
||
138 | */ |
||
139 | public function get($key, $default = '') |
||
143 | |||
144 | /** |
||
145 | * Set source directory. |
||
146 | * |
||
147 | * @param null $sourceDir |
||
148 | * |
||
149 | * @throws Exception |
||
150 | * |
||
151 | * @return $this |
||
152 | */ |
||
153 | View Code Duplication | public function setSourceDir($sourceDir = null) |
|
165 | |||
166 | /** |
||
167 | * Get source directory. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | public function getSourceDir() |
||
175 | |||
176 | /** |
||
177 | * Set destination directory. |
||
178 | * |
||
179 | * @param null $destinationDir |
||
180 | * |
||
181 | * @throws Exception |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | View Code Duplication | public function setDestinationDir($destinationDir = null) |
|
197 | |||
198 | /** |
||
199 | * Get destination directory. |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | public function getDestinationDir() |
||
207 | |||
208 | /** |
||
209 | * Path helpers. |
||
210 | */ |
||
211 | |||
212 | /** |
||
213 | * Return content directory path. |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | public function getContentPath() |
||
221 | |||
222 | /** |
||
223 | * Return templates directory path. |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | public function getLayoutsPath() |
||
231 | |||
232 | /** |
||
233 | * Return themes directory path. |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | public function getThemesPath() |
||
241 | |||
242 | /** |
||
243 | * Return internal templates directory path. |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | public function getInternalLayoutsPath() |
||
251 | |||
252 | /** |
||
253 | * Return output directory path. |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | public function getOutputPath() |
||
261 | |||
262 | /** |
||
263 | * Return static files directory path. |
||
264 | * |
||
265 | * @return string |
||
266 | */ |
||
267 | public function getStaticPath() |
||
271 | |||
272 | /** |
||
273 | * Themes helpers. |
||
274 | */ |
||
275 | |||
276 | /** |
||
277 | * Return theme(s). |
||
278 | * |
||
279 | * @return array|null |
||
280 | */ |
||
281 | public function getTheme() |
||
291 | |||
292 | /** |
||
293 | * Has a (valid) theme(s)? |
||
294 | * |
||
295 | * @throws Exception |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | public function hasTheme() |
||
317 | |||
318 | /** |
||
319 | * Return the path of a specific theme's directory. |
||
320 | * |
||
321 | * @param string $theme |
||
322 | * @param string $dir |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | public function getThemeDirPath($theme, $dir = 'layouts') |
||
330 | |||
331 | /** |
||
332 | * Return "clean" array output format array. |
||
333 | * |
||
334 | * @param string $format |
||
335 | * |
||
336 | * @return array |
||
337 | */ |
||
338 | public function getOutputFormat(string $format): array |
||
351 | } |
||
352 |