Skip to content

[DayOfMonth] Allow 'L' (last day of month) as a member of a day-of-month list - #224

Open
AjmalAlkhaledi wants to merge 1 commit into
dragonmantank:masterfrom
AjmalAlkhaledi:fix/day-of-month-list-with-last-day
Open

AjmalAlkhaledi wants to merge 1 commit into
dragonmantank:masterfrom
AjmalAlkhaledi:fix/day-of-month-list-with-last-day

Conversation

@AjmalAlkhaledi

Copy link
Copy Markdown

Summary

The day-of-month field validator rejects any list that contains L, so a
valid expression such as L,15 ("the last day of the month or the 15th")
throws InvalidArgumentException at construction — even though the rest of the
engine already handles L inside a list correctly.

Fixes #189.

Problem

new CronExpression('0 23 L,15 * *');
// InvalidArgumentException: Invalid CRON field value L,15 at position 2

L is documented as an allowed day-of-month character, and lists (,) are
allowed too, but the two cannot currently be combined. Users who want "run on
the 15th and on the last day of the month" have no way to express it in a single
field.

Root cause

DayOfMonthField::validate() contains a guard that rejects a list if it
contains either W or L:

// Validate that a list don't have W or L
if (false !== strpos($value, ',') && (false !== strpos($value, 'W') || false !== strpos($value, 'L'))) {
    return false;
}

The restriction is correct for W: the class docblock states that W "can only
be specified when the day-of-month is a single day, not a range or list of
days." There is no such restriction for L, and none is needed:

  • Matching already supports L in a list. CronExpression::getRunDate()
    splits a comma list and calls isSatisfiedBy() on each member, so L,15
    evaluates isSatisfiedBy($date, 'L') (last-day check) OR
    isSatisfiedBy($date, '15') (day 15).
  • Validation of each member already works: AbstractField::validate()
    validates list members individually, and DayOfMonthField::validate('L')
    returns true.

So the single over-broad guard is the only thing rejecting L,15.

Solution

Reject a list only when it contains W; allow L:

// 'W' may only qualify a single day, never a member of a list (see the
// class docblock). 'L' (last day of the month), by contrast, is a valid
// list member, e.g. "L,15" meaning "the last day or the 15th".
if (false !== strpos($value, ',') && false !== strpos($value, 'W')) {
    return false;
}

No change to matching or increment is required — they already behave correctly.

Tests

Added to tests/Cron/DayOfMonthFieldTest.php:

  • testIssue189ListMayContainLastDayOfMonth() — L,15, 15,L, L,1-5,
    L,15,20 validate; 5W,L, 5W,15, 15,5W remain invalid.
  • testIssue189ListWithLastDayOfMonthSchedules() — 0 0 L,15 * * schedules on
    the 15th and the last day of each month, across a leap February
    (…2024-02-15, 2024-02-29, 2024-03-15, 2024-03-31).

The validation assertions fail on the unmodified code (validate('L,15')
returns false) and pass with this change. The existing assertion
assertFalse($f->validate('5W,L')) still holds (that value contains W).

Run:

vendor/bin/phpunit --filter DayOfMonthFieldTest

Backward compatibility

No behavior change for any expression that already validated. Expressions that
previously threw (L in a list) now construct and schedule as documented. The
documented W-single-day restriction is unchanged.

The day-of-month validator rejected any list containing 'L', so a valid
expression such as "L,15" (the last day of the month or the 15th) threw
InvalidArgumentException at construction — even though the matching path
(CronExpression::getRunDate splits the list and evaluates each member) and
increment already handle 'L' inside a list correctly.

Only 'W' (nearest weekday) is documented as single-day-only, so the list
guard now rejects 'W' in a list and allows 'L'. "5W,L" and other W-in-list
values remain invalid.

Fixes dragonmantank#189.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Last day of month (L) does not work in tandem with other days of month

1 participant