From abac8080004ce018287275e41de40f57800f97aa Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 19 Jun 2018 22:20:56 +0200 Subject: adds PHP typehints for message setters (#4533) --- php/src/Google/Protobuf/Internal/DescriptorProto.php | 2 +- php/src/Google/Protobuf/Internal/EnumDescriptorProto.php | 2 +- .../Protobuf/Internal/EnumValueDescriptorProto.php | 2 +- .../Google/Protobuf/Internal/FieldDescriptorProto.php | 2 +- php/src/Google/Protobuf/Internal/FileDescriptorProto.php | 4 ++-- .../Google/Protobuf/Internal/MethodDescriptorProto.php | 2 +- .../Google/Protobuf/Internal/OneofDescriptorProto.php | 2 +- .../Google/Protobuf/Internal/ServiceDescriptorProto.php | 2 +- src/google/protobuf/compiler/php/php_generator.cc | 16 ++++++++++++---- 9 files changed, 21 insertions(+), 13 deletions(-) diff --git a/php/src/Google/Protobuf/Internal/DescriptorProto.php b/php/src/Google/Protobuf/Internal/DescriptorProto.php index 3b215d52..707e39d0 100644 --- a/php/src/Google/Protobuf/Internal/DescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/DescriptorProto.php @@ -306,7 +306,7 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message * @param \Google\Protobuf\Internal\MessageOptions $var * @return $this */ - public function setOptions($var) + public function setOptions(\Google\Protobuf\Internal\MessageOptions $var = null) { GPBUtil::checkMessage($var, \Google\Protobuf\Internal\MessageOptions::class); $this->options = $var; diff --git a/php/src/Google/Protobuf/Internal/EnumDescriptorProto.php b/php/src/Google/Protobuf/Internal/EnumDescriptorProto.php index da30fa99..df912e55 100644 --- a/php/src/Google/Protobuf/Internal/EnumDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/EnumDescriptorProto.php @@ -143,7 +143,7 @@ class EnumDescriptorProto extends \Google\Protobuf\Internal\Message * @param \Google\Protobuf\Internal\EnumOptions $var * @return $this */ - public function setOptions($var) + public function setOptions(\Google\Protobuf\Internal\EnumOptions $var = null) { GPBUtil::checkMessage($var, \Google\Protobuf\Internal\EnumOptions::class); $this->options = $var; diff --git a/php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php b/php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php index 50bda008..6f4a6dd7 100644 --- a/php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php @@ -119,7 +119,7 @@ class EnumValueDescriptorProto extends \Google\Protobuf\Internal\Message * @param \Google\Protobuf\Internal\EnumValueOptions $var * @return $this */ - public function setOptions($var) + public function setOptions(\Google\Protobuf\Internal\EnumValueOptions $var = null) { GPBUtil::checkMessage($var, \Google\Protobuf\Internal\EnumValueOptions::class); $this->options = $var; diff --git a/php/src/Google/Protobuf/Internal/FieldDescriptorProto.php b/php/src/Google/Protobuf/Internal/FieldDescriptorProto.php index e5781975..77b99887 100644 --- a/php/src/Google/Protobuf/Internal/FieldDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/FieldDescriptorProto.php @@ -455,7 +455,7 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message * @param \Google\Protobuf\Internal\FieldOptions $var * @return $this */ - public function setOptions($var) + public function setOptions(\Google\Protobuf\Internal\FieldOptions $var = null) { GPBUtil::checkMessage($var, \Google\Protobuf\Internal\FieldOptions::class); $this->options = $var; diff --git a/php/src/Google/Protobuf/Internal/FileDescriptorProto.php b/php/src/Google/Protobuf/Internal/FileDescriptorProto.php index cb10aa79..4b6e3a34 100644 --- a/php/src/Google/Protobuf/Internal/FileDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/FileDescriptorProto.php @@ -429,7 +429,7 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message * @param \Google\Protobuf\Internal\FileOptions $var * @return $this */ - public function setOptions($var) + public function setOptions(\Google\Protobuf\Internal\FileOptions $var = null) { GPBUtil::checkMessage($var, \Google\Protobuf\Internal\FileOptions::class); $this->options = $var; @@ -467,7 +467,7 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message * @param \Google\Protobuf\Internal\SourceCodeInfo $var * @return $this */ - public function setSourceCodeInfo($var) + public function setSourceCodeInfo(\Google\Protobuf\Internal\SourceCodeInfo $var = null) { GPBUtil::checkMessage($var, \Google\Protobuf\Internal\SourceCodeInfo::class); $this->source_code_info = $var; diff --git a/php/src/Google/Protobuf/Internal/MethodDescriptorProto.php b/php/src/Google/Protobuf/Internal/MethodDescriptorProto.php index 1bd5dd3e..6186cd1c 100644 --- a/php/src/Google/Protobuf/Internal/MethodDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/MethodDescriptorProto.php @@ -182,7 +182,7 @@ class MethodDescriptorProto extends \Google\Protobuf\Internal\Message * @param \Google\Protobuf\Internal\MethodOptions $var * @return $this */ - public function setOptions($var) + public function setOptions(\Google\Protobuf\Internal\MethodOptions $var = null) { GPBUtil::checkMessage($var, \Google\Protobuf\Internal\MethodOptions::class); $this->options = $var; diff --git a/php/src/Google/Protobuf/Internal/OneofDescriptorProto.php b/php/src/Google/Protobuf/Internal/OneofDescriptorProto.php index 9ecfe5cb..d4105e46 100644 --- a/php/src/Google/Protobuf/Internal/OneofDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/OneofDescriptorProto.php @@ -85,7 +85,7 @@ class OneofDescriptorProto extends \Google\Protobuf\Internal\Message * @param \Google\Protobuf\Internal\OneofOptions $var * @return $this */ - public function setOptions($var) + public function setOptions(\Google\Protobuf\Internal\OneofOptions $var = null) { GPBUtil::checkMessage($var, \Google\Protobuf\Internal\OneofOptions::class); $this->options = $var; diff --git a/php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php b/php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php index 8de7afd0..f5b4cee5 100644 --- a/php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php @@ -119,7 +119,7 @@ class ServiceDescriptorProto extends \Google\Protobuf\Internal\Message * @param \Google\Protobuf\Internal\ServiceOptions $var * @return $this */ - public function setOptions($var) + public function setOptions(\Google\Protobuf\Internal\ServiceOptions $var = null) { GPBUtil::checkMessage($var, \Google\Protobuf\Internal\ServiceOptions::class); $this->options = $var; diff --git a/src/google/protobuf/compiler/php/php_generator.cc b/src/google/protobuf/compiler/php/php_generator.cc index cdb2f509..0bfe10c9 100644 --- a/src/google/protobuf/compiler/php/php_generator.cc +++ b/src/google/protobuf/compiler/php/php_generator.cc @@ -664,10 +664,18 @@ void GenerateFieldAccessor(const FieldDescriptor* field, bool is_descriptor, // Generate setter. GenerateFieldDocComment(printer, field, is_descriptor, kFieldSetter); - printer->Print( - "public function set^camel_name^($var)\n" - "{\n", - "camel_name", UnderscoresToCamelCase(field->name(), true)); + if (field->type() == FieldDescriptor::TYPE_MESSAGE && !field->is_repeated()) { + printer->Print( + "public function set^camel_name^(^typehint^ $var = null)\n" + "{\n", + "camel_name", UnderscoresToCamelCase(field->name(), true), + "typehint", "\\" + FullClassName(field->message_type(), is_descriptor)); + } else { + printer->Print( + "public function set^camel_name^($var)\n" + "{\n", + "camel_name", UnderscoresToCamelCase(field->name(), true)); + } Indent(printer); -- cgit v1.2.3