Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Interval.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ArrayLookup;

use ArrayLookup\Assert\Filter;
use InvalidArgumentException;
use Traversable;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -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;
Expand Down
23 changes: 16 additions & 7 deletions tests/IntervalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ArrayLookup\Tests;

use ArrayLookup\Interval;
use InvalidArgumentException;
use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -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);
}
}