Conditions | 10 |
Paths | 4 |
Total Lines | 30 |
Code Lines | 21 |
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 |
||
17 | public static function difference(PhpDateTime $time1, PhpDateTime $time2) |
||
18 | { |
||
19 | if ($time1 == $time2) { |
||
20 | return '0'; |
||
21 | } |
||
22 | |||
23 | $interval = $time1->diff($time2); |
||
24 | $years = $interval->format('%y'); |
||
25 | $months = $interval->format('%m'); |
||
26 | $days = $interval->format('%d'); |
||
27 | $hours = $interval->format('%h'); |
||
28 | $minutes = $interval->format('%i'); |
||
29 | $seconds = $interval->format('%s'); |
||
30 | |||
31 | $differenceString = trim( |
||
32 | ($years ? $years . 'Y ' : '') |
||
33 | . ($months ? $months . 'M ' : '') |
||
34 | . ($days ? $days . 'd ' : '') |
||
35 | . ($hours ? $hours . 'h ' : '') |
||
36 | . ($minutes ? $minutes . 'm ' : '') |
||
37 | . ($seconds ? $seconds . 's ' : '') |
||
38 | ); |
||
39 | |||
40 | if (!strlen($differenceString)) { |
||
41 | $milliseconds = max(0, $time2->format("u") / 1000 - $time1->format("u") / 1000); |
||
42 | $differenceString = $milliseconds ? sprintf('%0.2fms', $milliseconds) : ''; |
||
43 | } |
||
44 | |||
45 | return $differenceString; |
||
46 | } |
||
47 | |||
61 |