Forráskód Böngészése

adds PHP typehints for message setters (#4533)

Brent Shaffer 7 éve
szülő
commit
abac808000

+ 1 - 1
php/src/Google/Protobuf/Internal/DescriptorProto.php

@@ -306,7 +306,7 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message
      * @param \Google\Protobuf\Internal\MessageOptions $var
      * @param \Google\Protobuf\Internal\MessageOptions $var
      * @return $this
      * @return $this
      */
      */
-    public function setOptions($var)
+    public function setOptions(\Google\Protobuf\Internal\MessageOptions $var = null)
     {
     {
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\MessageOptions::class);
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\MessageOptions::class);
         $this->options = $var;
         $this->options = $var;

+ 1 - 1
php/src/Google/Protobuf/Internal/EnumDescriptorProto.php

@@ -143,7 +143,7 @@ class EnumDescriptorProto extends \Google\Protobuf\Internal\Message
      * @param \Google\Protobuf\Internal\EnumOptions $var
      * @param \Google\Protobuf\Internal\EnumOptions $var
      * @return $this
      * @return $this
      */
      */
-    public function setOptions($var)
+    public function setOptions(\Google\Protobuf\Internal\EnumOptions $var = null)
     {
     {
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\EnumOptions::class);
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\EnumOptions::class);
         $this->options = $var;
         $this->options = $var;

+ 1 - 1
php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php

@@ -119,7 +119,7 @@ class EnumValueDescriptorProto extends \Google\Protobuf\Internal\Message
      * @param \Google\Protobuf\Internal\EnumValueOptions $var
      * @param \Google\Protobuf\Internal\EnumValueOptions $var
      * @return $this
      * @return $this
      */
      */
-    public function setOptions($var)
+    public function setOptions(\Google\Protobuf\Internal\EnumValueOptions $var = null)
     {
     {
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\EnumValueOptions::class);
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\EnumValueOptions::class);
         $this->options = $var;
         $this->options = $var;

+ 1 - 1
php/src/Google/Protobuf/Internal/FieldDescriptorProto.php

@@ -455,7 +455,7 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message
      * @param \Google\Protobuf\Internal\FieldOptions $var
      * @param \Google\Protobuf\Internal\FieldOptions $var
      * @return $this
      * @return $this
      */
      */
-    public function setOptions($var)
+    public function setOptions(\Google\Protobuf\Internal\FieldOptions $var = null)
     {
     {
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\FieldOptions::class);
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\FieldOptions::class);
         $this->options = $var;
         $this->options = $var;

+ 2 - 2
php/src/Google/Protobuf/Internal/FileDescriptorProto.php

@@ -429,7 +429,7 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
      * @param \Google\Protobuf\Internal\FileOptions $var
      * @param \Google\Protobuf\Internal\FileOptions $var
      * @return $this
      * @return $this
      */
      */
-    public function setOptions($var)
+    public function setOptions(\Google\Protobuf\Internal\FileOptions $var = null)
     {
     {
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\FileOptions::class);
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\FileOptions::class);
         $this->options = $var;
         $this->options = $var;
@@ -467,7 +467,7 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message
      * @param \Google\Protobuf\Internal\SourceCodeInfo $var
      * @param \Google\Protobuf\Internal\SourceCodeInfo $var
      * @return $this
      * @return $this
      */
      */
-    public function setSourceCodeInfo($var)
+    public function setSourceCodeInfo(\Google\Protobuf\Internal\SourceCodeInfo $var = null)
     {
     {
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\SourceCodeInfo::class);
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\SourceCodeInfo::class);
         $this->source_code_info = $var;
         $this->source_code_info = $var;

+ 1 - 1
php/src/Google/Protobuf/Internal/MethodDescriptorProto.php

@@ -182,7 +182,7 @@ class MethodDescriptorProto extends \Google\Protobuf\Internal\Message
      * @param \Google\Protobuf\Internal\MethodOptions $var
      * @param \Google\Protobuf\Internal\MethodOptions $var
      * @return $this
      * @return $this
      */
      */
-    public function setOptions($var)
+    public function setOptions(\Google\Protobuf\Internal\MethodOptions $var = null)
     {
     {
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\MethodOptions::class);
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\MethodOptions::class);
         $this->options = $var;
         $this->options = $var;

+ 1 - 1
php/src/Google/Protobuf/Internal/OneofDescriptorProto.php

@@ -85,7 +85,7 @@ class OneofDescriptorProto extends \Google\Protobuf\Internal\Message
      * @param \Google\Protobuf\Internal\OneofOptions $var
      * @param \Google\Protobuf\Internal\OneofOptions $var
      * @return $this
      * @return $this
      */
      */
-    public function setOptions($var)
+    public function setOptions(\Google\Protobuf\Internal\OneofOptions $var = null)
     {
     {
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\OneofOptions::class);
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\OneofOptions::class);
         $this->options = $var;
         $this->options = $var;

+ 1 - 1
php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php

@@ -119,7 +119,7 @@ class ServiceDescriptorProto extends \Google\Protobuf\Internal\Message
      * @param \Google\Protobuf\Internal\ServiceOptions $var
      * @param \Google\Protobuf\Internal\ServiceOptions $var
      * @return $this
      * @return $this
      */
      */
-    public function setOptions($var)
+    public function setOptions(\Google\Protobuf\Internal\ServiceOptions $var = null)
     {
     {
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\ServiceOptions::class);
         GPBUtil::checkMessage($var, \Google\Protobuf\Internal\ServiceOptions::class);
         $this->options = $var;
         $this->options = $var;

+ 12 - 4
src/google/protobuf/compiler/php/php_generator.cc

@@ -664,10 +664,18 @@ void GenerateFieldAccessor(const FieldDescriptor* field, bool is_descriptor,
 
 
   // Generate setter.
   // Generate setter.
   GenerateFieldDocComment(printer, field, is_descriptor, kFieldSetter);
   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);
   Indent(printer);