Conditions | 29 |
Paths | 8624 |
Total Lines | 96 |
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 |
||
182 | public function retrieve($pattern = null, $toDate = null, $fromDate = null, $limit = null, $dontClip = false, $countMeta = false) |
||
183 | { |
||
184 | // test if we got a pattern, if not match against all |
||
185 | if (is_null($pattern)) { |
||
186 | $pattern = '*'; |
||
187 | } |
||
188 | // test if we got some dates to filter by |
||
189 | if (is_null($toDate)) { |
||
190 | $toDate = 9999999999; |
||
191 | } |
||
192 | if (is_null($fromDate)) { |
||
193 | $fromDate = 0; |
||
194 | } |
||
195 | |||
196 | // get the raw entries |
||
197 | $this->lastRetrieve = $this->getStorageContent(); |
||
198 | $rawEntries = explode(self::LINE_BREAK, rtrim($this->lastRetrieve, self::LINE_BREAK)); |
||
199 | |||
200 | $entries = array(); |
||
201 | |||
202 | // iterate them and generate the entities |
||
203 | $bookingKey = null; |
||
204 | $bookingCount = 0; |
||
205 | foreach ($rawEntries as $key => $rawEntry) { |
||
206 | // get the potential entry and filter them by ticket ID |
||
207 | $entry = explode(self::SEPARATOR, trim($rawEntry, ' |')); |
||
208 | $timestamp = strtotime($entry[0]); |
||
209 | if (isset($entry[1]) && |
||
210 | (fnmatch($pattern, $entry[1]) || isset(BookingFactory::getAllMetaTicketIds()[$entry[1]])) && |
||
211 | $timestamp > $fromDate && $timestamp < $toDate |
||
212 | ) { |
||
213 | // collect the actual booking |
||
214 | $comment = isset($entry[2]) ? $entry[2] : ''; |
||
215 | $pushed = isset($entry[3]) && !empty($entry[3]) ? true : false; |
||
216 | $booking = BookingFactory::getBooking($comment, $entry[1], $entry[0], $pushed); |
||
217 | $entries[] = $booking; |
||
218 | |||
219 | // increase the booking counter |
||
220 | if (!$booking->isMetaBooking() || $countMeta) { |
||
221 | $bookingCount ++; |
||
222 | } |
||
223 | |||
224 | // if clipping is not omitted we will add the rear clipping to our collection. |
||
225 | // We do it here to make sure we get the correct day |
||
226 | if (count($entries) === 1 && !$dontClip) { |
||
227 | // test if the last booking is from the today, if not we have to clip at the end of the last booked day |
||
228 | $bookingTime = new \DateTime($booking->getTime()); |
||
229 | $now = new \DateTime(); |
||
230 | $interval = $bookingTime->diff($now); |
||
231 | if ($interval->days === 0) { |
||
232 | $entries[] = BookingFactory::getBooking('', Clipping::CLIPPING_TAG_REAR); |
||
233 | } else { |
||
234 | $entries[] = BookingFactory::getBooking('', Clipping::CLIPPING_TAG_REAR, date('Y-m-d', strtotime($booking->getTime()) + 24 * 60 * 60)); |
||
235 | } |
||
236 | // reverse entries array to let it start with our clipping again |
||
237 | $entries = array_reverse($entries); |
||
238 | } |
||
239 | |||
240 | // collect keys we found something for, for later re-use |
||
241 | $bookingKey = $key; |
||
242 | |||
243 | // break if we got as much bookings as our limit is |
||
244 | if (!is_null($limit) && $limit <= $bookingCount) { |
||
245 | break; |
||
246 | } |
||
247 | } |
||
248 | } |
||
249 | |||
250 | // entries still empty? Then we can quit here |
||
251 | if (empty($entries)) { |
||
252 | return $entries; |
||
253 | } |
||
254 | |||
255 | // clip the front, but only if we filter by from date |
||
256 | if (!$dontClip && $fromDate !== 0) { |
||
257 | $entries[] = BookingFactory::getBooking('', Clipping::CLIPPING_TAG_FRONT, $fromDate); |
||
258 | |||
259 | // move some bookings into the past to get the startbooking of a potential task we might need |
||
260 | for ($i = $bookingKey + 1; $i < count($rawEntries); $i++) { |
||
|
|||
261 | if (empty(trim($rawEntries[$i]))) { |
||
262 | continue; |
||
263 | } |
||
264 | $entry = explode(self::SEPARATOR, trim($rawEntries[$i], ' |')); |
||
265 | $comment = isset($entry[2]) ? $entry[2] : ''; |
||
266 | $pushed = isset($entry[3]) && !empty($entry[3]) ? true : false; |
||
267 | $booking = BookingFactory::getBooking($comment, $entry[1], $entry[0], $pushed); |
||
268 | $entries[] = $booking; |
||
269 | // break after the first non-meta booking |
||
270 | if (!$booking->isMetaBooking()) { |
||
271 | break; |
||
272 | } |
||
273 | } |
||
274 | } |
||
275 | |||
276 | return $entries; |
||
277 | } |
||
278 | } |
||
279 |
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: