Conditions | 14 |
Paths | 128 |
Total Lines | 55 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
11 | function time_elapsed_string($datetime, $full = false) { |
||
|
|||
12 | global $cLang; |
||
13 | $now = new DateTime; |
||
14 | $ago = new DateTime($datetime); |
||
15 | $diff = $now->diff($ago); |
||
16 | |||
17 | $diff->w = floor($diff->d / 7); |
||
18 | $diff->d -= $diff->w * 7; |
||
19 | |||
20 | if(!isset($cLang)) { |
||
21 | $string = array( |
||
22 | 'y' => 'year', |
||
23 | 'm' => 'month', |
||
24 | 'w' => 'week', |
||
25 | 'd' => 'day', |
||
26 | 'h' => 'hour', |
||
27 | 'i' => 'minute', |
||
28 | 's' => 'second', |
||
29 | ); |
||
30 | } else { |
||
31 | $string = array( |
||
32 | 'y' => $cLang['year'], |
||
33 | 'm' => $cLang['month'], |
||
34 | 'w' => $cLang['week'], |
||
35 | 'd' => $cLang['day'], |
||
36 | 'h' => $cLang['hour'], |
||
37 | 'i' => $cLang['minute'], |
||
38 | 's' => $cLang['second'], |
||
39 | ); |
||
40 | } |
||
41 | |||
42 | if(!isset($cLang)) { |
||
43 | foreach ($string as $k => &$v) { |
||
44 | if ($diff->$k) { |
||
45 | $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : ''); |
||
46 | } else { |
||
47 | unset($string[$k]); |
||
48 | } |
||
49 | } |
||
50 | } else { |
||
51 | foreach ($string as $k => &$v) { |
||
52 | if ($diff->$k) { |
||
53 | $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? $cLang['pluralTimeRight'] : ''); |
||
54 | } else { |
||
55 | unset($string[$k]); |
||
56 | } |
||
57 | } |
||
58 | } |
||
59 | |||
60 | if(!isset($cLang)) { |
||
61 | if (!$full) $string = array_slice($string, 0, 1); |
||
62 | return $string ? implode(', ', $string) . ' ago' : 'just now'; |
||
63 | } else { |
||
64 | if (!$full) $string = array_slice($string, 0, 1); |
||
65 | return $string ? implode($cLang['agoLeft'], $string) . " " . " " . $cLang['agoRight'] : $cLang['justNow']; |
||
66 | } |
||
95 | ?> |
||
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.