| Conditions | 17 |
| Paths | 252 |
| Total Lines | 61 |
| Code Lines | 37 |
| 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 |
||
| 165 | public static function convertDateTime($dateInput) |
||
| 166 | { |
||
| 167 | $seconds = 0; // Time expressed as fraction of 24h hours in seconds |
||
| 168 | $year = $month = $day = 0; |
||
| 169 | |||
| 170 | $date_time = $dateInput; |
||
| 171 | if (preg_match("/(\d{4})\-(\d{2})\-(\d{2})/", $date_time, $matches)) { |
||
| 172 | list($junk, $year, $month, $day) = $matches; |
||
| 173 | } |
||
| 174 | if (preg_match("/(\d+):(\d{2}):(\d{2})/", $date_time, $matches)) { |
||
| 175 | list($junk, $hour, $min, $sec) = $matches; |
||
| 176 | $seconds = ($hour * 60 * 60 + $min * 60 + $sec) / (24 * 60 * 60); |
||
| 177 | } |
||
| 178 | unset($junk); |
||
| 179 | |||
| 180 | //using 1900 as epoch, not 1904, ignoring 1904 special case |
||
| 181 | // Special cases for Excel. |
||
| 182 | if ('1899-12-31' == "$year-$month-$day") { |
||
| 183 | return $seconds; |
||
| 184 | } // Excel 1900 epoch |
||
| 185 | if ('1900-01-00' == "$year-$month-$day") { |
||
| 186 | return $seconds; |
||
| 187 | } // Excel 1900 epoch |
||
| 188 | if ('1900-02-29' == "$year-$month-$day") { |
||
| 189 | return 60 + $seconds; |
||
| 190 | } // Excel false leapday |
||
| 191 | // We calculate the date by calculating the number of days since the epoch |
||
| 192 | // and adjust for the number of leap days. We calculate the number of leap |
||
| 193 | // days by normalising the year in relation to the epoch. Thus the year 2000 |
||
| 194 | // becomes 100 for 4 and 100 year leapdays and 400 for 400 year leapdays. |
||
| 195 | $epoch = 1900; |
||
| 196 | $offset = 0; |
||
| 197 | $norm = 300; |
||
| 198 | $range = $year - $epoch; |
||
| 199 | // Set month days and check for leap year. |
||
| 200 | $leap = ((0 == $year % 400) || ((0 == $year % 4) && ($year % 100))) ? 1 : 0; |
||
| 201 | $mdays = [31, ($leap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
||
| 202 | // Some boundary checks |
||
| 203 | if ($year < $epoch || $year > 9999) { |
||
| 204 | return 0; |
||
| 205 | } |
||
| 206 | if ($month < 1 || $month > 12) { |
||
| 207 | return 0; |
||
| 208 | } |
||
| 209 | if ($day < 1 || $day > $mdays[$month - 1]) { |
||
| 210 | return 0; |
||
| 211 | } |
||
| 212 | // Accumulate the number of days since the epoch. |
||
| 213 | $days = $day; // Add days for current month |
||
| 214 | $days += array_sum(array_slice($mdays, 0, $month - 1)); // Add days for past months |
||
| 215 | $days += $range * 365; // Add days for past years |
||
| 216 | $days += intval(($range) / 4); // Add leapdays |
||
| 217 | $days -= intval(($range + $offset) / 100); // Subtract 100 year leapdays |
||
| 218 | $days += intval(($range + $offset + $norm) / 400); // Add 400 year leapdays |
||
| 219 | $days -= $leap; // Already counted above |
||
| 220 | // Adjust for Excel erroneously treating 1900 as a leap year. |
||
| 221 | if ($days > 59) { |
||
| 222 | ++$days; |
||
| 223 | } |
||
| 224 | |||
| 225 | return $days + $seconds; |
||
| 226 | } |
||
| 239 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.