Conditions | 12 |
Paths | 144 |
Total Lines | 45 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
54 | function timeDifference($start, $end, $return = 'days') |
||
55 | { |
||
56 | //change times to Unix timestamp. |
||
57 | //$start = strtotime($start); |
||
58 | //$end = strtotime($end); |
||
59 | //subtract dates |
||
60 | $difference = max($end, $start) - min($end,$start); |
||
61 | $time = null; |
||
62 | //24 hours equal to 86400 |
||
63 | //calculate time difference. |
||
64 | switch($return) { |
||
65 | case 'days': |
||
66 | $days = floor($difference/86400); |
||
67 | $difference = $difference % 86400; |
||
68 | $time['days'] = $days; |
||
|
|||
69 | case 'hours': |
||
70 | $hours = floor($difference/3600); |
||
71 | $difference = $difference % 3600; |
||
72 | $time['hours'] = $hours; |
||
73 | case 'minutes': |
||
74 | $minutes = floor($difference/60); |
||
75 | $difference = $difference % 60; |
||
76 | $time['minutes'] = $minutes; |
||
77 | case 'seconds': |
||
78 | $seconds = $difference; |
||
79 | $time['seconds'] = $seconds; |
||
80 | } |
||
81 | |||
82 | $output = []; |
||
83 | if(is_array($time)) { |
||
84 | $showSec = true; |
||
85 | if(isset($time['hours']) && $time['hours'] > 0) { |
||
86 | $output[] = $time['hours']. ' ' . _MB_XOOPSMEMBERS_HOUR; |
||
87 | $showSec = false; |
||
88 | } |
||
89 | |||
90 | if(isset($time['minutes']) && $time['minutes'] > 0) { |
||
91 | $output[] = $time['minutes']. ' ' . _MB_XOOPSMEMBERS_MINUTES; |
||
92 | $showSec = false; |
||
93 | } |
||
94 | |||
95 | if (isset($time['seconds']) && true === $showSec) { |
||
96 | return $time['seconds']. ' ' . _MB_XOOPSMEMBERS_SECONDS; |
||
97 | } |
||
98 | return implode(', ',$output); |
||
99 | } |
||
148 |