adhocore / php-cron-expr

Ultra lightweight, Dependency free and Super Fast Cron Expression parser for PHP
https://github.com/adhocore/php-cron-expr
MIT License
50 stars 5 forks source link

issue with range() #12

Closed avest closed 5 years ago

avest commented 5 years ago

There is an issue with the range() call in Validator.php.

you will get an error if you run: Expression::isDue('5/20 ', '2018-08-13 00:24:00');

Here is my fix:

public function inStep($value, $offset)
{
    if (\strpos($offset, '*/') !== false || \strpos($offset, '0/') !== false)
    {
        $parts = \explode('/', $offset, 2);

        return $value % $parts[1] === 0;
    }

    $parts = \explode('/', $offset, 2);

    $subparts = \explode('-', $parts[0], 2) + [1 => $value];

    $range_start = $subparts[0];

    $range_end = $subparts[1];

    $range_step = $parts[1];

    $range_max = $range_start + $range_step;

    $range = array($range_start);
    if ($range_end >= $range_max)
    {
        $range = \range($subparts[0], $subparts[1], $parts[1]);
    }

    $in_range = false;
    if ($range)
    {
        $in_range = \in_array($value, $range);
    }

    //return ($subparts[0] <= $value && $value <= $subparts[1] && $parts[1])
    //  ? \in_array($value, \range($subparts[0], $subparts[1], $parts[1]))
    //  : false;
    return ($subparts[0] <= $value && $value <= $subparts[1] && $parts[1])
        ? $in_range
        : false;
}
avest commented 5 years ago

Turns out when I test "5/20 " as a real cron job, it doesn't actually kick off a job, "5-45/20 " does. So, probably not a valid expression...

adhocore commented 5 years ago

thanks for filing issue. will take a look later today. 😊

adhocore commented 5 years ago

fixed. and bulk checks added too. maybe bump version in your composer.json (something like "adhocore/cron-expr": "^0.0") then $ composer update should be fine