Skip to content

Commit

Permalink
Error "Undefined array key 0" fix
Browse files Browse the repository at this point in the history
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! :)
  • Loading branch information
aliowacom authored Dec 22, 2021
1 parent 66f504e commit cfaeb3d
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/PeriodTraits/PeriodOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit cfaeb3d

Please sign in to comment.