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