Conditions | 20 |
Paths | 640 |
Total Lines | 75 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 8 | ||
Bugs | 1 | Features | 4 |
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 |
||
129 | public function retrieve($pattern = null, $toDate = null, $fromDate = null, $limit = null, $dontClip = false) |
||
130 | { |
||
131 | // test if we got a pattern, if not match against all |
||
132 | if (is_null($pattern)) { |
||
133 | $pattern = '*'; |
||
134 | } |
||
135 | // test if we got some dates to filter by |
||
136 | if (is_null($toDate)) { |
||
137 | $toDate = 9999999999; |
||
138 | } |
||
139 | if (is_null($fromDate)) { |
||
140 | $fromDate = 0; |
||
141 | } |
||
142 | |||
143 | // get the raw entries |
||
144 | $rawData = $this->getStorageContent(); |
||
145 | $rawEntries = explode(self::LINE_BREAK, rtrim($rawData, self::LINE_BREAK)); |
||
146 | |||
147 | $entries = array(); |
||
148 | |||
149 | // if clipping is not omitted we will add the rear clipping to our collection |
||
150 | if (!$dontClip) { |
||
151 | $entries[] = BookingFactory::getBooking('', Clipping::CLIPPING_TAG_REAR); |
||
152 | } |
||
153 | |||
154 | // iterate them and generate the entities |
||
155 | $bookingKey = null; |
||
156 | $bookingCount = 0; |
||
157 | foreach ($rawEntries as $key => $rawEntry) { |
||
158 | // get the potential entry and filter them by ticket ID |
||
159 | $entry = explode(self::SEPARATOR, trim($rawEntry, ' |')); |
||
160 | $timestamp = strtotime($entry[0]); |
||
161 | if (isset($entry[1]) && |
||
162 | (fnmatch($pattern, $entry[1]) || isset(BookingFactory::getAllMetaTicketIds()[$entry[1]])) && |
||
163 | $timestamp > $fromDate && $timestamp < $toDate |
||
164 | ) { |
||
165 | // collect the actual booking |
||
166 | $comment = isset($entry[2]) ? $entry[2] : ''; |
||
167 | $booking = BookingFactory::getBooking($comment, $entry[1], $entry[0]); |
||
168 | $entries[] = $booking; |
||
169 | |||
170 | // increase the booking counter |
||
171 | if (!$booking->isMetaBooking()) { |
||
172 | $bookingCount ++; |
||
173 | } |
||
174 | |||
175 | // collect keys we found something for, for later re-use |
||
176 | $bookingKey = $key; |
||
177 | |||
178 | // break if we got as much bookings as our limit is |
||
179 | if (!is_null($limit) && $limit <= $bookingCount) { |
||
180 | break; |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | |||
185 | // clip the front, but only if we filter by from date |
||
186 | if (!$dontClip && $fromDate !== 0) { |
||
187 | $entries[] = BookingFactory::getBooking('', Clipping::CLIPPING_TAG_FRONT, $fromDate); |
||
188 | |||
189 | // move some bookings into the past to get the startbooking of a potential task we might need |
||
190 | for ($i = $bookingKey + 1; $i < count($rawEntries); $i++) { |
||
|
|||
191 | $entry = explode(self::SEPARATOR, trim($rawEntries[$i], ' |')); |
||
192 | $comment = isset($entry[2]) ? $entry[2] : ''; |
||
193 | $booking = BookingFactory::getBooking($comment, $entry[1], $entry[0]); |
||
194 | $entries[] = $booking; |
||
195 | // break after the first non-meta booking |
||
196 | if (!$booking->isMetaBooking()) { |
||
197 | break; |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | |||
202 | return $entries; |
||
203 | } |
||
204 | } |
||
205 |
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: