Conditions | 12 |
Paths | 144 |
Total Lines | 49 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php declare(strict_types=1); |
||
55 | function timeDifference($start, $end, $return = 'days') |
||
56 | { |
||
57 | //change times to Unix timestamp. |
||
58 | //$start = strtotime($start); |
||
59 | //$end = strtotime($end); |
||
60 | //subtract dates |
||
61 | $difference = max($end, $start) - min($end, $start); |
||
62 | $time = null; |
||
63 | //24 hours equal to 86400 |
||
64 | //calculate time difference. |
||
65 | switch ($return) { |
||
66 | case 'days': |
||
67 | $days = floor($difference / 86400); |
||
68 | $difference = $difference % 86400; |
||
69 | $time['days'] = $days; |
||
70 | // no break |
||
71 | case 'hours': |
||
72 | $hours = floor($difference / 3600); |
||
73 | $difference = $difference % 3600; |
||
74 | $time['hours'] = $hours; |
||
75 | // no break |
||
76 | case 'minutes': |
||
77 | $minutes = floor($difference / 60); |
||
78 | $difference = $difference % 60; |
||
79 | $time['minutes'] = $minutes; |
||
80 | // no break |
||
81 | case 'seconds': |
||
82 | $seconds = $difference; |
||
83 | $time['seconds'] = $seconds; |
||
84 | } |
||
85 | |||
86 | $output = []; |
||
87 | if (is_array($time)) { |
||
|
|||
88 | $showSec = true; |
||
89 | if (isset($time['hours']) && $time['hours'] > 0) { |
||
90 | $output[] = $time['hours'] . ' ' . _MB_XOOPSMEMBERS_HOUR; |
||
91 | $showSec = false; |
||
92 | } |
||
93 | |||
94 | if (isset($time['minutes']) && $time['minutes'] > 0) { |
||
95 | $output[] = $time['minutes'] . ' ' . _MB_XOOPSMEMBERS_MINUTES; |
||
96 | $showSec = false; |
||
97 | } |
||
98 | |||
99 | if (isset($time['seconds']) && true === $showSec) { |
||
100 | return $time['seconds'] . ' ' . _MB_XOOPSMEMBERS_SECONDS; |
||
101 | } |
||
102 | |||
103 | return implode(', ', $output); |
||
104 | } |
||
152 |