| Total Complexity | 58 |
| Total Lines | 412 |
| Duplicated Lines | 0 % |
| Changes | 31 | ||
| Bugs | 1 | Features | 1 |
Complex classes like CronExpression often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CronExpression, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class CronExpression |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Weekday name look-up table |
||
| 19 | */ |
||
| 20 | private const WEEKDAY_NAMES = [ |
||
| 21 | 'sun' => 0, |
||
| 22 | 'mon' => 1, |
||
| 23 | 'tue' => 2, |
||
| 24 | 'wed' => 3, |
||
| 25 | 'thu' => 4, |
||
| 26 | 'fri' => 5, |
||
| 27 | 'sat' => 6 |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Month name look-up table |
||
| 32 | */ |
||
| 33 | private const MONTH_NAMES = [ |
||
| 34 | 'jan' => 1, |
||
| 35 | 'feb' => 2, |
||
| 36 | 'mar' => 3, |
||
| 37 | 'apr' => 4, |
||
| 38 | 'may' => 5, |
||
| 39 | 'jun' => 6, |
||
| 40 | 'jul' => 7, |
||
| 41 | 'aug' => 8, |
||
| 42 | 'sep' => 9, |
||
| 43 | 'oct' => 10, |
||
| 44 | 'nov' => 11, |
||
| 45 | 'dec' => 12 |
||
| 46 | ]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Value boundaries |
||
| 50 | */ |
||
| 51 | private const VALUE_BOUNDARIES = [ |
||
| 52 | 0 => [ |
||
| 53 | 'min' => 0, |
||
| 54 | 'max' => 59, |
||
| 55 | 'mod' => 1 |
||
| 56 | ], |
||
| 57 | 1 => [ |
||
| 58 | 'min' => 0, |
||
| 59 | 'max' => 23, |
||
| 60 | 'mod' => 1 |
||
| 61 | ], |
||
| 62 | 2 => [ |
||
| 63 | 'min' => 1, |
||
| 64 | 'max' => 31, |
||
| 65 | 'mod' => 1 |
||
| 66 | ], |
||
| 67 | 3 => [ |
||
| 68 | 'min' => 1, |
||
| 69 | 'max' => 12, |
||
| 70 | 'mod' => 1 |
||
| 71 | ], |
||
| 72 | 4 => [ |
||
| 73 | 'min' => 0, |
||
| 74 | 'max' => 7, |
||
| 75 | 'mod' => 0 |
||
| 76 | ] |
||
| 77 | ]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Time zone |
||
| 81 | * |
||
| 82 | * @var DateTimeZone|null |
||
| 83 | */ |
||
| 84 | protected $timeZone = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Matching registers |
||
| 88 | * |
||
| 89 | * @var array|null |
||
| 90 | */ |
||
| 91 | protected $registers = null; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $expression a cron expression, e.g. "* * * * *" |
||
| 95 | * @param DateTimeZone|null $timeZone time zone object |
||
| 96 | */ |
||
| 97 | public function __construct(string $expression, DateTimeZone $timeZone = null) |
||
| 98 | { |
||
| 99 | $this->timeZone = $timeZone; |
||
| 100 | |||
| 101 | try { |
||
| 102 | $this->registers = $this->parse($expression); |
||
| 103 | } catch (Exception $e) { |
||
| 104 | $this->registers = null; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Whether current cron expression has been parsed successfully |
||
| 110 | * |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | public function isValid(): bool |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Match either "now", a given date/time object or a timestamp against current cron expression |
||
| 120 | * |
||
| 121 | * @param mixed $when a DateTime object, a timestamp (int), or "now" if not set |
||
| 122 | * @return bool |
||
| 123 | * @throws Exception |
||
| 124 | */ |
||
| 125 | public function isMatching($when = null): bool |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Calculate next matching timestamp |
||
| 140 | * |
||
| 141 | * @param mixed $start a DateTime object, a timestamp (int) or "now" if not set |
||
| 142 | * @return int|bool next matching timestamp, or false on error |
||
| 143 | * @throws Exception |
||
| 144 | */ |
||
| 145 | public function getNext($start = null) |
||
| 146 | { |
||
| 147 | if ($this->isValid()) { |
||
| 148 | $now = $this->toDateTime($start); |
||
| 149 | $pos = sscanf($now->format('i G j n Y w'), '%d %d %d %d %d %d'); |
||
| 150 | |||
| 151 | while ($this->increase($now, $pos)) { |
||
| 152 | $this->reset($now, $pos); |
||
| 153 | } |
||
| 154 | |||
| 155 | return $now->getTimestamp(); |
||
| 156 | } |
||
| 157 | |||
| 158 | return false; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param mixed $start a DateTime object, a timestamp (int) or "now" if not set |
||
| 163 | * @return DateTime |
||
| 164 | */ |
||
| 165 | private function toDateTime($start): DateTime |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Increases the timestamp in step sizes depending on which segment(s) of the cron pattern are matching. |
||
| 187 | * Returns FALSE if the cron pattern is matching and thus no further cycle is required. |
||
| 188 | * |
||
| 189 | * @param DateTimeInterface $now |
||
| 190 | * @param array $pos |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | private function increase(DateTimeInterface $now, array $pos): bool |
||
| 194 | { |
||
| 195 | if (isset($this->registers[3][$pos[3]]) === false) { |
||
| 196 | $now->modify('+1 month'); |
||
| 197 | return true; |
||
| 198 | } elseif (false === (isset($this->registers[2][$pos[2]]) && isset($this->registers[4][$pos[5]]))) { |
||
| 199 | $now->modify('+1 day'); |
||
| 200 | return true; |
||
| 201 | } elseif (isset($this->registers[0][$pos[0]]) === false) { |
||
| 202 | $now->modify('+1 minute'); |
||
| 203 | return true; |
||
| 204 | } elseif (isset($this->registers[1][$pos[1]]) === false) { |
||
| 205 | $now->modify('+1 hour'); |
||
| 206 | return true; |
||
| 207 | } |
||
| 208 | |||
| 209 | return false; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param DateTimeInterface $now |
||
| 214 | * @param array $pos |
||
| 215 | */ |
||
| 216 | private function reset(DateTimeInterface $now, array &$pos): void |
||
| 217 | { |
||
| 218 | $current = sscanf($now->format('i G j n Y w'), '%d %d %d %d %d %d'); |
||
| 219 | |||
| 220 | if ($pos[4] !== $current[4]) { |
||
| 221 | // next year, reset month/day/hour/minute |
||
| 222 | $now->setTime(0, 0); |
||
| 223 | $now->setDate($current[4], 1, 1); |
||
| 224 | } elseif ($pos[3] !== $current[3]) { |
||
| 225 | // next month, reset day/hour/minute |
||
| 226 | $now->setTime(0, 0); |
||
| 227 | $now->setDate($current[4], $current[3], 1); |
||
| 228 | } elseif ($pos[2] !== $current[2]) { |
||
| 229 | // next day, reset hour/minute |
||
| 230 | $now->setTime(0, 0); |
||
| 231 | } |
||
| 232 | |||
| 233 | $pos = sscanf($now->format('i G j n Y w'), '%d %d %d %d %d %d'); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param array $segments |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | private function match(array $segments): bool |
||
| 241 | { |
||
| 242 | $result = true; |
||
| 243 | |||
| 244 | foreach ($this->registers as $i => $item) { |
||
| 245 | if (isset($item[(int)$segments[$i]]) === false) { |
||
| 246 | $result = false; |
||
| 247 | break; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | return $result; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Parse whole cron expression |
||
| 256 | * |
||
| 257 | * @param string $expression |
||
| 258 | * @return array |
||
| 259 | * @throws Exception |
||
| 260 | */ |
||
| 261 | private function parse(string $expression): array |
||
| 262 | { |
||
| 263 | $segments = preg_split('/\s+/', trim($expression)); |
||
| 264 | |||
| 265 | if (is_array($segments) && sizeof($segments) === 5) { |
||
| 266 | $registers = array_fill(0, 5, []); |
||
| 267 | |||
| 268 | foreach ($segments as $index => $segment) { |
||
| 269 | $this->parseSegment($registers[$index], $index, $segment); |
||
| 270 | } |
||
| 271 | |||
| 272 | if (isset($registers[4][7])) { |
||
| 273 | $registers[4][0] = true; |
||
| 274 | } |
||
| 275 | |||
| 276 | return $registers; |
||
| 277 | } |
||
| 278 | |||
| 279 | throw new Exception('invalid number of segments'); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Parse one segment of a cron expression |
||
| 284 | * |
||
| 285 | * @param array $register |
||
| 286 | * @param int $index |
||
| 287 | * @param string $segment |
||
| 288 | * @throws Exception |
||
| 289 | */ |
||
| 290 | private function parseSegment(array &$register, $index, $segment): void |
||
| 291 | { |
||
| 292 | $allowed = [false, false, false, self::MONTH_NAMES, self::WEEKDAY_NAMES]; |
||
| 293 | |||
| 294 | // month names, weekdays |
||
| 295 | if ($allowed[$index] !== false && isset($allowed[$index][strtolower($segment)])) { |
||
| 296 | // cannot be used together with lists or ranges |
||
| 297 | $register[$allowed[$index][strtolower($segment)]] = true; |
||
| 298 | } else { |
||
| 299 | // split up current segment into single elements, e.g. "1,5-7,*/2" => [ "1", "5-7", "*/2" ] |
||
| 300 | foreach (explode(',', $segment) as $element) { |
||
| 301 | $this->parseElement($register, $index, $element); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param array $register |
||
| 308 | * @param int $index |
||
| 309 | * @param string $element |
||
| 310 | * @throws Exception |
||
| 311 | */ |
||
| 312 | private function parseElement(array &$register, int $index, string $element): void |
||
| 313 | { |
||
| 314 | $step = 1; |
||
| 315 | $segments = explode('/', $element); |
||
| 316 | |||
| 317 | if (sizeof($segments) > 1) { |
||
| 318 | $this->validateStepping($segments, $index); |
||
| 319 | |||
| 320 | $element = (string)$segments[0]; |
||
| 321 | $step = (int)$segments[1]; |
||
| 322 | } |
||
| 323 | |||
| 324 | if (is_numeric($element)) { |
||
| 325 | $this->validateValue($element, $index, $step); |
||
| 326 | $register[intval($element)] = true; |
||
| 327 | } else { |
||
| 328 | $this->parseRange($register, $index, $element, $step); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Parse range of values, e.g. "5-10" |
||
| 334 | * |
||
| 335 | * @param array $register |
||
| 336 | * @param int $index |
||
| 337 | * @param string $range |
||
| 338 | * @param int $stepping |
||
| 339 | * @throws Exception |
||
| 340 | */ |
||
| 341 | private function parseRange(array &$register, int $index, string $range, int $stepping): void |
||
| 342 | { |
||
| 343 | if ($range === '*') { |
||
| 344 | $range = [self::VALUE_BOUNDARIES[$index]['min'], self::VALUE_BOUNDARIES[$index]['max']]; |
||
| 345 | } else { |
||
| 346 | $range = explode('-', $range); |
||
| 347 | } |
||
| 348 | |||
| 349 | $this->validateRange($range, $index); |
||
| 350 | $this->fillRange($register, $index, $range, $stepping); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param array $register |
||
| 355 | * @param int $index |
||
| 356 | * @param array $range |
||
| 357 | * @param int $stepping |
||
| 358 | */ |
||
| 359 | private function fillRange(array &$register, int $index, array $range, int $stepping): void |
||
| 360 | { |
||
| 361 | $boundary = self::VALUE_BOUNDARIES[$index]['max'] + self::VALUE_BOUNDARIES[$index]['mod']; |
||
| 362 | $length = $range[1] - $range[0]; |
||
| 363 | |||
| 364 | if ($range[0] > $range[1]) { |
||
| 365 | $length += $boundary; |
||
| 366 | } |
||
| 367 | |||
| 368 | for ($i = 0; $i <= $length; $i += $stepping) { |
||
| 369 | $register[($range[0] + $i) % $boundary] = true; |
||
| 370 | } |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Validate whether a given range of values exceeds allowed value boundaries |
||
| 375 | * |
||
| 376 | * @param array $range |
||
| 377 | * @param int $index |
||
| 378 | * @throws Exception |
||
| 379 | */ |
||
| 380 | private function validateRange(array $range, int $index): void |
||
| 381 | { |
||
| 382 | if (sizeof($range) !== 2) { |
||
| 383 | throw new Exception('invalid range notation'); |
||
| 384 | } |
||
| 385 | |||
| 386 | foreach ($range as $value) { |
||
| 387 | $this->validateValue($value, $index); |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $value |
||
| 393 | * @param int $index |
||
| 394 | * @param int $step |
||
| 395 | * @throws Exception |
||
| 396 | */ |
||
| 397 | private function validateValue(string $value, int $index, int $step = 1): void |
||
| 398 | { |
||
| 399 | if ((string)$value !== (string)(int)$value) { |
||
| 400 | throw new Exception('non-integer value'); |
||
| 401 | } |
||
| 402 | |||
| 403 | if (intval($value) < self::VALUE_BOUNDARIES[$index]['min'] || |
||
| 404 | intval($value) > self::VALUE_BOUNDARIES[$index]['max'] |
||
| 405 | ) { |
||
| 406 | throw new Exception('value out of boundary'); |
||
| 407 | } |
||
| 408 | |||
| 409 | if ($step !== 1) { |
||
| 410 | throw new Exception('invalid combination of value and stepping notation'); |
||
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @param array $segments |
||
| 416 | * @param int $index |
||
| 417 | * @throws Exception |
||
| 418 | */ |
||
| 419 | private function validateStepping(array $segments, int $index): void |
||
| 427 | } |
||
| 428 | } |
||
| 429 | } |
||
| 430 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths