diff --git a/src/IntRange.php b/src/IntRange.php index fbbd47c..56bd50c 100644 --- a/src/IntRange.php +++ b/src/IntRange.php @@ -17,8 +17,6 @@ abstract protected function min(): int; /** The maximum allowed value. */ abstract protected function max(): int; - public ?string $description = 'Checks if the given column is of the format 96-well column'; - public function serialize($value) { if (is_int($value) && $this->isValueInExpectedRange($value)) { @@ -26,7 +24,7 @@ public function serialize($value) } $notInRange = Utils::printSafe($value); - throw new \InvalidArgumentException("Value not in range: {$notInRange}."); + throw new \InvalidArgumentException("Value not in range {$this->rangeDescription()}: {$notInRange}."); } public function parseValue($value) @@ -36,7 +34,7 @@ public function parseValue($value) } $notInRange = Utils::printSafe($value); - throw new Error("Value not in range: {$notInRange}."); + throw new Error("Value not in range {$this->rangeDescription()}: {$notInRange}."); } public function parseLiteral(Node $valueNode, ?array $variables = null) @@ -49,11 +47,19 @@ public function parseLiteral(Node $valueNode, ?array $variables = null) } $notInRange = Printer::doPrint($valueNode); - throw new Error("Value not in range: {$notInRange}.", $valueNode); + throw new Error("Value not in range {$this->rangeDescription()}: {$notInRange}.", $valueNode); } private function isValueInExpectedRange(int $value): bool { return $value <= static::max() && $value >= static::min(); } + + private function rangeDescription(): string + { + $min = static::min(); + $max = static::max(); + + return "{$min}-{$max}"; + } } diff --git a/tests/IntRangeTest.php b/tests/IntRangeTest.php index c79a298..1f78215 100644 --- a/tests/IntRangeTest.php +++ b/tests/IntRangeTest.php @@ -10,7 +10,7 @@ final class IntRangeTest extends TestCase public function testSerializeThrowsIfNotAnInt(): void { $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Value not in range: "12".'); + $this->expectExceptionMessage('Value not in range 1-12: "12".'); (new UpToADozen())->serialize('12'); } @@ -18,7 +18,7 @@ public function testSerializeThrowsIfNotAnInt(): void public function testSerializeThrowsIfInvalid(): void { $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Value not in range: 13.'); + $this->expectExceptionMessage('Value not in range 1-12: 13.'); (new UpToADozen())->serialize(13); } @@ -33,7 +33,7 @@ public function testSerializePassesWhenValid(): void public function testParseValueThrowsIfInvalid(): void { $this->expectException(Error::class); - $this->expectExceptionMessage('Value not in range: 13.'); + $this->expectExceptionMessage('Value not in range 1-12: 13.'); (new UpToADozen())->parseValue(13); }