| Conditions | 13 |
| Paths | 16 |
| Total Lines | 34 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | 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 | $seconds = self::getSeconds($dateTime); |
||
| 35 | if ("$year-$month-$day" == '1899-12-31' || "$year-$month-$day" == '1900-01-00') { |
||
| 36 | return $seconds; |
||
| 37 | } |
||
| 38 | if ("$year-$month-$day" == '1900-02-29') { |
||
| 39 | return 60 + $seconds; |
||
| 40 | } |
||
| 41 | $range = $year - $epoch; |
||
| 42 | // check leapDay |
||
| 43 | $leap = (new \DateTime($dateInput))->format('L'); |
||
| 44 | $mDays = [31, ($leap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
||
| 45 | if (($year < $epoch || $year > 9999) || ($month < 1 || $month > 12) || $day < 1 || $day > $mDays[$month - 1]) { |
||
| 46 | return 0; |
||
| 47 | } |
||
| 48 | $days = $day + ($range * 365) + array_sum(array_slice($mDays, 0, $month - 1)); |
||
| 49 | $days += intval(($range) / 4) - intval(($range + $offset) / 100); |
||
| 50 | $days += intval(($range + $offset + $norm) / 400) - intval($leap); |
||
| 51 | if ($days > 59) { |
||
| 52 | $days++; |
||
| 53 | } |
||
| 54 | |||
| 55 | return $days + $seconds; |
||
| 56 | } |
||
| 57 | |||
| 154 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.