diff --git a/php/src/Google/Protobuf/FieldDescriptor.php b/php/src/Google/Protobuf/FieldDescriptor.php index 6e5eeca9ff..ac919a24a9 100644 --- a/php/src/Google/Protobuf/FieldDescriptor.php +++ b/php/src/Google/Protobuf/FieldDescriptor.php @@ -82,14 +82,6 @@ class FieldDescriptor return $this->internal_desc->getType(); } - /** - * @return int - */ - public function getOneofIndex() - { - return $this->internal_desc->getOneofIndex(); - } - /** * @return OneofDescriptor */ diff --git a/php/src/Google/Protobuf/Internal/Descriptor.php b/php/src/Google/Protobuf/Internal/Descriptor.php index a965158ebe..02b5433290 100644 --- a/php/src/Google/Protobuf/Internal/Descriptor.php +++ b/php/src/Google/Protobuf/Internal/Descriptor.php @@ -193,10 +193,6 @@ class Descriptor $desc->setLegacyClass($legacy_classname); $desc->setOptions($proto->getOptions()); - foreach ($proto->getField() as $field_proto) { - $desc->addField(FieldDescriptor::buildFromProto($field_proto, $desc)); - } - // Handle nested types. foreach ($proto->getNestedType() as $nested_proto) { $desc->addNestedType(Descriptor::buildFromProto( @@ -217,6 +213,11 @@ class Descriptor $index++; } + // Pass the descriptor to build FieldDescriptor after the OneofDescriptors are populated. + foreach ($proto->getField() as $field_proto) { + $desc->addField(FieldDescriptor::buildFromProto($field_proto, $desc)); + } + return $desc; } } diff --git a/php/src/Google/Protobuf/Internal/FieldDescriptor.php b/php/src/Google/Protobuf/Internal/FieldDescriptor.php index 7ab30dd1c0..875280b75c 100644 --- a/php/src/Google/Protobuf/Internal/FieldDescriptor.php +++ b/php/src/Google/Protobuf/Internal/FieldDescriptor.php @@ -49,8 +49,6 @@ class FieldDescriptor private $oneof_index = -1; private $proto3_optional; - /** @var Descriptor $containing_type */ - private $containing_type; /** @var OneofDescriptor $containing_oneof */ private $containing_oneof; @@ -242,10 +240,10 @@ class FieldDescriptor /** * @param FieldDescriptorProto $proto - * @param Descriptor $parent_desc + * @param Descriptor $desc * @return FieldDescriptor */ - public static function getFieldDescriptor($proto, $parent_desc) + public static function getFieldDescriptor($proto, $desc) { $type_name = null; $type = $proto->getType(); @@ -261,10 +259,10 @@ class FieldDescriptor if ($proto->hasOneofIndex()) { $oneof_index = $proto->getOneofIndex(); - $containing_oneof = $parent_desc->getOneofDecl()[$oneof_index]; + $containing_oneof = $desc->getOneofDecl()[$oneof_index]; } else { - $containing_oneof = null; $oneof_index = -1; + $containing_oneof = null; } // TODO: once proto2 is supported, this default should be false // for proto2. @@ -284,7 +282,6 @@ class FieldDescriptor $field = new FieldDescriptor(); $field->setName($proto->getName()); - $field->containing_type = $parent_desc; $field->containing_oneof = $containing_oneof; $json_name = $proto->hasJsonName() ? $proto->getJsonName() : diff --git a/php/src/Google/Protobuf/Internal/OneofDescriptor.php b/php/src/Google/Protobuf/Internal/OneofDescriptor.php index e685b26701..a74be1e585 100644 --- a/php/src/Google/Protobuf/Internal/OneofDescriptor.php +++ b/php/src/Google/Protobuf/Internal/OneofDescriptor.php @@ -67,7 +67,8 @@ class OneofDescriptor public function isSynthetic() { - return count($this->fields) === 1 && $this->fields[0]->getProto3Optional(); + return !is_null($this->fields) && count($this->fields) === 1 + && $this->fields[0]->getProto3Optional(); } public static function buildFromProto($oneof_proto, $desc, $index)