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 |
||
23 | class Configuration |
||
24 | { |
||
25 | /** |
||
26 | * First line in INI file, to hide contents |
||
27 | * |
||
28 | * @const string |
||
29 | */ |
||
30 | const PROTECTION_LINE = ';<?php http_response_code(403); /*'; |
||
31 | |||
32 | /** |
||
33 | * parsed configuration |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | private $_configuration; |
||
38 | |||
39 | /** |
||
40 | * default configuration |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | private static $_defaults = array( |
||
45 | 'main' => array( |
||
46 | 'name' => 'PrivateBin', |
||
47 | 'discussion' => true, |
||
48 | 'opendiscussion' => false, |
||
49 | 'password' => true, |
||
50 | 'fileupload' => false, |
||
51 | 'burnafterreadingselected' => false, |
||
52 | 'instantburnafterreading' => false, |
||
53 | 'defaultformatter' => 'plaintext', |
||
54 | 'syntaxhighlightingtheme' => null, |
||
55 | 'sizelimit' => 2097152, |
||
56 | 'template' => 'bootstrap', |
||
57 | 'notice' => '', |
||
58 | 'languageselection' => false, |
||
59 | 'languagedefault' => '', |
||
60 | 'urlshortener' => '', |
||
61 | 'icon' => 'identicon', |
||
62 | 'cspheader' => 'default-src \'none\'; manifest-src \'self\'; connect-src *; script-src \'self\'; style-src \'self\'; font-src \'self\'; img-src \'self\' data:; referrer no-referrer; sandbox allow-same-origin allow-scripts allow-forms allow-popups', |
||
63 | 'zerobincompatibility' => false, |
||
64 | ), |
||
65 | 'expire' => array( |
||
66 | 'default' => '1week', |
||
67 | 'clone' => true, |
||
68 | ), |
||
69 | 'expire_options' => array( |
||
70 | '5min' => 300, |
||
71 | '10min' => 600, |
||
72 | '1hour' => 3600, |
||
73 | '1day' => 86400, |
||
74 | '1week' => 604800, |
||
75 | '1month' => 2592000, |
||
76 | '1year' => 31536000, |
||
77 | 'never' => 0, |
||
78 | ), |
||
79 | 'formatter_options' => array( |
||
80 | 'plaintext' => 'Plain Text', |
||
81 | 'syntaxhighlighting' => 'Source Code', |
||
82 | 'markdown' => 'Markdown', |
||
83 | ), |
||
84 | 'traffic' => array( |
||
85 | 'limit' => 10, |
||
86 | 'header' => null, |
||
87 | 'dir' => 'data', |
||
88 | ), |
||
89 | 'purge' => array( |
||
90 | 'limit' => 300, |
||
91 | 'batchsize' => 10, |
||
92 | 'dir' => 'data', |
||
93 | ), |
||
94 | 'model' => array( |
||
95 | 'class' => 'Filesystem', |
||
96 | ), |
||
97 | 'model_options' => array( |
||
98 | 'dir' => 'data', |
||
99 | ), |
||
100 | ); |
||
101 | |||
102 | /** |
||
103 | * parse configuration file and ensure default configuration values are present |
||
104 | * |
||
105 | * @throws Exception |
||
106 | */ |
||
107 | 127 | public function __construct() |
|
232 | |||
233 | /** |
||
234 | * get configuration as array |
||
235 | * |
||
236 | * return array |
||
237 | */ |
||
238 | 6 | public function get() |
|
242 | |||
243 | /** |
||
244 | * get default configuration as array |
||
245 | * |
||
246 | * return array |
||
247 | */ |
||
248 | 125 | public static function getDefaults() |
|
252 | |||
253 | /** |
||
254 | * get a key from the configuration, typically the main section or all keys |
||
255 | * |
||
256 | * @param string $key |
||
257 | * @param string $section defaults to main |
||
258 | * @throws Exception |
||
259 | * return mixed |
||
260 | */ |
||
261 | 116 | public function getKey($key, $section = 'main') |
|
269 | |||
270 | /** |
||
271 | * get a section from the configuration, must exist |
||
272 | * |
||
273 | * @param string $section |
||
274 | * @throws Exception |
||
275 | * return mixed |
||
276 | */ |
||
277 | 116 | public function getSection($section) |
|
284 | } |
||
285 |
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.