Conditions | 3 |
Paths | 3 |
Total Lines | 56 |
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 |
||
78 | function StrToSec ($str) { |
||
79 | if (empty($str)) |
||
80 | return 0; |
||
81 | |||
82 | // All number, return directly |
||
83 | if (is_numeric($str)) |
||
84 | return $str; |
||
85 | |||
86 | // Parse c, y, m, w, d, h, i, s |
||
87 | $str = strtolower($str); |
||
88 | $str = strtr($str, array( |
||
89 | 'sec' => 's', |
||
90 | 'second' => 's', |
||
91 | 'seconds' => 's', |
||
92 | 'min' => 'i', |
||
93 | 'minute' => 'i', |
||
94 | 'minutes' => 'i', |
||
95 | 'hour' => 'h', |
||
96 | 'hours' => 'h', |
||
97 | 'day' => 'd', |
||
98 | 'days' => 'd', |
||
99 | 'week' => 'w', |
||
100 | 'weeks' => 'w', |
||
101 | 'month' => 'm', |
||
102 | 'months' => 'm', |
||
103 | 'year' => 'y', |
||
104 | 'years' => 'y', |
||
105 | 'century' => 'c', |
||
106 | 'centuries' => 'c', |
||
107 | )); |
||
108 | $str = preg_replace(array( |
||
109 | '/([+-]?\d+)s/', |
||
110 | '/([+-]?\d+)i/', |
||
111 | '/([+-]?\d+)h/', |
||
112 | '/([+-]?\d+)d/', |
||
113 | '/([+-]?\d+)w/', |
||
114 | '/([+-]?\d+)m/', |
||
115 | '/([+-]?\d+)y/', |
||
116 | '/([+-]?\d+)c/', |
||
117 | ), array( |
||
118 | '+$1 ', |
||
119 | '+$1 * 60 ', |
||
120 | '+$1 * 3600 ', |
||
121 | '+$1 * 86400 ', |
||
122 | '+$1 * 604800 ', |
||
123 | '+$1 * 2592000 ', |
||
124 | '+$1 * 31536000 ', |
||
125 | '+$1 * 3153600000 ', |
||
126 | ), $str); |
||
127 | // Fix +- |
||
128 | $str = preg_replace('/\+\s*\-/', '-', $str); |
||
129 | $str = preg_replace('/\-\s*\+/', '-', $str); |
||
130 | $str = preg_replace('/\+\s*\+/', '+', $str); |
||
131 | eval('$i_sec = ' . $str . ';'); |
||
132 | return $i_sec; |
||
|
|||
133 | } // end of func StrToSec |
||
134 | |||
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.