| Conditions | 23 |
| Paths | 832 |
| Total Lines | 86 |
| Code Lines | 45 |
| 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 |
||
| 142 | public function retrieve($pattern = null, $toDate = null, $fromDate = null, $limit = null, $dontClip = false, $countMeta = false) |
||
| 143 | { |
||
| 144 | // test if we got a pattern, if not match against all |
||
| 145 | if (is_null($pattern)) { |
||
| 146 | $pattern = '*'; |
||
| 147 | } |
||
| 148 | // test if we got some dates to filter by |
||
| 149 | if (is_null($toDate)) { |
||
| 150 | $toDate = 9999999999; |
||
| 151 | } |
||
| 152 | if (is_null($fromDate)) { |
||
| 153 | $fromDate = 0; |
||
| 154 | } |
||
| 155 | |||
| 156 | // get the raw entries |
||
| 157 | $rawData = $this->getStorageContent(); |
||
| 158 | $rawEntries = explode(self::LINE_BREAK, rtrim($rawData, self::LINE_BREAK)); |
||
| 159 | |||
| 160 | $entries = array(); |
||
| 161 | |||
| 162 | // iterate them and generate the entities |
||
| 163 | $bookingKey = null; |
||
| 164 | $bookingCount = 0; |
||
| 165 | foreach ($rawEntries as $key => $rawEntry) { |
||
| 166 | // get the potential entry and filter them by ticket ID |
||
| 167 | $entry = explode(self::SEPARATOR, trim($rawEntry, ' |')); |
||
| 168 | $timestamp = strtotime($entry[0]); |
||
| 169 | if (isset($entry[1]) && |
||
| 170 | (fnmatch($pattern, $entry[1]) || isset(BookingFactory::getAllMetaTicketIds()[$entry[1]])) && |
||
| 171 | $timestamp > $fromDate && $timestamp < $toDate |
||
| 172 | ) { |
||
| 173 | // collect the actual booking |
||
| 174 | $comment = isset($entry[2]) ? $entry[2] : ''; |
||
| 175 | $booking = BookingFactory::getBooking($comment, $entry[1], $entry[0]); |
||
| 176 | $entries[] = $booking; |
||
| 177 | |||
| 178 | // increase the booking counter |
||
| 179 | if (!$booking->isMetaBooking() || $countMeta) { |
||
| 180 | $bookingCount ++; |
||
| 181 | } |
||
| 182 | |||
| 183 | // if clipping is not omitted we will add the rear clipping to our collection. |
||
| 184 | // We do it here to make sure we get the correct day |
||
| 185 | if (count($entries) === 1 && !$dontClip) { |
||
| 186 | // test if the last booking is from the today, if not we have to clip at the end of the last booked day |
||
| 187 | $bookingTime = new \DateTime($booking->getTime()); |
||
| 188 | $now = new \DateTime(); |
||
| 189 | $interval = $bookingTime->diff($now); |
||
| 190 | if ($interval->days === 0) { |
||
| 191 | $entries[] = BookingFactory::getBooking('', Clipping::CLIPPING_TAG_REAR); |
||
| 192 | } else { |
||
| 193 | $entries[] = BookingFactory::getBooking('', Clipping::CLIPPING_TAG_REAR, date('Y-m-d', strtotime($booking->getTime()) + 24 * 60 * 60)); |
||
| 194 | } |
||
| 195 | // reverse entries array to let it start with our clipping again |
||
| 196 | $entries = array_reverse($entries); |
||
| 197 | } |
||
| 198 | |||
| 199 | // collect keys we found something for, for later re-use |
||
| 200 | $bookingKey = $key; |
||
| 201 | |||
| 202 | // break if we got as much bookings as our limit is |
||
| 203 | if (!is_null($limit) && $limit <= $bookingCount) { |
||
| 204 | break; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | // clip the front, but only if we filter by from date |
||
| 210 | if (!$dontClip && $fromDate !== 0) { |
||
| 211 | $entries[] = BookingFactory::getBooking('', Clipping::CLIPPING_TAG_FRONT, $fromDate); |
||
| 212 | |||
| 213 | // move some bookings into the past to get the startbooking of a potential task we might need |
||
| 214 | for ($i = $bookingKey + 1; $i < count($rawEntries); $i++) { |
||
|
|
|||
| 215 | $entry = explode(self::SEPARATOR, trim($rawEntries[$i], ' |')); |
||
| 216 | $comment = isset($entry[2]) ? $entry[2] : ''; |
||
| 217 | $booking = BookingFactory::getBooking($comment, $entry[1], $entry[0]); |
||
| 218 | $entries[] = $booking; |
||
| 219 | // break after the first non-meta booking |
||
| 220 | if (!$booking->isMetaBooking()) { |
||
| 221 | break; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | return $entries; |
||
| 227 | } |
||
| 228 | } |
||
| 229 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: