Conditions | 48 |
Paths | > 20000 |
Total Lines | 222 |
Code Lines | 180 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
145 | public function setSchedule($schedule) |
||
146 | { |
||
147 | if ($schedule === '') { |
||
148 | $schedule = '* * * * * *'; |
||
149 | } elseif ($schedule === null) { |
||
150 | $this->_schedule = $schedule; |
||
151 | return; |
||
152 | } |
||
153 | $schedule = trim($schedule); |
||
154 | $this->_schedule = $schedule; |
||
155 | $this->_attr = []; |
||
156 | if (strlen($schedule) > 1 && $schedule[0] == '@') { |
||
157 | if (is_numeric($triggerTime = substr($schedule, 1))) { |
||
158 | $this->_triggerTime = (int) $triggerTime; |
||
159 | return; |
||
160 | } |
||
161 | } |
||
162 | |||
163 | if (self::$_validatorCache) { |
||
164 | $minuteValidator = self::$_validatorCache['m']; |
||
165 | $hourValidator = self::$_validatorCache['h']; |
||
166 | $domValidator = self::$_validatorCache['dom']; |
||
167 | $monthValidator = self::$_validatorCache['mo']; |
||
168 | $dowValidator = self::$_validatorCache['dow']; |
||
169 | $yearValidator = self::$_validatorCache['y']; |
||
170 | $fullValidator = self::$_validatorCache['f']; |
||
171 | } else { |
||
172 | $minute = '(?:[0-9]|[1-5][0-9])'; |
||
173 | $minuteStar = '\*(?:\/(?:[1-9]|[1-5][0-9]))?'; |
||
174 | $minuteRegex = $minute . '(?:\-(?:[1-9]|[1-5][0-9]))?(?:\/(?:[1-9]|[1-5][0-9]))?'; |
||
175 | $hour = '(?:[0-9]|1[0-9]|2[0-3])'; |
||
176 | $hourStar = '\*(?:\/(?:[1-9]|1[0-9]|2[0-3]))?'; |
||
177 | $hourRegex = $hour . '(?:\-(?:[1-9]|1[0-9]|2[0-3]))?(?:\/(?:[1-9]|1[0-9]|2[0-3]))?'; |
||
178 | $dom = '(?:(?:[1-9]|[12][0-9]|3[01])W?)'; |
||
179 | $domWOW = '(?:[1-9]|[12][0-9]|3[01])'; |
||
180 | $domStar = '\*(?:\/(?:[1-9]|[12][0-9]|3[01]))?'; |
||
181 | $domRegex = '(?:' . $dom . '(?:\-' . $dom . ')?(?:\/' . $domWOW . ')?|' . '(?:L(?:\-[1-5])?)' . ')'; |
||
182 | $month = '(?:[1-9]|1[012]|' . |
||
183 | self::$_keywords[self::MONTH_OF_YEAR][1] . '|' . |
||
184 | self::$_keywords[self::MONTH_OF_YEAR][2] . '|' . |
||
185 | self::$_keywords[self::MONTH_OF_YEAR][3] . '|' . |
||
186 | self::$_keywords[self::MONTH_OF_YEAR][4] . '|' . |
||
187 | self::$_keywords[self::MONTH_OF_YEAR][5] . '|' . |
||
188 | self::$_keywords[self::MONTH_OF_YEAR][6] . '|' . |
||
189 | self::$_keywords[self::MONTH_OF_YEAR][7] . '|' . |
||
190 | self::$_keywords[self::MONTH_OF_YEAR][8] . '|' . |
||
191 | self::$_keywords[self::MONTH_OF_YEAR][9] . '|' . |
||
192 | self::$_keywords[self::MONTH_OF_YEAR][10] . '|' . |
||
193 | self::$_keywords[self::MONTH_OF_YEAR][11] . '|' . |
||
194 | self::$_keywords[self::MONTH_OF_YEAR][12] . ')'; |
||
195 | $monthStar = '\*(?:\/(?:[1-9]|1[012]))?'; |
||
196 | $monthRegex = $month . '(?:\-' . $month . ')?(?:\/(?:[1-9]|1[012]))?'; |
||
197 | $dow = '(?:[0-6]|' . |
||
198 | self::$_keywords[self::DAY_OF_WEEK][0] . '|' . |
||
199 | self::$_keywords[self::DAY_OF_WEEK][1] . '|' . |
||
200 | self::$_keywords[self::DAY_OF_WEEK][2] . '|' . |
||
201 | self::$_keywords[self::DAY_OF_WEEK][3] . '|' . |
||
202 | self::$_keywords[self::DAY_OF_WEEK][4] . '|' . |
||
203 | self::$_keywords[self::DAY_OF_WEEK][5] . '|' . |
||
204 | self::$_keywords[self::DAY_OF_WEEK][6] . ')'; |
||
205 | $dowStar = '\*(?:\/[0-6])?'; |
||
206 | $dowRegex = '(?:[0-6]L|' . $dow . '(?:(?:\-' . $dow . ')?(?:\/[0-6])?|#[1-5])?)'; |
||
207 | $year = '(?:19[7-9][0-9]|20[0-9][0-9])'; |
||
208 | $yearStar = '\*(?:\/[0-9]?[0-9])?'; |
||
209 | $yearRegex = $year . '(?:\-' . $year . ')?(?:\/[0-9]?[0-9])?'; |
||
210 | $fullValidator = '/^(?:(@(?:annually|yearly|monthly|weekly|daily|hourly))|' . |
||
211 | '(?#minute)((?:' . $minuteStar . '|' . $minuteRegex . ')(?:\,(?:' . $minuteStar . '|' . $minuteRegex . '))*)[\s]+' . |
||
212 | '(?#hour)((?:' . $hourStar . '|' . $hourRegex . ')(?:\,(?:' . $hourStar . '|' . $hourRegex . '))*)[\s]+' . |
||
213 | '(?#DoM)(\?|(?:(?:' . $domStar . '|' . $domRegex . ')(?:,(?:' . $domStar . '|' . $domRegex . '))*))[\s]+' . |
||
214 | '(?#month)((?:' . $monthStar . '|' . $monthRegex . ')(?:\,(?:' . $monthStar . '|' . $monthRegex . '))*)[\s]+' . |
||
215 | '(?#DoW)(\?|(?:' . $dowStar . '|' . $dowRegex . ')(?:\,(?:' . $dowStar . '|' . $dowRegex . '))*)' . |
||
216 | '(?#year)(?:[\s]+' . |
||
217 | '((?:' . $yearStar . '|' . $yearRegex . ')(?:\,(?:' . $yearStar . '|' . $yearRegex . '))*)' . |
||
218 | ')?' . |
||
219 | ')$/i'; |
||
220 | |||
221 | $minuteValidator = '/^(\*|' . $minute . ')(?:\-(' . $minute . '))?(?:\/(' . $minute . '))?$/i'; |
||
222 | $hourValidator = '/^(\*|' . $hour . ')(?:\-(' . $hour . '))?(?:\/(' . $hour . '))?$/i'; |
||
223 | $domValidator = '/^(\*|\?|L|' . $domWOW . ')(W)?(?:\-(' . $domWOW . ')(W)?)?(?:\/(' . $domWOW . '))?$/i'; |
||
224 | $monthValidator = '/^(\*|' . $month . ')(?:\-(' . $month . '))?(?:\/(' . $month . '))?$/i'; |
||
225 | $dowValidator = '/^(\*|\?|' . $dow . ')(L)?(?:\-(' . $dow . ')(L)?)?(?:\/(' . $dow . '))?(?:#([1-5]))?$/i'; |
||
226 | $yearValidator = '/^(\*|' . $year . ')(?:\-(' . $year . '))?(?:\/([1-9]?[0-9]))?$/i'; |
||
227 | self::$_validatorCache = [ |
||
228 | 'm' => $minuteValidator, |
||
229 | 'h' => $hourValidator, |
||
230 | 'dom' => $domValidator, |
||
231 | 'mo' => $monthValidator, |
||
232 | 'dow' => $dowValidator, |
||
233 | 'y' => $yearValidator, |
||
234 | 'f' => $fullValidator, |
||
235 | ]; |
||
236 | } |
||
237 | |||
238 | $i = 0; |
||
|
|||
239 | do { |
||
240 | if (!preg_match($fullValidator, $schedule, $matches)) { |
||
241 | throw new TInvalidDataValueException('timescheduler_invalid_string', $schedule); |
||
242 | } |
||
243 | if ($matches[1]) { |
||
244 | foreach (self::$_intervals as $interval => $intervalSchedule) { |
||
245 | if ($interval == $matches[1]) { |
||
246 | $schedule = $intervalSchedule; |
||
247 | } |
||
248 | } |
||
249 | } |
||
250 | } while ($matches[1]); |
||
251 | |||
252 | $this->_attr[self::MINUTE] = []; |
||
253 | foreach (explode(',', $matches[2]) as $match) { |
||
254 | if (preg_match($minuteValidator, $match, $m2)) { |
||
255 | if ($m2[1] === '*') { |
||
256 | $data = ['min' => 0, 'end' => 59]; |
||
257 | } else { |
||
258 | $data = ['min' => $m2[1]]; |
||
259 | $data['end'] = $m2[2] ?? $data['min']; |
||
260 | } |
||
261 | $data['period'] = (int) max($m2[3] ?? 1, 1); |
||
262 | if (!($m2[2] ?? 0) && ($m2[3] ?? 0)) { |
||
263 | $data['end'] = 59; //No end with period |
||
264 | } |
||
265 | $this->_attr[self::MINUTE][] = $data; |
||
266 | } else { |
||
267 | throw new TInvalidDataValueException('timescheduler_invalid_string', $match); |
||
268 | } |
||
269 | } |
||
270 | |||
271 | $this->_attr[self::HOUR] = []; |
||
272 | foreach (explode(',', $matches[3]) as $match) { |
||
273 | if (preg_match($hourValidator, $match, $m2)) { |
||
274 | if ($m2[1] === '*') { |
||
275 | $data = ['hour' => 0, 'end' => 23]; |
||
276 | } else { |
||
277 | $data = ['hour' => $m2[1]]; |
||
278 | $data['end'] = $m2[2] ?? $m2[1]; |
||
279 | } |
||
280 | $data['period'] = (int) max($m2[3] ?? 1, 1); |
||
281 | if (!($m2[2] ?? 0) && ($m2[3] ?? 0)) { |
||
282 | $data['end'] = 23; //No end with period |
||
283 | } |
||
284 | $this->_attr[self::HOUR][] = $data; |
||
285 | } else { |
||
286 | throw new TInvalidDataValueException('timescheduler_invalid_string', $match); |
||
287 | } |
||
288 | } |
||
289 | |||
290 | $this->_attr[self::DAY_OF_MONTH] = []; |
||
291 | foreach (explode(',', $matches[4]) as $match) { |
||
292 | if (preg_match($domValidator, $match, $m2)) { |
||
293 | $data = ['dom' => $m2[1]]; // *, ?, \d, L |
||
294 | $data['domWeekday'] = $m2[2] ?? null; |
||
295 | $data['end'] = $m2[3] ?? ($m2[1] != 'L' ? $m2[1] : null); |
||
296 | //$data['endWeekday'] = $m2[4] ?? null; |
||
297 | $data['endWeekday'] = $m2[4] ?? (($m2[3] ?? null) ? null : $data['domWeekday']); |
||
298 | $data['period'] = (int) max($m2[5] ?? 1, 1); |
||
299 | if (!($m2[3] ?? 0) && ($m2[5] ?? 0)) { |
||
300 | $data['end'] = 31; //No end with period |
||
301 | } |
||
302 | $this->_attr[self::DAY_OF_MONTH][] = $data; |
||
303 | } else { |
||
304 | throw new TInvalidDataValueException('timescheduler_invalid_string', $match); |
||
305 | } |
||
306 | } |
||
307 | |||
308 | $this->_attr[self::MONTH_OF_YEAR] = []; |
||
309 | foreach (explode(',', $matches[5]) as $match) { |
||
310 | if (preg_match($monthValidator, $match, $m2)) { |
||
311 | if ($m2[1] === '*') { |
||
312 | $data = ['moy' => 1, 'end' => 12]; |
||
313 | } else { |
||
314 | $data = ['moy' => (int) $this->translateMonthOfYear($m2[1])]; |
||
315 | $data['end'] = (isset($m2[2]) && $m2[2]) ? (int) $this->translateMonthOfYear($m2[2]) : $data['moy']; |
||
316 | } |
||
317 | $data['period'] = (int) max($m2[3] ?? 1, 1); |
||
318 | if (!($m2[2] ?? 0) && ($m2[3] ?? 0)) { |
||
319 | $data['end'] = 12; //No end with period |
||
320 | } |
||
321 | $this->_attr[self::MONTH_OF_YEAR][] = $data; |
||
322 | } else { |
||
323 | throw new TInvalidDataValueException('timescheduler_invalid_string', $match); |
||
324 | } |
||
325 | } |
||
326 | |||
327 | $this->_attr[self::DAY_OF_WEEK] = []; |
||
328 | foreach (explode(',', $matches[6]) as $match) { |
||
329 | if (preg_match($dowValidator, $match, $m2)) { |
||
330 | if ($m2[1] === '*' || $m2[1] === '?') { |
||
331 | $data = ['dow' => 0, 'end' => 6]; |
||
332 | } else { |
||
333 | $data = ['dow' => (int) $this->translateDayOfWeek($m2[1])]; |
||
334 | $data['end'] = (isset($m2[3]) && $m2[3]) ? (int) $this->translateDayOfWeek($m2[3]) : $data['dow']; |
||
335 | } |
||
336 | $data['lastDow'] = $m2[2] ?? null; |
||
337 | $data['lastEnd'] = $m2[4] ?? null; |
||
338 | $data['period'] = (int) max($m2[5] ?? 1, 1); |
||
339 | $data['week'] = $m2[6] ?? null; |
||
340 | if (!($m2[3] ?? 0) && ($m2[5] ?? 0)) { |
||
341 | $data['end'] = 6; //No end with period |
||
342 | } |
||
343 | $this->_attr[self::DAY_OF_WEEK][] = $data; |
||
344 | } else { |
||
345 | throw new TInvalidDataValueException('timescheduler_invalid_string', $match); |
||
346 | } |
||
347 | } |
||
348 | |||
349 | $this->_attr[self::YEAR] = []; |
||
350 | $matches[7] ??= '*'; |
||
351 | foreach (explode(',', $matches[7]) as $match) { |
||
352 | if (preg_match($yearValidator, $match, $m2)) { |
||
353 | if ($m2[1] === '*') { |
||
354 | $data = ['year' => self::YEAR_MIN]; |
||
355 | $data['end'] = self::YEAR_MAX; |
||
356 | } else { |
||
357 | $data = ['year' => $m2[1]]; |
||
358 | $data['end'] = $m2[2] ?? $m2[1]; |
||
359 | } |
||
360 | $data['period'] = max($m2[3] ?? 1, 1); |
||
361 | if (!($m2[2] ?? 0) && ($m2[3] ?? 0)) { |
||
362 | $data['end'] = self::YEAR_MAX; //No end with period |
||
363 | } |
||
364 | $this->_attr[self::YEAR][] = $data; |
||
365 | } else { |
||
366 | throw new TInvalidDataValueException('timescheduler_invalid_string', $match); |
||
367 | } |
||
758 |