| Conditions | 9 |
| Paths | 6 |
| Total Lines | 60 |
| Code Lines | 32 |
| 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 |
||
| 102 | public function toUnixTime(Conversion $conversion) |
||
| 103 | { |
||
| 104 | $input = $conversion->getTo(); |
||
| 105 | |||
| 106 | if ( |
||
| 107 | !$input instanceof DateFragmentedRepresentationInterface |
||
| 108 | || !$input instanceof DateSolarRepresentationInterface |
||
| 109 | ) { |
||
| 110 | return; |
||
| 111 | } |
||
| 112 | |||
| 113 | if (null !== $input->getDayIndex() && null !== $input->getYear()) { |
||
| 114 | return ; |
||
| 115 | } |
||
| 116 | |||
| 117 | $year = $input->getDateParts()->getTransversal(0); |
||
| 118 | $weekIndex = $input->getDateParts()->getTransversal(1); |
||
| 119 | $dayOfWeek = $input->getDateParts()->getTransversal(2); |
||
| 120 | |||
| 121 | if (null === $year || null === $weekIndex) { |
||
| 122 | // Too imprecise to be worth |
||
| 123 | return; |
||
| 124 | } |
||
| 125 | |||
| 126 | $startingEraDayIndex = $this->calculator->getYearEraDayIndex($year); |
||
| 127 | |||
| 128 | // DoW of the first day of year |
||
| 129 | $dow = ($startingEraDayIndex + $this->firstYearDayIndex) % 7; |
||
| 130 | |||
| 131 | // walk until first thursday |
||
| 132 | $eraDayIndex = $startingEraDayIndex + (10 - $dow) % 7; |
||
| 133 | |||
| 134 | // Now eraDayIndex points on the thursday of the week #0. Lets go to the |
||
| 135 | // weekIndex, and move to the dayOfWeek |
||
| 136 | $eraDayIndex += $weekIndex * 7 + $dayOfWeek - 3; |
||
| 137 | |||
| 138 | $dayIndex = $eraDayIndex - $startingEraDayIndex; |
||
| 139 | |||
| 140 | if ($dayIndex < 0) { |
||
| 141 | $year--; |
||
| 142 | $eraDayIndex += $this->calculator->getYearLength($year); |
||
| 143 | $dayIndex = $eraDayIndex - $startingEraDayIndex; |
||
| 144 | } else { |
||
| 145 | $yl = $this->calculator->getYearLength($year); |
||
| 146 | |||
| 147 | if ($dayIndex >= $yl) { |
||
| 148 | $year++; |
||
| 149 | $dayIndex -= $yl; |
||
| 150 | $eraDayIndex += $yl; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | $isLeapYear = $this->calculator->isLeapYear($year); |
||
| 155 | |||
| 156 | $input = $input |
||
| 157 | ->withYear($year, $isLeapYear) |
||
| 158 | ->withDayIndex($dayIndex, $eraDayIndex) |
||
| 159 | ; |
||
| 160 | |||
| 161 | $conversion->setTo($input); |
||
| 162 | } |
||
| 164 |