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 |
||
32 | class Admin |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * The real ModuleAdmin object |
||
37 | * |
||
38 | * @var object |
||
39 | */ |
||
40 | protected static $ModuleAdmin = null; |
||
41 | protected $lastInfoBoxTitle = null; |
||
42 | protected static $paypal = ''; |
||
43 | |||
44 | /** |
||
45 | * Constructor |
||
46 | */ |
||
47 | protected function __construct() |
||
50 | |||
51 | /** |
||
52 | * Retrieve a module admin instance |
||
53 | * |
||
54 | * If we are on a next generation system this will be the a native Xoops\Module\Admin instance. |
||
55 | * Older systems with the Frameworks based admin class will get an instance of this class which |
||
56 | * provides compatible methods built from the old Frameworks version. |
||
57 | * |
||
58 | * @return object a ModuleAdmin or Xoops\Module\Admin instance. |
||
59 | * |
||
60 | * @since 1.0 |
||
61 | */ |
||
62 | public static function getInstance() |
||
81 | |||
82 | /** |
||
83 | * Add config line |
||
84 | * |
||
85 | * @param string $value message to include in config box |
||
86 | * @param string $type type of line to add |
||
87 | * minimal set of acceptable types and value expectation |
||
88 | * 'default' - value is message displayed directly (also used for unknown types) |
||
89 | * 'folder' - value is directory name, will display accept if exists, error if not |
||
90 | * 'chmod' - value is array(directory, permission) accept if exists with permission, |
||
91 | * else error |
||
92 | * 'module' - value is string module name, or array(module name, errortype) |
||
93 | * If module is active, an accept line displays, otherwise, a warning |
||
94 | * (if value is array(module, "warning") or an error displays. |
||
95 | * |
||
96 | * @return bool |
||
97 | */ |
||
98 | public function addConfigBoxLine($value = '', $type = 'default') |
||
116 | |||
117 | /** |
||
118 | * Add Info box |
||
119 | * |
||
120 | * @param string $title info box title |
||
121 | * @param string $type for compatibility only |
||
122 | * @param string $extra for compatibility only |
||
123 | * |
||
124 | * @return bool |
||
125 | */ |
||
126 | public function addInfoBox($title, $type = 'default', $extra = '') |
||
132 | |||
133 | /** |
||
134 | * Add line to the info box |
||
135 | * |
||
136 | * @param string $text text to add to info box |
||
137 | * @param string $type type of infobox line |
||
138 | * @param string $color color for infobox line |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | public function addInfoBoxLine($text = '', $type = 'default', $color = 'inherit') |
||
152 | |||
153 | /** |
||
154 | * Add Item button |
||
155 | * |
||
156 | * @param string $title title of button |
||
157 | * @param string $link link for button |
||
158 | * @param string $icon icon for button |
||
159 | * @param string $extra extra |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function addItemButton($title, $link, $icon = 'add', $extra = '') |
||
167 | |||
168 | /** |
||
169 | * Render all items buttons |
||
170 | * |
||
171 | * @param string $position button position (left, right) |
||
172 | * @param string $delimiter delimiter between buttons |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | public function renderButton($position = null, $delimiter = " ") |
||
184 | |||
185 | /** |
||
186 | * Display all item buttons |
||
187 | * |
||
188 | * @param string $position button position (left, right) |
||
189 | * @param string $delimiter delimiter between buttons |
||
190 | * |
||
191 | * @return void |
||
192 | */ |
||
193 | public function displayButton($position = null, $delimiter = " ") |
||
197 | |||
198 | /** |
||
199 | * Render InfoBox |
||
200 | * |
||
201 | * @return string HTML rendered info box |
||
202 | */ |
||
203 | public function renderInfoBox() |
||
207 | |||
208 | /** |
||
209 | * Display InfoBox |
||
210 | * |
||
211 | * @return void |
||
212 | */ |
||
213 | public function displayInfoBox() |
||
217 | |||
218 | /** |
||
219 | * Render index page for admin |
||
220 | * |
||
221 | * @return string HTML rendered info box |
||
222 | */ |
||
223 | public function renderIndex() |
||
227 | |||
228 | /** |
||
229 | * Display index page for admin |
||
230 | * |
||
231 | * @return void |
||
232 | */ |
||
233 | public function displayIndex() |
||
237 | |||
238 | /** |
||
239 | * Display the navigation menu |
||
240 | * |
||
241 | * @param string $menu menu key (script name, i.e. index.php) |
||
242 | * |
||
243 | * @return void |
||
244 | */ |
||
245 | public function displayNavigation($menu = '') |
||
249 | |||
250 | /** |
||
251 | * Render about page |
||
252 | * |
||
253 | * @param bool $logo_xoops display XOOPS logo |
||
254 | * |
||
255 | * @return bool|mixed|string |
||
256 | */ |
||
257 | public function renderAbout($logo_xoops = true) |
||
261 | |||
262 | /** |
||
263 | * Display about page |
||
264 | * |
||
265 | * @param bool $logo_xoops display XOOPS logo |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | public function displayAbout($logo_xoops = true) |
||
273 | |||
274 | /** |
||
275 | * Add error to config box |
||
276 | * |
||
277 | * @param string $value the error message |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | View Code Duplication | public function addConfigError($value = '') |
|
294 | |||
295 | /** |
||
296 | * Add accept (OK) message to config box |
||
297 | * |
||
298 | * @param string $value the OK message |
||
299 | * |
||
300 | * @return bool |
||
301 | */ |
||
302 | View Code Duplication | public function addConfigAccept($value = '') |
|
315 | |||
316 | /** |
||
317 | * Add warning to config box |
||
318 | * |
||
319 | * @param string $value the warning message |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | View Code Duplication | public function addConfigWarning($value = '') |
|
336 | |||
337 | |||
338 | /** |
||
339 | * Check for installed module and version and do addConfigBoxLine() |
||
340 | * |
||
341 | * @param string $moddir - module directory name |
||
342 | * @param integer $minversion - minimum acceptable module version (100 = V1.00) |
||
343 | * |
||
344 | * @return bool true if requested version of the module is available |
||
345 | */ |
||
346 | View Code Duplication | public function addConfigModuleVersion($moddir, $minversion) |
|
377 | |||
378 | // the following not part of next generation Xoops\Module\Admin |
||
379 | |||
380 | /** |
||
381 | * Are we in a next generation environment? |
||
382 | * |
||
383 | * not part of next generation Xoops\Module\Admin |
||
384 | * |
||
385 | * @return bool true if we are in a post XOOPS 2.5.x environment |
||
386 | */ |
||
387 | protected static function isXng() |
||
391 | |||
392 | /** |
||
393 | * Get an appropriate imagePath for menu.php use. |
||
394 | * |
||
395 | * just to help with other admin things than ModuleAdmin |
||
396 | * |
||
397 | * not part of next generation Xoops\Module\Admin |
||
398 | * |
||
399 | * @param string $image icon name to prepend with path |
||
400 | * |
||
401 | * @return string the icon path |
||
402 | */ |
||
403 | public static function menuIconPath($image) |
||
413 | |||
414 | /** |
||
415 | * Get an appropriate URL for system provided icons. |
||
416 | * |
||
417 | * Things which were in Frameworks in 2.5 are in media in later versions, |
||
418 | * making it harder to use and rely on the standard icons. |
||
419 | * |
||
420 | * not part of next generation Xoops\Module\Admin |
||
421 | * |
||
422 | * @param string $name the image name to provide URL for, or blank |
||
423 | * to just get the URL path. |
||
424 | * @param string $size the icon size (directory). Valid values are |
||
425 | * 16, 32 or /. A '/' slash will simply set the |
||
426 | * path to the icon directory and append $image. |
||
427 | * |
||
428 | * @return string path to icons |
||
429 | */ |
||
430 | public static function iconUrl($name = '', $size = '32') |
||
453 | |||
454 | /** |
||
455 | * set paypal for 2.5.x renderAbout |
||
456 | * |
||
457 | * not part of next generation Xoops\Module\Admin |
||
458 | * |
||
459 | * @param string $paypal PayPal identifier for donate button |
||
460 | * |
||
461 | * @return void |
||
462 | */ |
||
463 | public static function setPaypal($paypal = '') |
||
467 | } |
||
468 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: