Skip to content

Commit

Permalink
Handle case of RRULE:COUNT=1 (any FREQ)
Browse files Browse the repository at this point in the history
Outlook, as reported in u01jmg3#293, may in some cases reduce `COUNT` to `1`.

Whilst not disallowed in the RFC5545 spec (or any of its errata to date),
having a `COUNT` of `1` has no practical purpose that I can see.

We currently only check we've reached the count-limit *after* we've generated
the first few recurrences of an event and - more pressingly - *after* we've
appended the first candidate to the array of generated recurrences.

In this case `PHP_MAX_INT` could be replaced with some suitable default number,
akin to the current `defaultSpan` property used to put a limit on UNTIL.
But that's something for another PR.
  • Loading branch information
s0600204 committed Jan 12, 2022
1 parent d1fdfa2 commit 958fb82
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/ICal/ICal.php
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ protected function processRecurrences()
* enddate = <icalDate> || <icalDateTime>
*/
$count = 1;
$countLimit = (isset($rrules['COUNT'])) ? intval($rrules['COUNT']) : 0;
$countLimit = (isset($rrules['COUNT'])) ? intval($rrules['COUNT']) : PHP_INT_MAX;
$until = date_create()->modify("{$this->defaultSpan} years")->setTime(23, 59, 59)->getTimestamp();

if (isset($rrules['UNTIL'])) {
Expand All @@ -1373,7 +1373,7 @@ protected function processRecurrences()
$eventRecurrences = array();

$frequencyRecurringDateTime = clone $initialEventDate;
while ($frequencyRecurringDateTime->getTimestamp() <= $until) {
while ($frequencyRecurringDateTime->getTimestamp() <= $until && $count < $countLimit) {
$candidateDateTimes = array();

// phpcs:ignore Squiz.ControlStructures.SwitchDeclaration.MissingDefault
Expand Down Expand Up @@ -1587,14 +1587,11 @@ function ($yearDay) use ($matchingDays) {
$this->eventCount++;
}

// Count all evaluated candidates including excluded ones
if (isset($rrules['COUNT'])) {
$count++;

// If RRULE[COUNT] is reached then break
if ($count >= $countLimit) {
break 2;
}
// Count all evaluated candidates including excluded ones,
// and if RRULE[COUNT] (if set) is reached then break.
$count++;
if ($count >= $countLimit) {
break 2;
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/RecurrencesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,23 @@ public function testYearlyWithByMonthAndByMonthDay()
);
}

public function testCountIsOne()
{
$checks = array(
array('index' => 0, 'dateString' => '20211201T090000', 'message' => '1st and only expected event: '),
);
$this->assertVEVENT(
'UTC',
array(
'DTSTART:20211201T090000',
'DTEND:20211201T100000',
'RRULE:FREQ=DAILY;COUNT=1',
),
1,
$checks
);
}

public function assertVEVENT($defaultTimezone, $veventParts, $count, $checks)
{
$options = $this->getOptions($defaultTimezone);
Expand Down

0 comments on commit 958fb82

Please sign in to comment.