diff --git a/composer.json b/composer.json index 0d2029a..66f57b0 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ ], "config": { "platform": { - "php": "7.4" + "php": "8.0" } }, "require-dev": { diff --git a/composer.lock b/composer.lock index 79257bb..200ec17 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "eee0ba07948c8ad3aaffacb67e046a41", + "content-hash": "bfdc6a7197cf48a40f37e1e3241a5d14", "packages": [], "packages-dev": [ { @@ -3755,13 +3755,13 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": false, "prefer-lowest": false, - "platform": [], - "platform-dev": [], + "platform": {}, + "platform-dev": {}, "platform-overrides": { - "php": "7.4" + "php": "8.0" }, - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/psalm.xml b/psalm.xml index 30e1a90..851aaae 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,6 +1,6 @@ validate($value); $this->value = $value; } @@ -25,12 +26,17 @@ final public function getValue(): int * @param int|float|string $value * @return static */ - final public static function fromNumeric($value): self + final public static function fromNumeric($value): static { if (is_numeric($value)) { - return new self((int)$value); + return new static((int)$value); } throw new InvalidArgumentException('Value is not numeric.'); } + + protected function validate(int $value): void + { + // Integer does nothing. + } } diff --git a/src/Number/NonNegativeInteger.php b/src/Number/NonNegativeInteger.php index ff67eaa..627ee0e 100644 --- a/src/Number/NonNegativeInteger.php +++ b/src/Number/NonNegativeInteger.php @@ -8,12 +8,10 @@ class NonNegativeInteger extends Integer { - public function __construct(int $value) + protected function validate(int $value): void { if ($value < 0) { throw new InvalidArgumentException('Value must be a non negative integer'); } - - parent::__construct($value); } } diff --git a/src/Number/PositiveInteger.php b/src/Number/PositiveInteger.php index 6ea2de2..a8117b9 100644 --- a/src/Number/PositiveInteger.php +++ b/src/Number/PositiveInteger.php @@ -8,12 +8,10 @@ class PositiveInteger extends Integer { - public function __construct(int $value) + protected function validate(int $value): void { if ($value <= 0) { throw new InvalidArgumentException('Value must be a positive integer'); } - - parent::__construct($value); } } diff --git a/tests/Number/NonNegativeIntegerTest.php b/tests/Number/NonNegativeIntegerTest.php index 0b022be..ea31395 100644 --- a/tests/Number/NonNegativeIntegerTest.php +++ b/tests/Number/NonNegativeIntegerTest.php @@ -72,4 +72,25 @@ public function test_isNotEqualToDifferentType(): void $this->assertFalse($integer1->equals($integer2)); $this->assertFalse($integer2->equals($integer1)); } + + public function test_canNotBeCreatedFromNegativeNumericString(): void + { + $this->expectException(InvalidArgumentException::class); + + NonNegativeInteger::fromNumeric('-42'); + } + + public function test_canNotBeCreatedFromNegativeFloat(): void + { + $this->expectException(InvalidArgumentException::class); + + $result = NonNegativeInteger::fromNumeric(-42.3); + } + + public function test_canNotBeCreatedFromNegativeNumeric(): void + { + $this->expectException(InvalidArgumentException::class); + + NonNegativeInteger::fromNumeric(-42); + } } diff --git a/tests/Number/PositiveIntegerTest.php b/tests/Number/PositiveIntegerTest.php index 0c4bdb4..933a959 100644 --- a/tests/Number/PositiveIntegerTest.php +++ b/tests/Number/PositiveIntegerTest.php @@ -73,4 +73,32 @@ public function test_isNotEqualToDifferentType(): void $this->assertFalse($integer1->equals($integer2)); $this->assertFalse($integer2->equals($integer1)); } + + public function test_CannotBeCreatedFromNumericZeroString(): void + { + $this->expectException(InvalidArgumentException::class); + + PositiveInteger::fromNumeric('0'); + } + + public function test_CannotBeCreatedFromNumericZeroFloat(): void + { + $this->expectException(InvalidArgumentException::class); + + PositiveInteger::fromNumeric(0.0); + } + + public function test_CannotBeCreatedFromNumericNegativeIntegerString(): void + { + $this->expectException(InvalidArgumentException::class); + + PositiveInteger::fromNumeric('-1'); + } + + public function test_CannotBeCreatedFromNumericNegativeInteger(): void + { + $this->expectException(InvalidArgumentException::class); + + PositiveInteger::fromNumeric(-1); + } }