Total Complexity | 55 |
Total Lines | 470 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Utility often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Utility, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class Utility extends Common\SysUtility |
||
36 | { |
||
37 | //--------------- Custom module methods ----------------------------- |
||
38 | /** |
||
39 | * @param $eventId |
||
40 | * |
||
41 | * @return array |
||
42 | */ |
||
43 | public static function getEvent($eventId) |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param $REQUEST |
||
59 | * @param $event_picture1 |
||
60 | * @param $event_picture2 |
||
61 | */ |
||
62 | public static function loadImg($REQUEST, &$event_picture1, &$event_picture2) |
||
63 | { |
||
64 | /////////////////////////////////////////////////////////////////////////////// |
||
65 | $uploaddir_event = XOOPS_ROOT_PATH . '/uploads/extcal/'; |
||
66 | $uploadurl_event = XOOPS_URL . '/uploads/extcal/'; |
||
67 | //$picture = ''; |
||
68 | for ($j = 1; $j < 3; ++$j) { |
||
69 | $delimg = @$REQUEST['delimg_' . $j . '']; |
||
70 | $delimg = isset($delimg) ? (int)$delimg : 0; |
||
71 | if (0 == $delimg && !empty($REQUEST['xoops_upload_file'][$j])) { |
||
72 | $upload = new \XoopsMediaUploader( |
||
73 | $uploaddir_event, [ |
||
74 | 'image/gif', |
||
75 | 'image/jpeg', |
||
76 | 'image/pjpeg', |
||
77 | 'image/x-png', |
||
78 | 'image/png', |
||
79 | 'image/jpg', |
||
80 | ], 3145728, null, null |
||
81 | ); |
||
82 | if ($upload->fetchMedia($REQUEST['xoops_upload_file'][$j])) { |
||
83 | $upload->setPrefix('event_'); |
||
84 | $upload->fetchMedia($REQUEST['xoops_upload_file'][$j]); |
||
85 | if (!$upload->upload()) { |
||
86 | $errors = $upload->getErrors(); |
||
87 | \redirect_header('<script>javascript:history.go(-1)</script>', 3, $errors); |
||
88 | } else { |
||
89 | if (1 == $j) { |
||
90 | $event_picture1 = $upload->getSavedFileName(); |
||
91 | } elseif (2 == $j) { |
||
92 | $event_picture2 = $upload->getSavedFileName(); |
||
93 | } |
||
94 | } |
||
95 | } elseif (!empty($REQUEST['file' . $j])) { |
||
96 | if (1 == $j) { |
||
97 | $event_picture1 = $REQUEST['file' . $j]; |
||
98 | } elseif (2 == $j) { |
||
99 | $event_picture2 = $REQUEST['file' . $j]; |
||
100 | } |
||
101 | } |
||
102 | } else { |
||
103 | $url_event = XOOPS_ROOT_PATH . '/uploads/extcal/' . $REQUEST['file' . $j]; |
||
104 | if (1 == $j) { |
||
105 | $event_picture1 = ''; |
||
106 | } elseif (2 == $j) { |
||
107 | $event_picture2 = ''; |
||
108 | } |
||
109 | if (\is_file($url_event)) { |
||
110 | \chmod($url_event, 0777); |
||
111 | \unlink($url_event); |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | //exit; |
||
116 | /////////////////////////////////////////////////////////////////////////////// |
||
117 | } |
||
118 | |||
119 | /******************************************************************* |
||
120 | * |
||
121 | ****************************************************************** |
||
122 | * @param $cat |
||
123 | * @param bool $addNone |
||
124 | * @param string $name |
||
125 | * @return \XoopsFormSelect |
||
126 | */ |
||
127 | public static function getListCategories($cat, $addNone = true, $name = 'cat') |
||
128 | { |
||
129 | global $xoopsUser; |
||
130 | // Category selectbox |
||
131 | $categoryHandler = Helper::getInstance()->getHandler(\_EXTCAL_CLN_CAT); |
||
132 | |||
133 | $catsList = $categoryHandler->getAllCat($xoopsUser); |
||
134 | $catSelect = new \XoopsFormSelect('', $name, $cat); |
||
135 | if ($addNone) { |
||
136 | $catSelect->addOption(0, ' '); |
||
137 | } |
||
138 | |||
139 | foreach ($catsList as $catList) { |
||
140 | $catSelect->addOption($catList->getVar('cat_id'), $catList->getVar('cat_name')); |
||
141 | } |
||
142 | |||
143 | return $catSelect; |
||
144 | } |
||
145 | |||
146 | /******************************************************************* |
||
147 | * |
||
148 | ****************************************************************** |
||
149 | * @param string $name |
||
150 | * @param array|int|null $cat |
||
151 | * @return array |
||
152 | */ |
||
153 | public static function getCheckeCategories($name = 'cat', $cat = null) |
||
154 | { |
||
155 | global $xoopsUser; |
||
156 | // Category selectbox |
||
157 | //<option style="background-color:#00FFFF;">VARCHAR</option> |
||
158 | |||
159 | $categoryHandler = Helper::getInstance()->getHandler(\_EXTCAL_CLN_CAT); |
||
160 | $catsList = $categoryHandler->getAllCat($xoopsUser); |
||
161 | |||
162 | $t = []; |
||
163 | foreach ($catsList as $catList) { |
||
164 | $cat_id = $catList->getVar('cat_id'); |
||
165 | $name = $catList->getVar('cat_name'); |
||
166 | $cat_color = $catList->getVar('cat_color'); |
||
167 | $checked = \in_array($cat_id, $cat) ? 'checked' : ''; |
||
168 | $cat = '' |
||
169 | . "<div style='float:left; margin-left:5px;'>" |
||
170 | . "<input type='checkbox' name='{$name}[{$cat_id}]' value='1' {$checked}>" |
||
171 | . "<div style='absolute:left;height:12px; width:6px; background-color:#{$cat_color}; border:1px solid #000000; float:left; margin-right:5px;' ></div>" |
||
172 | . " {$name}" |
||
173 | . '</div>'; |
||
174 | |||
175 | $t[] = $cat; |
||
176 | } |
||
177 | |||
178 | return $t; |
||
179 | } |
||
180 | |||
181 | /******************************************************************* |
||
182 | * |
||
183 | ****************************************************************** |
||
184 | * @param string $name |
||
185 | * @param string $caption |
||
186 | * @param mixed $default |
||
187 | * @param bool $addNone |
||
188 | * @return \XoopsFormSelect |
||
189 | */ |
||
190 | public static function getListOrderBy($name = 'orderby', $caption = '', $default = null, $addNone = false) |
||
212 | } |
||
213 | |||
214 | /******************************************************************* |
||
215 | * |
||
216 | ****************************************************************** |
||
217 | * @param string $name |
||
218 | * @param string $caption |
||
219 | * @param mixed $default |
||
220 | * @return \XoopsFormSelect |
||
221 | */ |
||
222 | public static function getListAndOr($name = 'andor', $caption = '', $default = null) |
||
223 | { |
||
224 | global $xoopsUser; |
||
225 | |||
226 | $select = new \XoopsFormSelect($caption, $name, $default); |
||
227 | |||
228 | $select->addOption('AND', \_MD_EXTCAL_AND); |
||
229 | $select->addOption('OR', \_MD_EXTCAL_OR); |
||
230 | |||
231 | return $select; |
||
232 | } |
||
233 | |||
234 | /******************************************************************* |
||
235 | * |
||
236 | ****************************************************************** |
||
237 | * @param $name |
||
238 | * @param $caption |
||
239 | * @param $default |
||
240 | * @param $options |
||
241 | * @param string $sep |
||
242 | * @return \XoopsFormSelect |
||
243 | */ |
||
244 | public static function getList($name, $caption, $default, $options, $sep = ';') |
||
245 | { |
||
246 | global $xoopsUser; |
||
247 | |||
248 | $select = new \XoopsFormSelect($caption, $name, $default); |
||
249 | if (!\is_array($options)) { |
||
250 | $options = \explode($sep, $options); |
||
251 | } |
||
252 | |||
253 | foreach ($options as $h => $hValue) { |
||
254 | $select->addOption($h, $options[$h]); |
||
255 | } |
||
256 | |||
257 | return $select; |
||
258 | } |
||
259 | |||
260 | /******************************************************************* |
||
261 | * |
||
262 | ****************************************************************** |
||
263 | * @param $ts |
||
264 | * @param $startMonth |
||
265 | * @param $endMonth |
||
266 | * @param string $mode |
||
267 | * @return \DateTime |
||
268 | * @throws \Exception |
||
269 | * @throws \Exception |
||
270 | */ |
||
271 | public static function getDateBetweenDates($ts, $startMonth, $endMonth, $mode = 'w') |
||
272 | { |
||
273 | $d = new \DateTime($periodStart); |
||
274 | $d->setTimestamp($ts); |
||
275 | |||
276 | //echo "<br>affichage des periodes : <br>"; |
||
277 | $begin = new \DateTime(); |
||
278 | $begin->setTimestamp($startMonth); |
||
279 | //echo $begin->format("d/m/Y à H\hi:s").'<br>'; // 03/10/2007 à 19h39:53 |
||
280 | |||
281 | $end = new \DateTime(); |
||
282 | $end->setTimestamp($endMonth); |
||
283 | //echo $end->format("d/m/Y à H\hi:s").'<br>'; // 03/10/2007 à 19h39:53 |
||
284 | //echo "<hr>"; |
||
285 | $interval = \DateInterval::createFromDateString('next sunday'); |
||
286 | $period = new \DatePeriod($begin, $interval, $end); |
||
287 | //echoDateArray($period); |
||
288 | |||
289 | //echo "<hr>{$interval}"; |
||
290 | return $d; |
||
291 | //echo mktime($heure, $minute, $seconde, $mois, $jour, $an); |
||
292 | |||
293 | // |
||
294 | // $jour = date('d', $ts); |
||
295 | // $mois = date('m', $ts); |
||
296 | // $an = date('Y', $ts); |
||
297 | // $heure = date('H', $ts); |
||
298 | // $minute = date('i', $ts); |
||
299 | // $seconde = date('s', $ts); |
||
300 | // $d->setDate($heure,$minute,$seconde,$mois,$jour,$an); |
||
301 | |||
302 | // <?php |
||
303 | // $interval = DateInterval::createFromDateString('next sunday'); |
||
304 | // $period = new \DatePeriod($begin, $interval, $end); |
||
305 | // foreach ($period as $dt) { |
||
306 | // echo $dt->format( "l Y-m-d H:i:s\n" ); |
||
307 | } |
||
308 | |||
309 | /* |
||
310 | Sunday 2009-11-01 00:00:00 |
||
311 | Sunday 2009-11-08 00:00:00 |
||
312 | Sunday 2009-11-15 00:00:00 |
||
313 | Sunday 2009-11-22 00:00:00 |
||
314 | Sunday 2009-11-29 00:00:00 |
||
315 | Sunday 2009-12-06 00:00:00 |
||
316 | ... |
||
317 | */ |
||
318 | |||
319 | /** |
||
320 | * @param $period |
||
321 | */ |
||
322 | public static function echoDateArray($period) |
||
323 | { |
||
324 | foreach ($period as $dt) { |
||
325 | echo $dt->format("l Y-m-d H:i:s\n") . '<br>'; |
||
326 | } |
||
327 | } |
||
328 | |||
329 | /*****************************************************************/ |
||
330 | |||
331 | /** |
||
332 | * @param $t |
||
333 | * @param string $msg |
||
334 | */ |
||
335 | public static function echoArray($t, $msg = '') |
||
336 | { |
||
337 | if ('' != $msg) { |
||
338 | echo "<hr>{$msg}<hr>"; |
||
339 | } |
||
340 | |||
341 | $txt = \print_r($t, true); |
||
342 | echo '<pre>Number of items: ' . \count($t) . "<br>{$txt}</pre>"; |
||
343 | } |
||
344 | |||
345 | /*****************************************************************/ |
||
346 | |||
347 | /** |
||
348 | * @param $line |
||
349 | * @param string $msg |
||
350 | */ |
||
351 | public static function extEcho($line, $msg = '') |
||
352 | { |
||
353 | if ('' != $msg) { |
||
354 | echo "<hr>{$msg}<hr>"; |
||
355 | } |
||
356 | echo $line . '<br>'; |
||
357 | } |
||
358 | |||
359 | /*****************************************************************/ |
||
360 | |||
361 | /** |
||
362 | * @param $tsName |
||
363 | * @param string $msg |
||
364 | */ |
||
365 | public static function echoTsn($tsName, $msg = '') |
||
366 | { |
||
367 | global $$tsName; |
||
368 | $ts = $$tsName; |
||
369 | static::echoTsu($ts, $tsName, $msg = ''); |
||
370 | } |
||
371 | |||
372 | /*****************************************************************/ |
||
373 | |||
374 | /** |
||
375 | * @param $ts |
||
376 | * @param $tsName |
||
377 | * @param string $msg |
||
378 | */ |
||
379 | public static function echoTsu($ts, $tsName, $msg = '') |
||
380 | { |
||
381 | if ('' != $msg) { |
||
382 | echo "<hr>{$msg}<hr>"; |
||
383 | } |
||
384 | |||
385 | echo 'date --->' . $tsName . ' = ' . $ts . ' - ' . \date('d-m-Y H:m:s', $ts) . '<br>'; |
||
386 | } |
||
387 | |||
388 | /*****************************************************************/ |
||
389 | /*****************************************************************/ |
||
390 | |||
391 | /** |
||
392 | * @param $date |
||
393 | * @param string $sep |
||
394 | * |
||
395 | * @return int |
||
396 | */ |
||
397 | public static function convertDate($date, $sep = '-') |
||
398 | { |
||
399 | $lstSep = '/ .'; |
||
400 | |||
401 | for ($h = 0, $count = mb_strlen($lstSep); $h < $count; ++$h) { |
||
402 | $sep2replace = mb_substr($lstSep, $h, 1); |
||
403 | if (mb_strpos($date, $sep2replace)) { |
||
404 | $date = \str_replace($sep2replace, $sep, $date); |
||
405 | } |
||
406 | |||
407 | return \strtotime($date); |
||
408 | } |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * @param $givendate |
||
413 | * @param int $day |
||
414 | * @param int $mth |
||
415 | * @param int $yr |
||
416 | * |
||
417 | * @return int |
||
418 | */ |
||
419 | public static function addDate($givendate, $day = 0, $mth = 0, $yr = 0) |
||
420 | { |
||
421 | //$cd = strtotime($givendate); |
||
422 | $cd = $givendate; |
||
423 | $newdate = \date('Y-m-d h:i:s', \mktime(\date('h', $cd), \date('i', $cd), \date('s', $cd), \date('m', $cd) + $mth, \date('d', $cd) + $day, \date('Y', $cd) + $yr)); |
||
424 | |||
425 | return \strtotime($newdate); |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * @param $date |
||
430 | * @param $number |
||
431 | * @param $interval |
||
432 | * |
||
433 | * @return int |
||
434 | */ |
||
435 | public static function addDate2($date, $number, $interval = 'd') |
||
476 | } |
||
477 | |||
478 | // function date_diff($date1, $date2) { |
||
479 | // $current = $date1; |
||
480 | // $datetime2 = date_create($date2); |
||
481 | // $count = 0; |
||
482 | // while (date_create($current) < $datetime2) { |
||
483 | // $current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current))); |
||
484 | // ++$count; |
||
485 | // } |
||
486 | // return $count; |
||
487 | // } |
||
488 | |||
489 | /**************************************************************************/ |
||
490 | |||
491 | /** |
||
492 | * @param $color |
||
493 | * @param $plancher |
||
494 | * @param $plafond |
||
495 | * |
||
496 | * @return string |
||
497 | */ |
||
498 | public static function getLighterColor($color, $plancher, $plafond) |
||
505 | } |
||
506 | /**************************************************************************/ |
||
507 | } |
||
508 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: