diff --git a/src/Interval.php b/src/Interval.php index 6a21aac..9da613a 100644 --- a/src/Interval.php +++ b/src/Interval.php @@ -5,6 +5,7 @@ namespace ArrayLookup; use ArrayLookup\Assert\Filter; +use InvalidArgumentException; use Traversable; use Webmozart\Assert\Assert; @@ -59,7 +60,9 @@ public static function isExclusiveOf( Filter::boolean($filter); if ($max - $min <= 1) { - return false; + throw new InvalidArgumentException( + 'The difference between min and max must be greater than 1 for an exclusive interval.' + ); } $totalFound = 0; diff --git a/tests/IntervalTest.php b/tests/IntervalTest.php index 85aaad2..2d8d79c 100644 --- a/tests/IntervalTest.php +++ b/tests/IntervalTest.php @@ -5,6 +5,7 @@ namespace ArrayLookup\Tests; use ArrayLookup\Interval; +use InvalidArgumentException; use Iterator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -134,12 +135,20 @@ public static function exclusiveDataProvider(): Iterator 5, false, ]; - yield 'no space between bounds' => [ - [1, 2, 3], - static fn($datum): bool => $datum > 1, - 2, - 3, - false, - ]; + } + + public function testNoSpaceIntervalIsExclusiveOf(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'The difference between min and max must be greater than 1 for an exclusive interval.' + ); + + $data = [1, 2, 3]; + $filter = static fn($datum): bool => $datum > 1; + $min = 2; + $max = 3; + + Interval::isExclusiveOf($data, $filter, $min, $max); } }