| Conditions | 12 |
| Total Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 9 | function timeDifference($start,$end,$return='days') { |
||
| 10 | //change times to Unix timestamp. |
||
| 11 | //$start = strtotime($start); |
||
| 12 | //$end = strtotime($end); |
||
| 13 | //subtract dates |
||
| 14 | $difference = max($end, $start) - min($end,$start); |
||
| 15 | $time = NULL; |
||
| 16 | //24 hours equal to 86400 |
||
| 17 | //calculate time difference. |
||
| 18 | switch($return) { |
||
| 19 | case 'days': |
||
| 20 | $days = floor($difference/86400); |
||
| 21 | $difference = $difference % 86400; |
||
| 22 | $time['days'] = $days; |
||
|
|
|||
| 23 | case 'hours': |
||
| 24 | $hours = floor($difference/3600); |
||
| 25 | $difference = $difference % 3600; |
||
| 26 | $time['hours'] = $hours; |
||
| 27 | case 'minutes': |
||
| 28 | $minutes = floor($difference/60); |
||
| 29 | $difference = $difference % 60; |
||
| 30 | $time['minutes'] = $minutes; |
||
| 31 | case 'seconds': |
||
| 32 | $seconds = $difference; |
||
| 33 | $time['seconds'] = $seconds; |
||
| 34 | } |
||
| 35 | |||
| 36 | $output = array(); |
||
| 37 | if(is_array($time)) { |
||
| 38 | $showSec = true; |
||
| 39 | if(isset($time['hours']) && $time['hours'] > 0) { |
||
| 40 | $output[] = $time['hours'] . ' Hour'; |
||
| 41 | $showSec = false; |
||
| 42 | } |
||
| 43 | |||
| 44 | if(isset($time['minutes']) && $time['minutes'] > 0) { |
||
| 45 | $output[] = $time['minutes'] . ' minutes'; |
||
| 46 | $showSec = false; |
||
| 47 | } |
||
| 48 | |||
| 49 | if(isset($time['seconds']) && $showSec == true) { |
||
| 50 | return $time['seconds'] . ' seconds'; |
||
| 51 | } |
||
| 52 | return implode(', ',$output); |
||
| 53 | } |
||
| 136 |