Skip to content

Commit f4d0f7c

Browse files
authored
feat: add support for deprecated fields to PHP compiler (protocolbuffers#8223)
* feat: add support for deprecated fields to PHP compiler * PR feedback 1
1 parent 6969408 commit f4d0f7c

File tree

4 files changed

+67
-12
lines changed

4 files changed

+67
-12
lines changed

php/tests/GeneratedClassTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,33 @@ public function testInt32Field()
7171
$this->assertSame(MIN_INT32, $m->getOptionalInt32());
7272
}
7373

74+
#########################################################
75+
# Test deprecated int32 field.
76+
#########################################################
77+
78+
public function testDeprecatedInt32Field()
79+
{
80+
$m = new TestMessage();
81+
82+
// temporarily change error handler to capture the deprecated errors
83+
$deprecationCount = 0;
84+
set_error_handler(function ($errno, $errstr) use (&$deprecationCount) {
85+
if ($errstr === 'deprecated_optional_int32 is deprecated.') {
86+
$deprecationCount++;
87+
}
88+
}, E_USER_DEPRECATED);
89+
90+
// default test set
91+
$m->setDeprecatedOptionalInt32(MAX_INT32);
92+
$this->assertSame(MAX_INT32, $m->getDeprecatedOptionalInt32());
93+
$m->setDeprecatedOptionalInt32(MIN_INT32);
94+
$this->assertSame(MIN_INT32, $m->getDeprecatedOptionalInt32());
95+
96+
restore_error_handler();
97+
98+
$this->assertSame(4, $deprecationCount);
99+
}
100+
74101
#########################################################
75102
# Test optional int32 field.
76103
#########################################################

php/tests/GeneratedPhpdocTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,13 @@ public function providePhpDocForGettersAndSetters()
340340
],
341341
'@param \NoNamespaceMessage $var'
342342
],
343+
[
344+
[
345+
'setDeprecatedOptionalInt32',
346+
'getDeprecatedOptionalInt32',
347+
],
348+
'@deprecated'
349+
],
343350
];
344351
}
345352
}

php/tests/proto/test.proto

+3
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ message TestMessage {
147147
map<string, google.protobuf.Any> map_string_any = 122;
148148
map<string, google.protobuf.ListValue> map_string_list = 123;
149149
map<string, google.protobuf.Struct> map_string_struct = 124;
150+
151+
// deprecated field
152+
int32 deprecated_optional_int32 = 125 [deprecated=true];
150153
}
151154

152155
enum TestEnum {

src/google/protobuf/compiler/php/php_generator.cc

+30-12
Original file line numberDiff line numberDiff line change
@@ -644,43 +644,50 @@ void GenerateFieldAccessor(const FieldDescriptor* field, const Options& options,
644644
// Generate getter.
645645
GenerateFieldDocComment(printer, field, options, kFieldGetter);
646646

647+
// deprecation
648+
std::string deprecation_trigger = (field->options().deprecated()) ? "@trigger_error('" +
649+
field->name() + " is deprecated.', E_USER_DEPRECATED);\n " : "";
650+
647651
if (oneof != NULL) {
648652
printer->Print(
649653
"public function get^camel_name^()\n"
650654
"{\n"
651-
" return $this->readOneof(^number^);\n"
655+
" ^deprecation_trigger^return $this->readOneof(^number^);\n"
652656
"}\n\n"
653657
"public function has^camel_name^()\n"
654658
"{\n"
655-
" return $this->hasOneof(^number^);\n"
659+
" ^deprecation_trigger^return $this->hasOneof(^number^);\n"
656660
"}\n\n",
657661
"camel_name", UnderscoresToCamelCase(field->name(), true),
658-
"number", IntToString(field->number()));
662+
"number", IntToString(field->number()),
663+
"deprecation_trigger", deprecation_trigger);
659664
} else if (field->has_presence()) {
660665
printer->Print(
661666
"public function get^camel_name^()\n"
662667
"{\n"
663-
" return isset($this->^name^) ? $this->^name^ : ^default_value^;\n"
668+
" ^deprecation_trigger^return isset($this->^name^) ? $this->^name^ : ^default_value^;\n"
664669
"}\n\n"
665670
"public function has^camel_name^()\n"
666671
"{\n"
667-
" return isset($this->^name^);\n"
672+
" ^deprecation_trigger^return isset($this->^name^);\n"
668673
"}\n\n"
669674
"public function clear^camel_name^()\n"
670675
"{\n"
671-
" unset($this->^name^);\n"
676+
" ^deprecation_trigger^unset($this->^name^);\n"
672677
"}\n\n",
673678
"camel_name", UnderscoresToCamelCase(field->name(), true),
674679
"name", field->name(),
675-
"default_value", DefaultForField(field));
680+
"default_value", DefaultForField(field),
681+
"deprecation_trigger", deprecation_trigger);
676682
} else {
677683
printer->Print(
678684
"public function get^camel_name^()\n"
679685
"{\n"
680-
" return $this->^name^;\n"
686+
" ^deprecation_trigger^return $this->^name^;\n"
681687
"}\n\n",
682-
"camel_name", UnderscoresToCamelCase(field->name(), true), "name",
683-
field->name());
688+
"camel_name", UnderscoresToCamelCase(field->name(), true),
689+
"name", field->name(),
690+
"deprecation_trigger", deprecation_trigger);
684691
}
685692

686693
// For wrapper types, generate an additional getXXXUnwrapped getter
@@ -692,10 +699,11 @@ void GenerateFieldAccessor(const FieldDescriptor* field, const Options& options,
692699
printer->Print(
693700
"public function get^camel_name^Unwrapped()\n"
694701
"{\n"
695-
" return $this->readWrapperValue(\"^field_name^\");\n"
702+
" ^deprecation_trigger^return $this->readWrapperValue(\"^field_name^\");\n"
696703
"}\n\n",
697704
"camel_name", UnderscoresToCamelCase(field->name(), true),
698-
"field_name", field->name());
705+
"field_name", field->name(),
706+
"deprecation_trigger", deprecation_trigger);
699707
}
700708

701709
// Generate setter.
@@ -707,6 +715,13 @@ void GenerateFieldAccessor(const FieldDescriptor* field, const Options& options,
707715

708716
Indent(printer);
709717

718+
if (field->options().deprecated()) {
719+
printer->Print(
720+
"^deprecation_trigger^",
721+
"deprecation_trigger", deprecation_trigger
722+
);
723+
}
724+
710725
// Type check.
711726
if (field->is_map()) {
712727
const Descriptor* map_entry = field->message_type();
@@ -1741,6 +1756,9 @@ void GenerateFieldDocComment(io::Printer* printer, const FieldDescriptor* field,
17411756
"php_type", PhpGetterTypeName(field, options),
17421757
"maybe_null", can_return_null ? "|null" : "");
17431758
}
1759+
if (field->options().deprecated()) {
1760+
printer->Print(" * @deprecated\n");
1761+
}
17441762
printer->Print(" */\n");
17451763
}
17461764

0 commit comments

Comments
 (0)