From cfaeb3d4e7b750f0ae5ee3fd1a92c8cc0171aa84 Mon Sep 17 00:00:00 2001 From: aliowacom <91871615+aliowacom@users.noreply.github.com> Date: Wed, 22 Dec 2021 18:27:06 +0100 Subject: [PATCH] Error "Undefined array key 0" fix There's an error, if you pass an empty PeriodCollection to `overlap()` or `subtract()` method since there's no check whether the collection actually has any values. Steps to reproduce: ```php $period = Period::make('2021-12-31', '2022-01-01'); $collection = new PeriodCollection; $period->overlap(... $collection); // Throws ErrorException "Undefined array key 0" $period->subtract(... $collection); // Throws ErrorException "Undefined array key 0" ``` P.S. Thank you for your products and Merry Christmas! :) --- src/PeriodTraits/PeriodOperations.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/PeriodTraits/PeriodOperations.php b/src/PeriodTraits/PeriodOperations.php index 292abad..d7a0aca 100644 --- a/src/PeriodTraits/PeriodOperations.php +++ b/src/PeriodTraits/PeriodOperations.php @@ -38,7 +38,9 @@ public function gap(Period $period): ?static public function overlap(Period ...$others): ?static { - if (count($others) > 1) { + if (count($others) === 0) { + return null; + } else if (count($others) > 1) { return $this->overlapAll(...$others); } else { $other = $others[0]; @@ -115,7 +117,9 @@ public function overlapAny(Period ...$others): PeriodCollection */ public function subtract(Period ...$others): PeriodCollection { - if (count($others) > 1) { + if (count($others) === 0) + return PeriodCollection::make($this); + } else if (count($others) > 1) { return $this->subtractAll(...$others); } else { $other = $others[0];