diff --git a/src/Plugin/Generator/GrpcGenerator.php b/src/Plugin/Generator/GrpcGenerator.php index f942aac..cd0bcc7 100644 --- a/src/Plugin/Generator/GrpcGenerator.php +++ b/src/Plugin/Generator/GrpcGenerator.php @@ -156,7 +156,7 @@ public function generateClient(Parser\ServiceDescriptor $service): PhpNamespace ) ->setReturnType('Client\ClientStreamChannel') ->addComment("{$phpdocPrefix}@return Client\\ClientStreamChannel<{$in->fqcn}, {$out->fqcn}>"); - } elseif (!$method->clientStreaming && $method->serverStreaming) { + } elseif (!$method->clientStreaming) { $classMethod ->setBody( <<<'PHP' @@ -281,7 +281,7 @@ public function generateServer(Parser\ServiceDescriptor $service): PhpNamespace ]) ->setReturnType($out->fqcn) ->addComment("{$phpdocPrefix}@param Server\\ClientStreamChannel<{$in->fqcn}, {$out->fqcn}> \$stream"); - } elseif (!$method->clientStreaming && $method->serverStreaming) { + } elseif (!$method->clientStreaming) { $interfaceMethod ->setParameters([ new Parameter('request')->setType($in->fqcn), @@ -357,7 +357,7 @@ public function generateServerRegistry(Parser\ServiceDescriptor $service): PhpNa PHP, $args, ); - } elseif (!$method->clientStreaming && $method->serverStreaming) { + } elseif (!$method->clientStreaming) { $handlers[] = new Literal( <<<'PHP' new Server\Rpc( diff --git a/src/Plugin/Generator/TypeDeclarationFactory.php b/src/Plugin/Generator/TypeDeclarationFactory.php index c3ccd51..63a2516 100644 --- a/src/Plugin/Generator/TypeDeclarationFactory.php +++ b/src/Plugin/Generator/TypeDeclarationFactory.php @@ -60,18 +60,12 @@ public function create(Parser\FieldDescriptor $field): TypeDeclaration }), default: 0, ), - Type::TYPE_INT64, Type::TYPE_UINT64, - Type::TYPE_FIXED64, - Type::TYPE_SFIXED64, - Type::TYPE_SINT64 => new TypeDeclaration( + Type::TYPE_FIXED64 => new TypeDeclaration( phpType: '\BcMath\Number', reflectionType: new Literal(match ($type) { - Type::TYPE_INT64 => 'Reflection\Int64T::T', Type::TYPE_UINT64 => 'Reflection\Uint64T::T', Type::TYPE_FIXED64 => 'Reflection\Fixed64T::T', - Type::TYPE_SFIXED64 => 'Reflection\SFixed64T::T', - Type::TYPE_SINT64 => 'Reflection\SInt64T::T', }), default: Literal::new('\BcMath\Number', [0]), ), @@ -79,7 +73,10 @@ public function create(Parser\FieldDescriptor $field): TypeDeclaration Type::TYPE_UINT32, Type::TYPE_FIXED32, Type::TYPE_SFIXED32, - Type::TYPE_SINT32 => new TypeDeclaration( + Type::TYPE_SINT32, + Type::TYPE_INT64, + Type::TYPE_SFIXED64, + Type::TYPE_SINT64 => new TypeDeclaration( phpType: 'int', reflectionType: new Literal(match ($type) { Type::TYPE_INT32 => 'Reflection\Int32T::T', @@ -87,6 +84,9 @@ public function create(Parser\FieldDescriptor $field): TypeDeclaration Type::TYPE_UINT32 => 'Reflection\Uint32T::T', Type::TYPE_SFIXED32 => 'Reflection\SFixed32T::T', Type::TYPE_SINT32 => 'Reflection\SInt32T::T', + Type::TYPE_INT64 => 'Reflection\Int64T::T', + Type::TYPE_SFIXED64 => 'Reflection\SFixed64T::T', + Type::TYPE_SINT64 => 'Reflection\SInt64T::T', }), default: 0, ), diff --git a/tests/fixtures/proto3_scalars/proto3_scalars.proto b/tests/fixtures/proto3_scalars/proto3_scalars.proto new file mode 100644 index 0000000..1543786 --- /dev/null +++ b/tests/fixtures/proto3_scalars/proto3_scalars.proto @@ -0,0 +1,26 @@ +syntax = "proto3"; + +package proto3.scalars.v1; + +// Exercises the PHP type mapping of 64-bit scalar fields under proto3: +// the signed types (int64, sint64, sfixed64) map to native `int`, while the +// unsigned 64-bit types (uint64, fixed64) stay `\BcMath\Number`. +message Scalars { + int64 int64_singular = 1; + sint64 sint64_singular = 2; + sfixed64 sfixed64_singular = 3; + uint64 uint64_singular = 4; + fixed64 fixed64_singular = 5; + + optional int64 int64_optional = 11; + optional sint64 sint64_optional = 12; + optional sfixed64 sfixed64_optional = 13; + optional uint64 uint64_optional = 14; + optional fixed64 fixed64_optional = 15; + + repeated int64 int64_repeated = 21; + repeated sint64 sint64_repeated = 22; + repeated sfixed64 sfixed64_repeated = 23; + repeated uint64 uint64_repeated = 24; + repeated fixed64 fixed64_repeated = 25; +} diff --git a/tests/snapshots/proto2/Proto/Api/V1/TestRequest.php b/tests/snapshots/proto2/Proto/Api/V1/TestRequest.php index 2654df1..bdfe57d 100644 --- a/tests/snapshots/proto2/Proto/Api/V1/TestRequest.php +++ b/tests/snapshots/proto2/Proto/Api/V1/TestRequest.php @@ -27,7 +27,7 @@ * @param bool $boolRequired another field comment. * @param list $boolRepeated * @param list $int32Repeated - * @param list<\BcMath\Number> $int64Repeated + * @param list $int64Repeated * @param list $fixed32Repeated * @param list<\BcMath\Number> $fixed64Repeated * @param list $uint32Repeated @@ -37,12 +37,12 @@ * @param list $stringRepeated * @param list $bytesRepeated * @param list $sint32Repeated - * @param list<\BcMath\Number> $sint64Repeated + * @param list $sint64Repeated * @param list $sfixed32Repeated - * @param list<\BcMath\Number> $sfixed64Repeated + * @param list $sfixed64Repeated * @param list $boolRepeatedPacked * @param list $int32RepeatedPacked - * @param list<\BcMath\Number> $int64RepeatedPacked + * @param list $int64RepeatedPacked * @param list $fixed32RepeatedPacked * @param list<\BcMath\Number> $fixed64RepeatedPacked * @param list $uint32RepeatedPacked @@ -50,9 +50,9 @@ * @param list $floatRepeatedPacked * @param list $doubleRepeatedPacked * @param list $sint32RepeatedPacked - * @param list<\BcMath\Number> $sint64RepeatedPacked + * @param list $sint64RepeatedPacked * @param list $sfixed32RepeatedPacked - * @param list<\BcMath\Number> $sfixed64RepeatedPacked + * @param list $sfixed64RepeatedPacked * @param Protobuf\Map $mapStringString * @param ?string $lastField Maximum possible tag number. */ @@ -64,7 +64,7 @@ public function __construct( #[Reflection\Field(11, Reflection\Int32T::T)] public int $int32Required, #[Reflection\Field(12, Reflection\Int64T::T)] - public \BcMath\Number $int64Required, + public int $int64Required, #[Reflection\Field(13, Reflection\Fixed32T::T)] public int $fixed32Required, #[Reflection\Field(14, Reflection\Fixed64T::T)] @@ -84,17 +84,17 @@ public function __construct( #[Reflection\Field(102, Reflection\SInt32T::T)] public int $sint32Required, #[Reflection\Field(103, Reflection\SInt64T::T)] - public \BcMath\Number $sint64Required, + public int $sint64Required, #[Reflection\Field(104, Reflection\SFixed32T::T)] public int $sfixed32Required, #[Reflection\Field(105, Reflection\SFixed64T::T)] - public \BcMath\Number $sfixed64Required, + public int $sfixed64Required, #[Reflection\Field(30, Reflection\BoolT::T)] public ?bool $boolOptional = null, #[Reflection\Field(31, Reflection\Int32T::T)] public ?int $int32Optional = null, #[Reflection\Field(32, Reflection\Int64T::T)] - public ?\BcMath\Number $int64Optional = null, + public ?int $int64Optional = null, #[Reflection\Field(33, Reflection\Fixed32T::T)] public ?int $fixed32Optional = null, #[Reflection\Field(34, Reflection\Fixed64T::T)] @@ -114,11 +114,11 @@ public function __construct( #[Reflection\Field(302, Reflection\SInt32T::T)] public ?int $sint32Optional = null, #[Reflection\Field(303, Reflection\SInt64T::T)] - public ?\BcMath\Number $sint64Optional = null, + public ?int $sint64Optional = null, #[Reflection\Field(304, Reflection\SFixed32T::T)] public ?int $sfixed32Optional = null, #[Reflection\Field(305, Reflection\SFixed64T::T)] - public ?\BcMath\Number $sfixed64Optional = null, + public ?int $sfixed64Optional = null, #[Reflection\Field(20, new Reflection\ListT(Reflection\BoolT::T, false))] public array $boolRepeated = [], #[Reflection\Field(21, new Reflection\ListT(Reflection\Int32T::T, false))] @@ -180,7 +180,7 @@ public function __construct( #[Reflection\Field(41, Reflection\Int32T::T)] public ?int $int32Defaulted = null, #[Reflection\Field(42, Reflection\Int64T::T)] - public ?\BcMath\Number $int64Defaulted = null, + public ?int $int64Defaulted = null, #[Reflection\Field(43, Reflection\Fixed32T::T)] public ?int $fixed32Defaulted = null, #[Reflection\Field(44, Reflection\Fixed64T::T)] @@ -200,11 +200,11 @@ public function __construct( #[Reflection\Field(402, Reflection\SInt32T::T)] public ?int $sint32Defaulted = null, #[Reflection\Field(403, Reflection\SInt64T::T)] - public ?\BcMath\Number $sint64Defaulted = null, + public ?int $sint64Defaulted = null, #[Reflection\Field(404, Reflection\SFixed32T::T)] public ?int $sfixed32Defaulted = null, #[Reflection\Field(405, Reflection\SFixed64T::T)] - public ?\BcMath\Number $sfixed64Defaulted = null, + public ?int $sfixed64Defaulted = null, #[Reflection\Field(406, new Reflection\MapT(Reflection\StringT::T, Reflection\StringT::T))] public Protobuf\Map $mapStringString = new Protobuf\Map(), #[Reflection\Field(407, new Reflection\ObjectT(\Google\Protobuf\Timestamp::class))] diff --git a/tests/snapshots/proto3_scalars/Proto3/Scalars/V1/Proto3ScalarsDescriptorRegistry.php b/tests/snapshots/proto3_scalars/Proto3/Scalars/V1/Proto3ScalarsDescriptorRegistry.php new file mode 100644 index 0000000..98c2877 --- /dev/null +++ b/tests/snapshots/proto3_scalars/Proto3/Scalars/V1/Proto3ScalarsDescriptorRegistry.php @@ -0,0 +1,36 @@ +add(Registry\Descriptor::base64(self::DESCRIPTOR_BUFFER), new File( + name: 'proto3_scalars.proto', + messages: [ + new File\MessageDescriptor('proto3.scalars.v1.Scalars', \Proto3\Scalars\V1\Scalars::class), + ], + )); + } +} diff --git a/tests/snapshots/proto3_scalars/Proto3/Scalars/V1/Scalars.php b/tests/snapshots/proto3_scalars/Proto3/Scalars/V1/Scalars.php new file mode 100644 index 0000000..5bf19a1 --- /dev/null +++ b/tests/snapshots/proto3_scalars/Proto3/Scalars/V1/Scalars.php @@ -0,0 +1,65 @@ + $int64Repeated + * @param list $sint64Repeated + * @param list $sfixed64Repeated + * @param list<\BcMath\Number> $uint64Repeated + * @param list<\BcMath\Number> $fixed64Repeated + */ + public function __construct( + #[Reflection\Field(1, Reflection\Int64T::T)] + public int $int64Singular = 0, + #[Reflection\Field(2, Reflection\SInt64T::T)] + public int $sint64Singular = 0, + #[Reflection\Field(3, Reflection\SFixed64T::T)] + public int $sfixed64Singular = 0, + #[Reflection\Field(4, Reflection\Uint64T::T)] + public \BcMath\Number $uint64Singular = new \BcMath\Number(0), + #[Reflection\Field(5, Reflection\Fixed64T::T)] + public \BcMath\Number $fixed64Singular = new \BcMath\Number(0), + #[Reflection\Field(11, Reflection\Int64T::T)] + public ?int $int64Optional = null, + #[Reflection\Field(12, Reflection\SInt64T::T)] + public ?int $sint64Optional = null, + #[Reflection\Field(13, Reflection\SFixed64T::T)] + public ?int $sfixed64Optional = null, + #[Reflection\Field(14, Reflection\Uint64T::T)] + public ?\BcMath\Number $uint64Optional = null, + #[Reflection\Field(15, Reflection\Fixed64T::T)] + public ?\BcMath\Number $fixed64Optional = null, + #[Reflection\Field(21, new Reflection\ListT(Reflection\Int64T::T))] + public array $int64Repeated = [], + #[Reflection\Field(22, new Reflection\ListT(Reflection\SInt64T::T))] + public array $sint64Repeated = [], + #[Reflection\Field(23, new Reflection\ListT(Reflection\SFixed64T::T))] + public array $sfixed64Repeated = [], + #[Reflection\Field(24, new Reflection\ListT(Reflection\Uint64T::T))] + public array $uint64Repeated = [], + #[Reflection\Field(25, new Reflection\ListT(Reflection\Fixed64T::T))] + public array $fixed64Repeated = [], + ) {} +} diff --git a/tests/snapshots/proto3_scalars/Proto3/Scalars/V1/autoload.metadata.php b/tests/snapshots/proto3_scalars/Proto3/Scalars/V1/autoload.metadata.php new file mode 100644 index 0000000..4e117e5 --- /dev/null +++ b/tests/snapshots/proto3_scalars/Proto3/Scalars/V1/autoload.metadata.php @@ -0,0 +1,14 @@ +register( + new \Thesis\Protobuf\Registry\OnceRegistrar(new \Proto3\Scalars\V1\Proto3ScalarsDescriptorRegistry()), +);