feat: add support for deprecated fields to PHP compiler (#8223)

* feat: add support for deprecated fields to PHP compiler

* PR feedback 1
This commit is contained in:
Christian Alexander Wolf 2021-01-29 20:33:27 +01:00 committed by GitHub
parent 69694080ff
commit f4d0f7c85e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 12 deletions

View File

@ -71,6 +71,33 @@ class GeneratedClassTest extends TestBase
$this->assertSame(MIN_INT32, $m->getOptionalInt32());
}
#########################################################
# Test deprecated int32 field.
#########################################################
public function testDeprecatedInt32Field()
{
$m = new TestMessage();
// temporarily change error handler to capture the deprecated errors
$deprecationCount = 0;
set_error_handler(function ($errno, $errstr) use (&$deprecationCount) {
if ($errstr === 'deprecated_optional_int32 is deprecated.') {
$deprecationCount++;
}
}, E_USER_DEPRECATED);
// default test set
$m->setDeprecatedOptionalInt32(MAX_INT32);
$this->assertSame(MAX_INT32, $m->getDeprecatedOptionalInt32());
$m->setDeprecatedOptionalInt32(MIN_INT32);
$this->assertSame(MIN_INT32, $m->getDeprecatedOptionalInt32());
restore_error_handler();
$this->assertSame(4, $deprecationCount);
}
#########################################################
# Test optional int32 field.
#########################################################

View File

@ -340,6 +340,13 @@ class GeneratedPhpdocTest extends TestBase
],
'@param \NoNamespaceMessage $var'
],
[
[
'setDeprecatedOptionalInt32',
'getDeprecatedOptionalInt32',
],
'@deprecated'
],
];
}
}

View File

@ -147,6 +147,9 @@ message TestMessage {
map<string, google.protobuf.Any> map_string_any = 122;
map<string, google.protobuf.ListValue> map_string_list = 123;
map<string, google.protobuf.Struct> map_string_struct = 124;
// deprecated field
int32 deprecated_optional_int32 = 125 [deprecated=true];
}
enum TestEnum {

View File

@ -644,43 +644,50 @@ void GenerateFieldAccessor(const FieldDescriptor* field, const Options& options,
// Generate getter.
GenerateFieldDocComment(printer, field, options, kFieldGetter);
// deprecation
std::string deprecation_trigger = (field->options().deprecated()) ? "@trigger_error('" +
field->name() + " is deprecated.', E_USER_DEPRECATED);\n " : "";
if (oneof != NULL) {
printer->Print(
"public function get^camel_name^()\n"
"{\n"
" return $this->readOneof(^number^);\n"
" ^deprecation_trigger^return $this->readOneof(^number^);\n"
"}\n\n"
"public function has^camel_name^()\n"
"{\n"
" return $this->hasOneof(^number^);\n"
" ^deprecation_trigger^return $this->hasOneof(^number^);\n"
"}\n\n",
"camel_name", UnderscoresToCamelCase(field->name(), true),
"number", IntToString(field->number()));
"number", IntToString(field->number()),
"deprecation_trigger", deprecation_trigger);
} else if (field->has_presence()) {
printer->Print(
"public function get^camel_name^()\n"
"{\n"
" return isset($this->^name^) ? $this->^name^ : ^default_value^;\n"
" ^deprecation_trigger^return isset($this->^name^) ? $this->^name^ : ^default_value^;\n"
"}\n\n"
"public function has^camel_name^()\n"
"{\n"
" return isset($this->^name^);\n"
" ^deprecation_trigger^return isset($this->^name^);\n"
"}\n\n"
"public function clear^camel_name^()\n"
"{\n"
" unset($this->^name^);\n"
" ^deprecation_trigger^unset($this->^name^);\n"
"}\n\n",
"camel_name", UnderscoresToCamelCase(field->name(), true),
"name", field->name(),
"default_value", DefaultForField(field));
"default_value", DefaultForField(field),
"deprecation_trigger", deprecation_trigger);
} else {
printer->Print(
"public function get^camel_name^()\n"
"{\n"
" return $this->^name^;\n"
" ^deprecation_trigger^return $this->^name^;\n"
"}\n\n",
"camel_name", UnderscoresToCamelCase(field->name(), true), "name",
field->name());
"camel_name", UnderscoresToCamelCase(field->name(), true),
"name", field->name(),
"deprecation_trigger", deprecation_trigger);
}
// For wrapper types, generate an additional getXXXUnwrapped getter
@ -692,10 +699,11 @@ void GenerateFieldAccessor(const FieldDescriptor* field, const Options& options,
printer->Print(
"public function get^camel_name^Unwrapped()\n"
"{\n"
" return $this->readWrapperValue(\"^field_name^\");\n"
" ^deprecation_trigger^return $this->readWrapperValue(\"^field_name^\");\n"
"}\n\n",
"camel_name", UnderscoresToCamelCase(field->name(), true),
"field_name", field->name());
"field_name", field->name(),
"deprecation_trigger", deprecation_trigger);
}
// Generate setter.
@ -707,6 +715,13 @@ void GenerateFieldAccessor(const FieldDescriptor* field, const Options& options,
Indent(printer);
if (field->options().deprecated()) {
printer->Print(
"^deprecation_trigger^",
"deprecation_trigger", deprecation_trigger
);
}
// Type check.
if (field->is_map()) {
const Descriptor* map_entry = field->message_type();
@ -1741,6 +1756,9 @@ void GenerateFieldDocComment(io::Printer* printer, const FieldDescriptor* field,
"php_type", PhpGetterTypeName(field, options),
"maybe_null", can_return_null ? "|null" : "");
}
if (field->options().deprecated()) {
printer->Print(" * @deprecated\n");
}
printer->Print(" */\n");
}