| Conditions | 4 |
| Paths | 4 |
| Total Lines | 61 |
| Code Lines | 24 |
| 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 |
||
| 59 | public function fromUnixTime(Conversion $conversion) |
||
| 60 | { |
||
| 61 | if (!$conversion->getTo() instanceof DateSolarRepresentationInterface) { |
||
| 62 | return; |
||
| 63 | } |
||
| 64 | |||
| 65 | $res = $conversion->getTo(); |
||
| 66 | |||
| 67 | // TODO : remove code repetition by using a separate abstraction |
||
| 68 | // handling basic math operations. |
||
| 69 | |||
| 70 | if (is_integer($this->dayLengthInSeconds)) { |
||
| 71 | // bc not needed/not available |
||
| 72 | |||
| 73 | // Relative time from era start. |
||
| 74 | $relativeTime = $conversion->getUnixTime() - $this->eraStart; |
||
| 75 | |||
| 76 | // Calculating global day index. Floor is used to properly handle |
||
| 77 | // negative values |
||
| 78 | $eraDayIndex = (int)floor($relativeTime / $this->dayLengthInSeconds); |
||
| 79 | |||
| 80 | $time = $relativeTime - $eraDayIndex * $this->dayLengthInSeconds; |
||
| 81 | |||
| 82 | // TODO : handle the loss as microseconds ? |
||
| 83 | if (is_float($time)) { |
||
|
|
|||
| 84 | $time = (int)ceil($time); |
||
| 85 | } |
||
| 86 | |||
| 87 | $conversion->setUnixTime($time); |
||
| 88 | } else { |
||
| 89 | // Using bc math if available for non int dayLengthInSeconds in |
||
| 90 | // order to stay as precise as possible. |
||
| 91 | |||
| 92 | // Relative time from era start. |
||
| 93 | $relativeTime = bcsub($conversion->getUnixTime(), $this->eraStart); |
||
| 94 | |||
| 95 | // Calculating global day index. Floor is used to properly handle |
||
| 96 | // negative values |
||
| 97 | $eraDayIndex = floor( |
||
| 98 | bcdiv($relativeTime, $this->dayLengthInSeconds) |
||
| 99 | ); |
||
| 100 | |||
| 101 | // TODO : handle the loss as microseconds ? |
||
| 102 | $time = ceil(bcsub( |
||
| 103 | $relativeTime, |
||
| 104 | bcmul($eraDayIndex, $this->dayLengthInSeconds) |
||
| 105 | )); |
||
| 106 | |||
| 107 | $conversion->setUnixTime((int)$time); |
||
| 108 | } |
||
| 109 | |||
| 110 | list($year, $dayIndex) = $this->calculator |
||
| 111 | ->getYearAndDayIndexFromErayDayIndex($eraDayIndex) |
||
| 112 | ; |
||
| 113 | |||
| 114 | $res = $res |
||
| 115 | ->withYear($year, $this->calculator->isLeapYear($year)) |
||
| 116 | ->withDayIndex($dayIndex, $eraDayIndex) |
||
| 117 | ; |
||
| 118 | |||
| 119 | $conversion->setTo($res); |
||
| 120 | } |
||
| 145 |