| Conditions | 14 |
| Paths | 32 |
| Total Lines | 40 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 23 | public static function convertDateTime($dateInput) |
||
| 24 | { |
||
| 25 | $epoch = 1900; |
||
| 26 | $norm = 300; |
||
| 27 | $year = $month = $day = $offset = $seconds = 0; |
||
| 28 | $dateTime = $dateInput; |
||
| 29 | if (preg_match("/(\d{4})\-(\d{2})\-(\d{2})/", $dateTime, $matches)) { |
||
| 30 | $year = $matches[1]; |
||
| 31 | $month = $matches[2]; |
||
| 32 | $day = $matches[3]; |
||
| 33 | } |
||
| 34 | |||
| 35 | if (preg_match("/(\d{2}):(\d{2}):(\d{2})/", $dateTime, $matches)) { |
||
| 36 | $seconds = ($matches[1] * 60 * 60 + $matches[2] * 60 + $matches[3]) / 86400; |
||
| 37 | } |
||
| 38 | |||
| 39 | if ("$year-$month-$day" == '1899-12-31' || "$year-$month-$day" == '1900-01-00') { |
||
| 40 | return $seconds; |
||
| 41 | } |
||
| 42 | if ("$year-$month-$day" == '1900-02-29') { |
||
| 43 | return 60 + $seconds; |
||
| 44 | } |
||
| 45 | $range = $year - $epoch; |
||
| 46 | // check leapDay |
||
| 47 | $leap = (new \DateTime($dateInput))->format('L'); |
||
| 48 | $mDays = [31, ($leap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
||
| 49 | |||
| 50 | if (($year < 1900 || $year > 9999) || ($month < 1 || $month > 12) || $day < 1 || $day > $mDays[$month - 1]) { |
||
| 51 | return 0; |
||
| 52 | } |
||
| 53 | |||
| 54 | $days = $day + ($range * 365) + array_sum(array_slice($mDays, 0, $month - 1)); |
||
| 55 | $days += intval(($range) / 4) - intval(($range + $offset) / 100); |
||
| 56 | $days += intval(($range + $offset + $norm) / 400) - intval($leap); |
||
| 57 | if ($days > 59) { |
||
| 58 | $days++; |
||
| 59 | } |
||
| 60 | |||
| 61 | return $days + $seconds; |
||
| 62 | } |
||
| 63 | |||
| 144 |