Skip to content

Commit cfb111f

Browse files
committed
Cleanup and optimize a bit.
1 parent 8b995f4 commit cfb111f

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

lib/Recur/EventIterator.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,15 @@ public function rewind(): void
342342
$this->nextDate = null;
343343
$this->currentDate = clone $this->startDate;
344344

345+
$this->currentCandidates = [];
346+
foreach ($this->recurIterators as $index => $iterator) {
347+
if (!$iterator->valid()) {
348+
continue;
349+
}
350+
$this->currentCandidates[$index] = $iterator->current()->getTimeStamp();
351+
}
352+
asort($this->currentCandidates);
353+
345354
$this->next();
346355
}
347356

@@ -363,41 +372,31 @@ public function next(): void
363372
// We need to ask rruleparser for the next date.
364373
// We need to do this until we find a date that's not in the
365374
// exception list.
366-
$candidates = [];
367-
foreach ($this->recurIterators as $index => $iterator) {
368-
if (!$iterator->valid()) {
369-
continue;
370-
}
371-
$candidates[$index] = $iterator->current()->getTimeStamp();
372-
}
373375
do {
374-
if (empty($candidates)) {
376+
if (empty($this->currentCandidates)) {
375377
$nextDate = null;
376378
break;
377379
}
378-
asort($candidates);
379-
$nextIndex = array_key_first($candidates);
380+
$nextIndex = array_key_first($this->currentCandidates);
380381
$nextDate = $this->recurIterators[$nextIndex]->current();
381-
$nextStamp = $candidates[$nextIndex];
382-
383-
$isException = isset($this->exceptions[$nextStamp]);
382+
$nextStamp = $this->currentCandidates[$nextIndex];
384383

385384
// advance all iterators which match the current timestamp
386-
foreach ($candidates as $index => $stamp) {
385+
foreach ($this->currentCandidates as $index => $stamp) {
387386
if ($stamp > $nextStamp) {
388387
break;
389388
}
390389
$iterator = $this->recurIterators[$index];
391390
$iterator->next();
392-
if ($isException) {
393-
if ($iterator->valid()) {
394-
$candidates[$index] = $iterator->current()->getTimeStamp();
395-
} else {
396-
unset($candidates[$index]);
397-
}
391+
if ($iterator->valid()) {
392+
$this->currentCandidates[$index] = $iterator->current()->getTimeStamp();
393+
asort($this->currentCandidates);
394+
} else {
395+
unset($this->currentCandidates[$index]);
396+
// resort not neccessary
398397
}
399398
}
400-
} while ($isException);
399+
} while (isset($this->exceptions[$nextStamp]));
401400
}
402401

403402
// $nextDate now contains what rrule thinks is the next one, but an
@@ -454,12 +453,19 @@ public function isInfinite(): bool
454453
}
455454

456455
/**
457-
* RRULE parser.
456+
* Array of RRULE parsers.
458457
*
459458
* @var array<int, RRuleIterator|RDateIterator>
460459
*/
461460
protected array $recurIterators;
462461

462+
/**
463+
* Array of current candidate timestamps.
464+
*
465+
* @var array<int, int>
466+
*/
467+
protected array $currentCandidates;
468+
463469
/**
464470
* The duration, in seconds, of the master event.
465471
*

0 commit comments

Comments
 (0)