| Conditions | 9 |
| Paths | 11 |
| Total Lines | 61 |
| Code Lines | 44 |
| 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 |
||
| 142 | protected function intervals(array $params): array |
||
| 143 | { |
||
| 144 | $year = Hash::get($params, 'year'); |
||
| 145 | $month = Hash::get($params, 'month'); |
||
| 146 | $week = Hash::get($params, 'week'); |
||
| 147 | $day = Hash::get($params, 'day'); |
||
| 148 | // case day: return interval with just one day |
||
| 149 | if ($day !== null) { |
||
| 150 | $start = new FrozenDate($day); |
||
| 151 | $end = $start->addDays(1); |
||
| 152 | |||
| 153 | return [['start' => $start->format('Y-m-d'), 'end' => $end->format('Y-m-d')]]; |
||
| 154 | } |
||
| 155 | // case week: return interval with all days of the week, ~ 7 days |
||
| 156 | if ($week !== null) { |
||
| 157 | $firstWeek = intval($week); |
||
| 158 | $lastWeek = intval($week); |
||
| 159 | $start = new FrozenDate(sprintf('first day of %s', $month)); |
||
| 160 | $start = $start->addWeeks($firstWeek - 1); |
||
| 161 | $end = $start->addWeeks(1)->subDays(1); |
||
| 162 | $intervals = []; |
||
| 163 | while ($start->lessThanOrEquals($end)) { |
||
| 164 | $next = $start->addDays(1); |
||
| 165 | $intervals[] = ['start' => $start->format('Y-m-d'), 'end' => $next->format('Y-m-d')]; |
||
| 166 | $start = $next; |
||
| 167 | } |
||
| 168 | |||
| 169 | return $intervals; |
||
| 170 | } |
||
| 171 | // case month: return interval with 4/5 weeks |
||
| 172 | if ($month !== null) { |
||
| 173 | $firstWeek = 1; |
||
| 174 | $defaultLastWeek = $month === 'february' ? 4 : 5; |
||
| 175 | $lastWeek = $defaultLastWeek; |
||
| 176 | $start = new FrozenDate(sprintf('first day of %s %s', $month, $year)); |
||
| 177 | $start = $start->addWeeks($firstWeek - 1); |
||
| 178 | $end = $start->addWeeks($lastWeek)->subDays(1); |
||
| 179 | $intervals = []; |
||
| 180 | while ($start->lessThanOrEquals($end)) { |
||
| 181 | $next = $start->addWeeks(1); |
||
| 182 | if ($next->format('m') !== $start->format('m')) { |
||
| 183 | $end = new FrozenDate(sprintf('last day of %s %s', $month, $year)); |
||
| 184 | $next = $end->addDays(1); |
||
| 185 | } |
||
| 186 | $intervals[] = ['start' => $start->format('Y-m-d'), 'end' => $next->format('Y-m-d')]; |
||
| 187 | $start = $next; |
||
| 188 | } |
||
| 189 | |||
| 190 | return $intervals; |
||
| 191 | } |
||
| 192 | // case year: return interval with 12 months |
||
| 193 | $start = new FrozenDate(sprintf('first day of january %s', $year)); |
||
| 194 | $end = new FrozenDate(sprintf('last day of december %s', $year)); |
||
| 195 | $intervals = []; |
||
| 196 | while ($start->lessThanOrEquals($end)) { |
||
| 197 | $next = $start->addMonths(1); |
||
| 198 | $intervals[] = ['start' => $start->format('Y-m-d'), 'end' => $next->format('Y-m-d')]; |
||
| 199 | $start = $next; |
||
| 200 | } |
||
| 201 | |||
| 202 | return $intervals; |
||
| 203 | } |
||
| 205 |