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 |
||
30 | class Admin |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * The real ModuleAdmin object |
||
35 | * |
||
36 | * @var object |
||
37 | */ |
||
38 | private static $ModuleAdmin = null; |
||
39 | private $version26 = null; |
||
40 | private $lastInfoBoxTitle = null; |
||
41 | private static $paypal = ''; |
||
42 | |||
43 | /** |
||
44 | * Constructor |
||
45 | */ |
||
46 | private function __construct() |
||
50 | |||
51 | /** |
||
52 | * Retrieve a module admin instance |
||
53 | * |
||
54 | * If we are on 2.6 this will be the a XoopsModuleAdmin instance. |
||
55 | * Older systems with the Frameworks based admin class will get |
||
56 | * an instance of this class which provides compatible methods |
||
57 | * built from the old Frameworks version. |
||
58 | * |
||
59 | * **Always use this to get the ModuleAdmin instance if you use |
||
60 | * anything (even the static methods) of this class.** |
||
61 | * |
||
62 | * @return object a ModuleAdmin instance. |
||
63 | * |
||
64 | * @since 1.0 |
||
65 | */ |
||
66 | public static function getInstance() |
||
67 | { |
||
68 | |||
69 | static $instance; |
||
70 | |||
71 | if ($instance === null) { |
||
72 | if (class_exists('\Xoops\Module\Admin', true)) { |
||
73 | $instance = new \Xoops\Module\Admin; |
||
74 | self::$ModuleAdmin = $instance; |
||
75 | } else { |
||
76 | \Xmf\Loader::loadFile( |
||
77 | \XoopsBaseConfig::get('root-path') . |
||
78 | '/Frameworks/moduleclasses/moduleadmin/moduleadmin.php' |
||
79 | ); |
||
80 | self::$ModuleAdmin = new \ModuleAdmin; |
||
81 | $instance = new \Xmf\Module\Admin; |
||
82 | } |
||
83 | |||
84 | } |
||
85 | |||
86 | return $instance; |
||
87 | |||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Are we in a 2.6 environment? |
||
92 | * |
||
93 | * just to help with other admin things than ModuleAdmin |
||
94 | * |
||
95 | * not part of 2.6 module admin |
||
96 | * |
||
97 | * @return bool true if we are in a 2.6 environment |
||
98 | */ |
||
99 | public static function is26() |
||
103 | |||
104 | /** |
||
105 | * Get an appropriate imagePath for menu.php use. |
||
106 | * |
||
107 | * just to help with other admin things than ModuleAdmin |
||
108 | * |
||
109 | * not part of 2.6 module admin |
||
110 | * |
||
111 | * @param string $image icon name to prepend with path |
||
112 | * |
||
113 | * @return string true if we are in a 2.6 environment |
||
114 | */ |
||
115 | public static function menuIconPath($image) |
||
116 | { |
||
117 | if (self::is26()) { |
||
118 | return($image); |
||
119 | } else { |
||
120 | $path='../../Frameworks/moduleclasses/icons/32/'; |
||
121 | |||
122 | return($path.$image); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Add config line |
||
128 | * |
||
129 | * @param string $value message to include in config box |
||
130 | * @param string $type type of line to add |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | public function addConfigBoxLine($value = '', $type = 'default') |
||
138 | |||
139 | /** |
||
140 | * Add Info box |
||
141 | * |
||
142 | * @param string $title info box title |
||
143 | * @param string $type for compatibility only |
||
144 | * @param string $extra for compatibility only |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | public function addInfoBox($title, $type = 'default', $extra = '') |
||
149 | { |
||
150 | $this->lastInfoBoxTitle = $title; |
||
151 | |||
152 | return self::$ModuleAdmin->addInfoBox($title); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Add line to the info box |
||
157 | * |
||
158 | * @param string $text text to add to info box |
||
159 | * @param string $type type of infobox line |
||
160 | * @param string $color color for infobox line |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function addInfoBoxLine($text = '', $type = 'default', $color = 'inherit') |
||
165 | { |
||
166 | return self::$ModuleAdmin->addInfoBoxLine( |
||
167 | $this->lastInfoBoxTitle, |
||
168 | $text, |
||
169 | '', |
||
170 | $color, |
||
171 | $type |
||
172 | ); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Add Item button |
||
177 | * |
||
178 | * @param string $title title of button |
||
179 | * @param string $link link for button |
||
180 | * @param string $icon icon for button |
||
181 | * @param string $extra extra |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function addItemButton($title, $link, $icon = 'add', $extra = '') |
||
189 | |||
190 | /** |
||
191 | * Render all items buttons |
||
192 | * |
||
193 | * @param string $position button position (left, right) |
||
194 | * @param string $delimiter delimiter between buttons |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | public function renderButton($position = null, $delimiter = " ") |
||
199 | { |
||
200 | if (null === $position) { |
||
201 | $position = 'right'; |
||
202 | } |
||
203 | |||
204 | return self::$ModuleAdmin->renderButton($position, $delimiter); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Display all item buttons |
||
209 | * |
||
210 | * @param string $position button position (left, right) |
||
211 | * @param string $delimiter delimiter between buttons |
||
212 | * |
||
213 | * @return void |
||
214 | */ |
||
215 | public function displayButton($position = null, $delimiter = " ") |
||
219 | |||
220 | /** |
||
221 | * Render InfoBox |
||
222 | * |
||
223 | * @return string HTML rendered info box |
||
224 | */ |
||
225 | public function renderInfoBox() |
||
229 | |||
230 | /** |
||
231 | * Display InfoBox |
||
232 | * |
||
233 | * @return void |
||
234 | */ |
||
235 | public function displayInfoBox() |
||
239 | |||
240 | /** |
||
241 | * Render index page for admin |
||
242 | * |
||
243 | * @return string HTML rendered info box |
||
244 | */ |
||
245 | public function renderIndex() |
||
249 | |||
250 | /** |
||
251 | * Display index page for admin |
||
252 | * |
||
253 | * @return void |
||
254 | */ |
||
255 | public function displayIndex() |
||
259 | |||
260 | /** |
||
261 | * Display the navigation menu |
||
262 | * |
||
263 | * @param string $menu menu key (script name, i.e. index.php) |
||
264 | * |
||
265 | * @return void |
||
266 | */ |
||
267 | public function displayNavigation($menu = '') |
||
271 | |||
272 | /** |
||
273 | * Render about page |
||
274 | * |
||
275 | * @param bool $logo_xoops display XOOPS logo |
||
276 | * |
||
277 | * @return bool|mixed|string |
||
278 | */ |
||
279 | public function renderAbout($logo_xoops = true) |
||
283 | |||
284 | /** |
||
285 | * set paypal for 2.5 renderAbout |
||
286 | * |
||
287 | * @param string $paypal PayPal identifier for donate button |
||
288 | * |
||
289 | * @return void |
||
290 | */ |
||
291 | public static function setPaypal($paypal = '') |
||
295 | |||
296 | /** |
||
297 | * Display about page |
||
298 | * |
||
299 | * @param bool $logo_xoops display XOOPS logo |
||
300 | * |
||
301 | * @return void |
||
302 | */ |
||
303 | public function displayAbout($logo_xoops = true) |
||
307 | |||
308 | // not in regular ModuleAdmin |
||
309 | |||
310 | /** |
||
311 | * Add error to config box |
||
312 | * |
||
313 | * @param string $value the error message |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | View Code Duplication | public static function addConfigError($value = '') |
|
334 | |||
335 | /** |
||
336 | * Add accept (OK) message to config box |
||
337 | * |
||
338 | * @param string $value the OK message |
||
339 | * |
||
340 | * @return bool |
||
341 | */ |
||
342 | View Code Duplication | public static function addConfigAccept($value = '') |
|
359 | |||
360 | /** |
||
361 | * Add warning to config box |
||
362 | * |
||
363 | * @param string $value the warning message |
||
364 | * |
||
365 | * @return bool |
||
366 | */ |
||
367 | View Code Duplication | public static function addConfigWarning($value = '') |
|
384 | |||
385 | /** |
||
386 | * Get an appropriate URL for system provided icons. |
||
387 | * |
||
388 | * Things which were in Frameworks in 2.5 are in media in 2.6, |
||
389 | * making it harder to use and rely on the standard icons. |
||
390 | * |
||
391 | * not part of 2.6, just a transition assist |
||
392 | * |
||
393 | * @param string $name the image name to provide URL for, or blank |
||
394 | * to just get the URL path. |
||
395 | * @param string $size the icon size (directory). Valid values are |
||
396 | * 16, 32 or /. A '/' slash will simply set the |
||
397 | * path to the icon directory and append $image. |
||
398 | * |
||
399 | * @return string path to icons |
||
400 | */ |
||
401 | public static function iconUrl($name = '', $size = '32') |
||
424 | |||
425 | /** |
||
426 | * Check for installed module and version and do addConfigBoxLine() |
||
427 | * |
||
428 | * @param string $moddir - module directory name |
||
429 | * @param integer $minversion - minimum acceptable module version (100 = V1.00) |
||
430 | * |
||
431 | * @return bool true if requested version of the module is available |
||
432 | */ |
||
433 | public static function checkModuleVersion($moddir, $minversion) |
||
465 | } |
||
466 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.