Conditions | 11 |
Paths | 11 |
Total Lines | 39 |
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 |
||
26 | function SecToStr ($i_sec, $b_simple = true) { |
||
27 | if (empty($i_sec) || !is_numeric($i_sec)) |
||
28 | return ''; |
||
29 | |||
30 | $ar_dict = array( |
||
31 | array('c', -1, 'century', 'centuries'), |
||
32 | array('y', 100, 'year', 'years'), |
||
33 | // 12m != 1y, can't count month in. |
||
34 | // array('m', 12, 'month', 'months'), |
||
35 | array('d', 365, 'day', 'days'), |
||
36 | array('h', 24, 'hour', 'hours'), |
||
37 | array('i', 60, 'minute', 'minutes'), |
||
38 | array('s', 60, 'second', 'seconds'), |
||
39 | ); |
||
40 | $i = count($ar_dict); |
||
41 | // Loop from end of $ar_dict |
||
42 | $s = ''; |
||
43 | while (0 < $i && 0 < $i_sec) { |
||
44 | // 1. for loop, 2. got current array index |
||
45 | $i --; |
||
46 | |||
47 | // Reach top level, end loop |
||
48 | if (-1 == $ar_dict[$i][1]) { |
||
49 | $s = $i_sec . $ar_dict[$i][(($b_simple) ? 0 |
||
50 | : ((1 == $i_sec) ? 2 : 3))] |
||
51 | . ' ' . $s; |
||
52 | break; |
||
53 | } |
||
54 | |||
55 | $j = $i_sec % $ar_dict[$i][1]; |
||
56 | if (0 != $j) |
||
57 | $s = $j . $ar_dict[$i][(($b_simple) ? 0 |
||
58 | : ((1 == $i_sec) ? 2 : 3))] |
||
59 | . ' ' . $s; |
||
60 | $i_sec = floor($i_sec / $ar_dict[$i][1]); |
||
61 | } |
||
62 | |||
63 | return rtrim($s); |
||
64 | } // end of func SecToStr |
||
65 | |||
152 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.