diff --git a/src/Internal/Serde/SerdeInt64.php b/src/Internal/Serde/SerdeInt64.php index a3e5993..dab8836 100644 --- a/src/Internal/Serde/SerdeInt64.php +++ b/src/Internal/Serde/SerdeInt64.php @@ -10,8 +10,8 @@ /** * @internal - * @template-implements SerializeValue - * @template-implements DeserializeValue + * @template-implements SerializeValue + * @template-implements DeserializeValue */ enum SerdeInt64 implements SerializeValue, @@ -30,7 +30,7 @@ public function serialize(WriteBuffer $buffer, mixed $value): void static $p63; $p63 ??= new Number(2)->pow(63); - $num = $value->mod($p64); + $num = new Number($value)->mod($p64); if ($num->compare($p63) >= 0) { $num -= $p64; @@ -44,7 +44,7 @@ public function serialize(WriteBuffer $buffer, mixed $value): void } #[\Override] - public function deserialize(ReadBuffer $buffer): Number + public function deserialize(ReadBuffer $buffer): int { /** @var ?Number $p63 */ static $p63; @@ -61,6 +61,6 @@ public function deserialize(ReadBuffer $buffer): Number $num -= $p64; } - return $num; + return (int) $num->value; } } diff --git a/src/Internal/Serde/SerdeSFixed64.php b/src/Internal/Serde/SerdeSFixed64.php index 40e31cf..4f90719 100644 --- a/src/Internal/Serde/SerdeSFixed64.php +++ b/src/Internal/Serde/SerdeSFixed64.php @@ -11,8 +11,8 @@ /** * @internal - * @template-implements SerializeValue - * @template-implements DeserializeValue + * @template-implements SerializeValue + * @template-implements DeserializeValue */ enum SerdeSFixed64 implements SerializeValue, @@ -23,12 +23,12 @@ enum SerdeSFixed64 implements #[\Override] public function serialize(WriteBuffer $buffer, mixed $value): void { - $buffer->write(Endian\Order::Little->packInt64($value)); + $buffer->write(Endian\Order::Little->packInt64(new Number($value))); } #[\Override] - public function deserialize(ReadBuffer $buffer): Number + public function deserialize(ReadBuffer $buffer): int { - return Endian\Order::Little->unpackInt64($buffer->read(8)); + return (int) Endian\Order::Little->unpackInt64($buffer->read(8))->value; } } diff --git a/src/Internal/Serde/SerdeSInt32.php b/src/Internal/Serde/SerdeSInt32.php index 590084d..0f1cfaf 100644 --- a/src/Internal/Serde/SerdeSInt32.php +++ b/src/Internal/Serde/SerdeSInt32.php @@ -37,7 +37,7 @@ public function serialize(WriteBuffer $buffer, mixed $value): void $num -= $p32; } - SerdeSInt64::T->serialize($buffer, $num); + SerdeSInt64::T->serialize($buffer, (int) $num->value); } #[\Override] @@ -51,8 +51,7 @@ public function deserialize(ReadBuffer $buffer): int static $p32; $p32 ??= new Number(2)->pow(32); - $num = SerdeSInt64::T->deserialize($buffer); - $num = $num->mod($p32); + $num = new Number(SerdeSInt64::T->deserialize($buffer))->mod($p32); if ($num->compare($p31) >= 0) { $num -= $p32; diff --git a/src/Internal/Serde/SerdeSInt64.php b/src/Internal/Serde/SerdeSInt64.php index 136b905..7f4c10f 100644 --- a/src/Internal/Serde/SerdeSInt64.php +++ b/src/Internal/Serde/SerdeSInt64.php @@ -11,8 +11,8 @@ /** * @internal - * @template-implements SerializeValue - * @template-implements DeserializeValue + * @template-implements SerializeValue + * @template-implements DeserializeValue */ enum SerdeSInt64 implements SerializeValue, @@ -23,12 +23,12 @@ enum SerdeSInt64 implements #[\Override] public function serialize(WriteBuffer $buffer, mixed $value): void { - SerdeVarint::T->serialize($buffer, Varint\BcMath::Codec->encodeZigZag($value)); + SerdeVarint::T->serialize($buffer, Varint\BcMath::Codec->encodeZigZag(new Number($value))); } #[\Override] - public function deserialize(ReadBuffer $buffer): Number + public function deserialize(ReadBuffer $buffer): int { - return Varint\BcMath::Codec->decodeZigZag(SerdeVarint::T->deserialize($buffer)); + return (int) Varint\BcMath::Codec->decodeZigZag(SerdeVarint::T->deserialize($buffer))->value; } } diff --git a/src/Reflection/Int64T.php b/src/Reflection/Int64T.php index 66e2ea9..da00e06 100644 --- a/src/Reflection/Int64T.php +++ b/src/Reflection/Int64T.php @@ -4,11 +4,9 @@ namespace Thesis\Protobuf\Reflection; -use BcMath\Number; - /** * @api - * @template-implements Type + * @template-implements Type */ enum Int64T implements Type { diff --git a/src/Reflection/Internal/Visitor/ToValueTypeVisitor.php b/src/Reflection/Internal/Visitor/ToValueTypeVisitor.php index 6e46433..d2e56b4 100644 --- a/src/Reflection/Internal/Visitor/ToValueTypeVisitor.php +++ b/src/Reflection/Internal/Visitor/ToValueTypeVisitor.php @@ -14,12 +14,15 @@ use Thesis\Protobuf\Reflection\Fixed32T; use Thesis\Protobuf\Reflection\FloatT; use Thesis\Protobuf\Reflection\Int32T; +use Thesis\Protobuf\Reflection\Int64T; use Thesis\Protobuf\Reflection\ListT; use Thesis\Protobuf\Reflection\MapT; use Thesis\Protobuf\Reflection\ObjectT; use Thesis\Protobuf\Reflection\Reflector; use Thesis\Protobuf\Reflection\SFixed32T; +use Thesis\Protobuf\Reflection\SFixed64T; use Thesis\Protobuf\Reflection\SInt32T; +use Thesis\Protobuf\Reflection\SInt64T; use Thesis\Protobuf\Reflection\StringT; use Thesis\Protobuf\Reflection\Type; use Thesis\Protobuf\Reflection\Uint32T; @@ -100,6 +103,30 @@ public function sfixed32(SFixed32T $type): mixed return $this->value; } + #[\Override] + public function int64(Int64T $type): mixed + { + \assert(\is_int($this->value)); + + return $this->value; + } + + #[\Override] + public function sint64(SInt64T $type): mixed + { + \assert(\is_int($this->value)); + + return $this->value; + } + + #[\Override] + public function sfixed64(SFixed64T $type): mixed + { + \assert(\is_int($this->value)); + + return $this->value; + } + #[\Override] public function string(StringT $type): mixed { diff --git a/src/Reflection/SFixed64T.php b/src/Reflection/SFixed64T.php index 6712bae..19ceb63 100644 --- a/src/Reflection/SFixed64T.php +++ b/src/Reflection/SFixed64T.php @@ -4,11 +4,9 @@ namespace Thesis\Protobuf\Reflection; -use BcMath\Number; - /** * @api - * @template-implements Type + * @template-implements Type */ enum SFixed64T implements Type { diff --git a/src/Reflection/SInt64T.php b/src/Reflection/SInt64T.php index c9e8061..bc9b11b 100644 --- a/src/Reflection/SInt64T.php +++ b/src/Reflection/SInt64T.php @@ -4,11 +4,9 @@ namespace Thesis\Protobuf\Reflection; -use BcMath\Number; - /** * @api - * @template-implements Type + * @template-implements Type */ enum SInt64T implements Type { diff --git a/src/Type/Int64T.php b/src/Type/Int64T.php index baa2b1d..ede2e5b 100644 --- a/src/Type/Int64T.php +++ b/src/Type/Int64T.php @@ -4,18 +4,17 @@ namespace Thesis\Protobuf\Type; -use BcMath\Number; use Thesis\Protobuf\Type; /** - * @template-implements Type - * @template-implements Listable + * @template-implements Type + * @template-implements Listable */ enum Int64T implements Type, Listable { - /** @use Listed */ + /** @use Listed */ use Listed; case T; diff --git a/src/Type/SFixed64T.php b/src/Type/SFixed64T.php index ec1545e..67581d5 100644 --- a/src/Type/SFixed64T.php +++ b/src/Type/SFixed64T.php @@ -4,18 +4,17 @@ namespace Thesis\Protobuf\Type; -use BcMath\Number; use Thesis\Protobuf\Type; /** - * @template-implements Type - * @template-implements Listable + * @template-implements Type + * @template-implements Listable */ enum SFixed64T implements Type, Listable { - /** @use Listed */ + /** @use Listed */ use Listed; case T; diff --git a/src/Type/SInt64T.php b/src/Type/SInt64T.php index d1cd40b..556c034 100644 --- a/src/Type/SInt64T.php +++ b/src/Type/SInt64T.php @@ -4,18 +4,17 @@ namespace Thesis\Protobuf\Type; -use BcMath\Number; use Thesis\Protobuf\Type; /** - * @template-implements Type - * @template-implements Listable + * @template-implements Type + * @template-implements Listable */ enum SInt64T implements Type, Listable { - /** @use Listed */ + /** @use Listed */ use Listed; case T; diff --git a/src/Value.php b/src/Value.php index 02ceb98..808a72e 100644 --- a/src/Value.php +++ b/src/Value.php @@ -29,9 +29,9 @@ public static function int32(int $num): self } /** - * @return self + * @return self */ - public static function int64(Number $num): self + public static function int64(int $num): self { return new self($num, int64T); } @@ -61,9 +61,9 @@ public static function sint32(int $num): self } /** - * @return self + * @return self */ - public static function sint64(Number $num): self + public static function sint64(int $num): self { return new self($num, sint64T); } @@ -93,9 +93,9 @@ public static function fixed64(Number $num): self } /** - * @return self + * @return self */ - public static function sfixed64(Number $num): self + public static function sfixed64(int $num): self { return new self($num, sfixed64T); } diff --git a/src/constructors.php b/src/constructors.php index 31c0796..9952568 100644 --- a/src/constructors.php +++ b/src/constructors.php @@ -52,9 +52,9 @@ function sint32Of(int $num): Value /** * @api - * @return Value + * @return Value */ -function int64Of(Number $num): Value +function int64Of(int $num): Value { return Value::int64($num); } @@ -74,9 +74,9 @@ function uint64Of(Number $num): Value /** * @api - * @return Value + * @return Value */ -function sint64Of(Number $num): Value +function sint64Of(int $num): Value { return Value::sint64($num); } @@ -118,9 +118,9 @@ function fixed64Of(Number $num): Value /** * @api - * @return Value + * @return Value */ -function sfixed64Of(Number $num): Value +function sfixed64Of(int $num): Value { return Value::sfixed64($num); } diff --git a/tests/ScalarCardinalityTest.php b/tests/ScalarCardinalityTest.php index cc87afe..025a3bf 100644 --- a/tests/ScalarCardinalityTest.php +++ b/tests/ScalarCardinalityTest.php @@ -112,15 +112,15 @@ public static function provideRoundTripCases(): iterable 'sint32' => Value::sint32(filter_var($value, FILTER_VALIDATE_INT)), /** @phpstan-ignore argument.type */ 'uint32' => Value::uint32(filter_var($value, FILTER_VALIDATE_INT)), - 'int64' => Value::int64(self::toNumber($value)), + 'int64' => Value::int64((int) $value), 'uint64' => Value::uint64(self::toNumber($value)), - 'sint64' => Value::sint64(self::toNumber($value)), + 'sint64' => Value::sint64((int) $value), /** @phpstan-ignore argument.type */ 'fixed32' => Value::fixed32(filter_var($value, FILTER_VALIDATE_INT)), /** @phpstan-ignore argument.type */ 'sfixed32' => Value::sfixed32(filter_var($value, FILTER_VALIDATE_INT)), 'fixed64' => Value::fixed64(self::toNumber($value)), - 'sfixed64' => Value::sfixed64(self::toNumber($value)), + 'sfixed64' => Value::sfixed64((int) $value), default => throw new \UnexpectedValueException("Cannot handle type '{$type}'."), }, $hex, diff --git a/tests/testdata/reflection/all_in_one.php b/tests/testdata/reflection/all_in_one.php index 0893dff..443904c 100644 --- a/tests/testdata/reflection/all_in_one.php +++ b/tests/testdata/reflection/all_in_one.php @@ -9,7 +9,7 @@ final readonly class Nested { /** - * @param ?list $values + * @param ?list $values */ public function __construct( #[Reflection\Field(1, Reflection\Uint32T::T)] @@ -76,7 +76,7 @@ public function __construct( #[Reflection\Field(1, Reflection\Int32T::T)] public ?int $aInt32 = null, #[Reflection\Field(2, Reflection\Int64T::T)] - public ?Number $aInt64 = null, + public ?int $aInt64 = null, #[Reflection\Field(3, Reflection\Uint32T::T)] public ?int $aUint32 = null, #[Reflection\Field(4, Reflection\Uint64T::T)] @@ -84,7 +84,7 @@ public function __construct( #[Reflection\Field(5, Reflection\SInt32T::T)] public ?int $aSint32 = null, #[Reflection\Field(6, Reflection\SInt64T::T)] - public ?Number $aSint64 = null, + public ?int $aSint64 = null, #[Reflection\Field(7, Reflection\Fixed32T::T)] public ?int $aFixed32 = null, #[Reflection\Field(8, Reflection\Fixed64T::T)] @@ -92,7 +92,7 @@ public function __construct( #[Reflection\Field(9, Reflection\SFixed32T::T)] public ?int $aSfixed32 = null, #[Reflection\Field(10, Reflection\SFixed64T::T)] - public ?Number $aSfixed64 = null, + public ?int $aSfixed64 = null, #[Reflection\Field(11, Reflection\FloatT::T)] public ?float $aFloat = null, #[Reflection\Field(12, Reflection\DoubleT::T)]