dragonmantank / cron-expression

CRON for PHP: Calculate the next or previous run date and determine if a CRON expression is due
MIT License
4.57k stars 124 forks source link

Fix getPreviousRunDate return value for cron expression with both day-of-month and day-of-week set #121

Closed olafkryus closed 2 years ago

olafkryus commented 3 years ago

In \Cron\CronExpression::getRunDate method, cron expressions with both day-of-month and day-of-week set are detected with special if (isset($parts[2]) && isset($parts[4])) check, and treated differently than other patterns. In this execution branch, the expression is treated as two separate expressions with wildcards instead of one of the dwo day parameters. The results are merged and sorted, and after that the corresponding $nth result is returned.

However, this method is used internally by both getNextRunDate and getPreviousRunDate methods of the class, and yet the sorting order is incorrectly the same in both cases, irrespective of the $invert parameter passed to the method.

This pull request fixes the bug and adds a test to prevent a future regression.

dragonmantank commented 2 years ago

Thanks!