From 86898a15a9dcbe11674f8d43694e9dfae48c9fdc Mon Sep 17 00:00:00 2001 From: Martin Strouhal Date: Fri, 4 Aug 2023 23:47:50 +0300 Subject: [PATCH] Floats that contain only zero floating part can now be extracted as Ints --- src/Base64String.php | 6 +--- src/Guid.php | 6 +--- src/Iban.php | 6 +--- src/IntType.php | 4 +++ src/JsonString.php | 6 +--- src/Part.php | 6 +--- src/Port.php | 6 +--- src/Quantity.php | 6 +--- src/ReLUValue.php | 6 +--- src/ScalarLeavesArray.php | 9 +----- src/SigmoidValue.php | 6 +--- src/SwiftBic.php | 6 +--- src/UnsignedFloat.php | 6 +--- src/UnsignedInt.php | 6 +--- tests/IntArrayUniqueTest.phpt | 7 ++-- tests/IntTypeTest.phpt | 60 +++++++++++++++++++++++++++++++++++ tests/UnsignedIntTest.phpt | 1 + tools/cs/ruleset.xml | 6 ++-- 18 files changed, 84 insertions(+), 75 deletions(-) create mode 100644 tests/IntTypeTest.phpt diff --git a/src/Base64String.php b/src/Base64String.php index 1bdaa90..6224d50 100644 --- a/src/Base64String.php +++ b/src/Base64String.php @@ -15,17 +15,13 @@ final class Base64String implements ToStringInterface, ComparableInterface use ToStringTrait; use StringComparableTrait; - private string $value; - private function __construct( - string $value + private string $value ) { if (!$this->isValid($value)) { throw new InvalidTypeException('Invalid Base64 string'); } - - $this->value = $value; } public static function encode( diff --git a/src/Guid.php b/src/Guid.php index a394593..628a462 100644 --- a/src/Guid.php +++ b/src/Guid.php @@ -15,16 +15,12 @@ final class Guid implements ToStringInterface, ComparableInterface use ToStringTrait; use StringComparableTrait; - private string $value; - private function __construct( - string $value + private string $value ) { if (!\preg_match('/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i', $value)) { throw new InvalidTypeException('Invalid guid value'); } - - $this->value = $value; } public static function fromHex32( diff --git a/src/Iban.php b/src/Iban.php index e79908f..240d75a 100644 --- a/src/Iban.php +++ b/src/Iban.php @@ -20,10 +20,8 @@ final class Iban implements ToStringInterface, ComparableInterface private \Iban\Validation\Iban $iban; - private string $value; - public function __construct( - string $value + private string $value ) { if (!\class_exists(\Iban\Validation\Iban::class) || !\class_exists(\Iban\Validation\Validator::class)) { @@ -36,8 +34,6 @@ public function __construct( if (!$validator->validate($this->iban)) { throw new InvalidTypeException('Invalid Iban: ' . $value); } - - $this->value = $value; } public function getValue(): string diff --git a/src/IntType.php b/src/IntType.php index f5035de..62bab6f 100644 --- a/src/IntType.php +++ b/src/IntType.php @@ -20,6 +20,10 @@ final public static function from( return (int) $value; } + if (Validators::isNumeric($value) && (int) $value === (int) \ceil(\abs((float) $value))) { + return (int) $value; + } + throw InvalidTypeException::typeError('int', $value); } diff --git a/src/JsonString.php b/src/JsonString.php index b82143a..ce38261 100644 --- a/src/JsonString.php +++ b/src/JsonString.php @@ -17,17 +17,13 @@ final class JsonString implements ToStringInterface, ComparableInterface use ToStringTrait; use StringComparableTrait; - private string $value; - private function __construct( - string $value + private string $value ) { if (!$this->isValid($value)) { throw new InvalidTypeException('Invalid JSON string'); } - - $this->value = $value; } /** diff --git a/src/Part.php b/src/Part.php index 15619f8..3013583 100644 --- a/src/Part.php +++ b/src/Part.php @@ -15,16 +15,12 @@ final class Part implements ToStringInterface, ComparableInterface use ToStringTrait; use StringComparableTrait; - private float $value; - public function __construct( - float $value + private float $value ) { if ($value < 0 || $value > 1) { throw new InvalidTypeException('Invalid part: ' . $value); } - - $this->value = $value; } public static function fromRatio( diff --git a/src/Port.php b/src/Port.php index 6c0cd1a..3ffc806 100644 --- a/src/Port.php +++ b/src/Port.php @@ -15,16 +15,12 @@ final class Port implements ToStringInterface, ComparableInterface use ToStringTrait; use StringComparableTrait; - private int $value; - public function __construct( - int $value + private int $value ) { if ($value < 0 || $value > 65535) { throw new InvalidTypeException('Invalid Port number: ' . $value); } - - $this->value = $value; } public function getValue(): int diff --git a/src/Quantity.php b/src/Quantity.php index 83fa2ba..80cadcc 100644 --- a/src/Quantity.php +++ b/src/Quantity.php @@ -15,16 +15,12 @@ final class Quantity implements ToStringInterface, ComparableInterface use ToStringTrait; use StringComparableTrait; - private int $value; - public function __construct( - int $value + private int $value ) { if ($value < 1 || $value > \PHP_INT_MAX) { throw new InvalidTypeException('Invalid quantity: ' . $value); } - - $this->value = $value; } public function getValue(): int diff --git a/src/ReLUValue.php b/src/ReLUValue.php index 06a786f..355c9c3 100644 --- a/src/ReLUValue.php +++ b/src/ReLUValue.php @@ -15,16 +15,12 @@ final class ReLUValue implements ToStringInterface, ComparableInterface use ToStringTrait; use StringComparableTrait; - private float $value; - public function __construct( - float $value + private float $value ) { if ($value < 0.0) { throw new InvalidTypeException('Invalid ReLU value: ' . $value); } - - $this->value = $value; } public function getValue(): float diff --git a/src/ScalarLeavesArray.php b/src/ScalarLeavesArray.php index f9f6fc4..99305e7 100644 --- a/src/ScalarLeavesArray.php +++ b/src/ScalarLeavesArray.php @@ -15,22 +15,15 @@ final class ScalarLeavesArray implements ToArrayInterface, ComparableInterface use ArrayExtractableTrait; use ArrayComparableTrait; - /** - * @var array - */ - private array $data; - /** * @param array $data */ public function __construct( - array $data + private array $data ) { if (!ValidationHelpers::isScalarLeavesArray($data)) { throw new InvalidTypeException('Array must have all it\'s leaves scalar or null'); } - - $this->data = $data; } /** diff --git a/src/SigmoidValue.php b/src/SigmoidValue.php index 470e6e1..2a86b2a 100644 --- a/src/SigmoidValue.php +++ b/src/SigmoidValue.php @@ -15,16 +15,12 @@ final class SigmoidValue implements ToStringInterface, ComparableInterface use ToStringTrait; use StringComparableTrait; - private float $value; - public function __construct( - float $value + private float $value ) { if ($value < -1 || $value > 1) { throw new InvalidTypeException('Invalid sigmoid value: ' . $value); } - - $this->value = $value; } public function getValue(): float diff --git a/src/SwiftBic.php b/src/SwiftBic.php index b2fe9bb..0857935 100644 --- a/src/SwiftBic.php +++ b/src/SwiftBic.php @@ -16,14 +16,10 @@ final class SwiftBic implements ToStringInterface, ComparableInterface use StringExtractableTrait; use StringComparableTrait; - private string $value; - public function __construct( - string $value + private string $value ) { - $this->value = $value; - if (!$this->isValid($this->value)) { throw new InvalidTypeException('Invalid Swift/Bic: ' . $value); } diff --git a/src/UnsignedFloat.php b/src/UnsignedFloat.php index 629b749..23e21d5 100644 --- a/src/UnsignedFloat.php +++ b/src/UnsignedFloat.php @@ -15,16 +15,12 @@ final class UnsignedFloat implements ToStringInterface, ComparableInterface use ToStringTrait; use StringComparableTrait; - private float $value; - public function __construct( - float $value + private float $value ) { if ($value < 0.0 || $value > \PHP_INT_MAX) { throw new InvalidTypeException('Invalid unsigned float: ' . $value); } - - $this->value = $value; } public function getValue(): float diff --git a/src/UnsignedInt.php b/src/UnsignedInt.php index 5fd7af8..321c665 100644 --- a/src/UnsignedInt.php +++ b/src/UnsignedInt.php @@ -15,16 +15,12 @@ final class UnsignedInt implements ToStringInterface, ComparableInterface use ToStringTrait; use StringComparableTrait; - private int $value; - public function __construct( - int $value + private int $value ) { if ($value < 0 || $value > \PHP_INT_MAX) { throw new InvalidTypeException('Invalid unsigned integer: ' . $value); } - - $this->value = $value; } public function getValue(): int diff --git a/tests/IntArrayUniqueTest.phpt b/tests/IntArrayUniqueTest.phpt index 0041f02..2de0650 100644 --- a/tests/IntArrayUniqueTest.phpt +++ b/tests/IntArrayUniqueTest.phpt @@ -26,7 +26,7 @@ final class IntArrayUniqueTest extends TestCase new \stdClass(), ], [ - 1.0, + 1.01, 2, 3, ], @@ -40,6 +40,7 @@ final class IntArrayUniqueTest extends TestCase foreach ($invalidValues as $invalidValue) { Assert::throws( static function () use ($invalidValue): void { + echo 'Testing ' . \var_export($invalidValue, true) . \PHP_EOL; UniqueIntArray::from($invalidValue); }, InvalidTypeException::class @@ -49,12 +50,12 @@ final class IntArrayUniqueTest extends TestCase $validValues = [ [ - '1', + '1.00', '2', '3', ], [ - 1, + 1.0, 2, 3, ], diff --git a/tests/IntTypeTest.phpt b/tests/IntTypeTest.phpt new file mode 100644 index 0000000..139c3c3 --- /dev/null +++ b/tests/IntTypeTest.phpt @@ -0,0 +1,60 @@ +run(); diff --git a/tests/UnsignedIntTest.phpt b/tests/UnsignedIntTest.phpt index 8926db3..4424b31 100644 --- a/tests/UnsignedIntTest.phpt +++ b/tests/UnsignedIntTest.phpt @@ -25,6 +25,7 @@ final class UnsignedIntTest extends TestCase foreach ($invalidValues as $validValue) { Assert::throws( static function () use ($validValue): void { + echo 'Testing ' . \var_export($validValue, true) . \PHP_EOL; UnsignedInt::from($validValue); }, InvalidTypeException::class diff --git a/tools/cs/ruleset.xml b/tools/cs/ruleset.xml index 7c552d4..2f08fcc 100644 --- a/tools/cs/ruleset.xml +++ b/tools/cs/ruleset.xml @@ -45,10 +45,10 @@ - - + + @@ -81,7 +81,6 @@ - @@ -101,7 +100,6 @@ -