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 1/2] 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]; From 8248b83ebc3f7a7ce1aeb1611b9bf91dcb22d7be Mon Sep 17 00:00:00 2001 From: Alex Averkiyev Date: Thu, 23 Dec 2021 09:46:03 +0100 Subject: [PATCH 2/2] Empty collection tests --- src/PeriodTraits/PeriodOperations.php | 2 +- tests/Operations/OverlapTest.php | 12 ++++++++++++ tests/Operations/SubtractTest.php | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/PeriodTraits/PeriodOperations.php b/src/PeriodTraits/PeriodOperations.php index d7a0aca..a3ccd84 100644 --- a/src/PeriodTraits/PeriodOperations.php +++ b/src/PeriodTraits/PeriodOperations.php @@ -117,7 +117,7 @@ public function overlapAny(Period ...$others): PeriodCollection */ public function subtract(Period ...$others): PeriodCollection { - if (count($others) === 0) + if (count($others) === 0) { return PeriodCollection::make($this); } else if (count($others) > 1) { return $this->subtractAll(...$others); diff --git a/tests/Operations/OverlapTest.php b/tests/Operations/OverlapTest.php index d65e770..1eb3f91 100644 --- a/tests/Operations/OverlapTest.php +++ b/tests/Operations/OverlapTest.php @@ -5,6 +5,7 @@ use Generator; use PHPUnit\Framework\TestCase; use Spatie\Period\Period; +use Spatie\Period\PeriodCollection; class OverlapTest extends TestCase { @@ -178,4 +179,15 @@ public function noOverlappingDates() */ yield [Period::make('2018-02-01', '2018-02-28'), Period::make('2018-01-01', '2018-01-31')]; } + + /** @test */ + public function passing_empty_period_collection_returns_null() + { + $current = Period::make('2018-01-01', '2018-01-31'); + $emptyCollection = new PeriodCollection; + + $diff = $current->overlap(... $emptyCollection); + + $this->assertNull($diff); + } } diff --git a/tests/Operations/SubtractTest.php b/tests/Operations/SubtractTest.php index 3789786..616a2e3 100644 --- a/tests/Operations/SubtractTest.php +++ b/tests/Operations/SubtractTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\TestCase; use Spatie\Period\Period; +use Spatie\Period\PeriodCollection; class SubtractTest extends TestCase { @@ -223,4 +224,17 @@ public function it_can_determine_multiple_diffs_for_sure() $this->assertTrue($diff[1]->equals(Period::make('2018-01-11', '2018-01-14'))); $this->assertTrue($diff[2]->equals(Period::make('2018-01-21', '2018-01-31'))); } + + /** @test */ + public function passing_empty_period_collection_returns_same_period_within_collection() + { + $current = Period::make('2018-01-01', '2018-01-31'); + $emptyCollection = new PeriodCollection; + + $diff = $current->subtract(... $emptyCollection); + + $this->assertInstanceOf(PeriodCollection::class, $diff); + $this->assertCount(1, $diff); + $this->assertTrue($diff[0]->equals($current)); + } }