| Conditions | 29 |
| Paths | 1280 |
| Total Lines | 124 |
| Code Lines | 88 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 75 | public function buildArray() |
||
| 76 | { |
||
| 77 | $results = array(); |
||
| 78 | |||
| 79 | $query = DealerShedulesQuery::create(); |
||
| 80 | |||
| 81 | $query->filterByPeriodNotNull(); |
||
| 82 | if ($this->getHidePast()) { |
||
| 83 | $query->filterByPeriodEnd(new \DateTime(), Criteria::GREATER_THAN); |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($id = $this->getId()) { |
||
| 87 | $query->filterById($id); |
||
| 88 | } |
||
| 89 | |||
| 90 | if ($day = $this->getDay()) { |
||
| 91 | $query->filterByDay($day); |
||
| 92 | } |
||
| 93 | |||
| 94 | if ($dealer_id = $this->getDealerId()) { |
||
| 95 | $query->filterByDealerId($dealer_id); |
||
| 96 | } |
||
| 97 | |||
| 98 | $query->filterByClosed($this->getClosed()); |
||
| 99 | |||
| 100 | foreach ($this->getOrder() as $order) { |
||
| 101 | switch ($order) { |
||
| 102 | case 'id': |
||
| 103 | $query->orderById(); |
||
| 104 | break; |
||
| 105 | case 'id-reverse': |
||
| 106 | $query->orderById(Criteria::DESC); |
||
| 107 | break; |
||
| 108 | case 'day': |
||
| 109 | $query->orderByDay(); |
||
| 110 | break; |
||
| 111 | case 'day-reverse': |
||
| 112 | $query->orderByDay(Criteria::DESC); |
||
| 113 | break; |
||
| 114 | case 'begin': |
||
| 115 | $query->orderByBegin(); |
||
| 116 | break; |
||
| 117 | case 'begin-reverse': |
||
| 118 | $query->orderByBegin(Criteria::DESC); |
||
| 119 | break; |
||
| 120 | case 'period-begin': |
||
| 121 | $query->orderByPeriodBegin(); |
||
| 122 | break; |
||
| 123 | case 'period-begin-reverse': |
||
| 124 | $query->orderByPeriodBegin(Criteria::DESC); |
||
| 125 | break; |
||
| 126 | default: |
||
| 127 | break; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($this->getMergeDay()) { |
||
| 132 | $query->orderByBegin(); |
||
| 133 | } |
||
| 134 | |||
| 135 | $dealerSchedules = $query->find(); |
||
| 136 | |||
| 137 | if ($this->getMergeDay()) { |
||
| 138 | $dealerCount = count($dealerSchedules); |
||
| 139 | |||
| 140 | for ($i = 0; $i < $dealerCount; $i++) { |
||
| 141 | |||
| 142 | if (isset($dealerSchedules[$i])) { |
||
| 143 | |||
| 144 | $formattedHours = null; |
||
| 145 | |||
| 146 | // if the next result has the same dates, same day, then concat the morning and afternoon hours |
||
| 147 | if ( |
||
| 148 | ($dealerSchedules[$i+1] !== null) |
||
| 149 | && ($dealerSchedules[$i]->getDay() == $dealerSchedules[$i+1]->getDay()) |
||
| 150 | && ($dealerSchedules[$i]->getDealerId() == $dealerSchedules[$i+1]->getDealerId()) |
||
| 151 | && ($dealerSchedules[$i]->getPeriodBegin() == $dealerSchedules[$i+1]->getPeriodBegin()) |
||
| 152 | && ($dealerSchedules[$i]->getPeriodEnd() == $dealerSchedules[$i+1]->getPeriodEnd()) |
||
| 153 | ) { |
||
| 154 | $end = $dealerSchedules[$i+1]->getEnd(); |
||
| 155 | if ($dealerSchedules[$i]->getEnd()->format('H\hi') === $dealerSchedules[$i+1]->getBegin()->format('H\hi')) { |
||
| 156 | $formattedHours = date_format($dealerSchedules[$i]->getBegin(), 'H\hi') . $this->getHourSeparator() . date_format($dealerSchedules[$i+1]->getEnd(), 'H\hi'); |
||
| 157 | } else { |
||
| 158 | $formattedHours = date_format($dealerSchedules[$i]->getBegin(), 'H\hi') . $this->getHourSeparator() . date_format($dealerSchedules[$i]->getEnd(), 'H\hi') . $this->getHalfDaySeparator() . date_format($dealerSchedules[$i+1]->getBegin(), 'H\hi') . $this->getHourSeparator() . date_format($dealerSchedules[$i+1]->getEnd(), 'H\hi'); |
||
| 159 | } |
||
| 160 | unset($dealerSchedules[$i+1]); |
||
| 161 | } else { |
||
| 162 | $end = $dealerSchedules[$i]->getEnd(); |
||
| 163 | if ($dealerSchedules[$i]->getBegin() && $dealerSchedules[$i]->getEnd()) { |
||
| 164 | $formattedHours = date_format($dealerSchedules[$i]->getBegin(), 'H\hi') . $this->getHourSeparator() . date_format($dealerSchedules[$i]->getEnd(), 'H\hi'); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | $results[] = array( |
||
| 169 | 'ID' => $dealerSchedules[$i]->getId(), |
||
| 170 | 'DEALER_ID' => $dealerSchedules[$i]->getDealerId(), |
||
| 171 | 'DAY' => $dealerSchedules[$i]->getDay(), |
||
| 172 | 'DAY_LABEL' => ($dealerSchedules[$i]->getDay() === null) ? null : $this->getDayLabel($dealerSchedules[$i]->getDay()), |
||
| 173 | 'PERIOD_BEGIN' => $dealerSchedules[$i]->getPeriodBegin(), |
||
| 174 | 'PERIOD_END' => $dealerSchedules[$i]->getPeriodEnd(), |
||
| 175 | 'BEGIN' => $dealerSchedules[$i]->getBegin(), |
||
| 176 | 'END' => $end, |
||
| 177 | 'FORMATTED_HOURS' => $formattedHours |
||
| 178 | ); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } else { |
||
| 182 | foreach ($dealerSchedules as $dealerSchedule) { |
||
| 183 | $results[] = array( |
||
| 184 | 'DEALER_ID' => $dealerSchedule->getDealerId(), |
||
| 185 | 'ID' => $dealerSchedule->getId(), |
||
| 186 | 'DAY' => $dealerSchedule->getDay(), |
||
| 187 | 'DAY_LABEL' => ($dealerSchedule->getDay() === null) ? null : $this->getDayLabel($dealerSchedule->getDay()), |
||
| 188 | 'BEGIN' => $dealerSchedule->getBegin(), |
||
| 189 | 'END' => $dealerSchedule->getEnd(), |
||
| 190 | 'PERIOD_BEGIN' => $dealerSchedule->getPeriodBegin(), |
||
| 191 | 'PERIOD_END' => $dealerSchedule->getPeriodEnd(), |
||
| 192 | 'FORMATTED_HOURS' => date_format($dealerSchedule->getBegin(), 'H\hi') . $this->getHourSeparator() . date_format($dealerSchedule->getEnd(), 'H\hi') |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | return $results; |
||
| 198 | } |
||
| 199 | |||
| 212 | } |