Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions php/tests/GeneratedClassTest.php
Expand Up @@ -71,6 +71,33 @@ public function testInt32Field()
$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.
#########################################################
Expand Down
7 changes: 7 additions & 0 deletions php/tests/GeneratedPhpdocTest.php
Expand Up @@ -340,6 +340,13 @@ public function providePhpDocForGettersAndSetters()
],
'@param \NoNamespaceMessage $var'
],
[
[
'setDeprecatedOptionalInt32',
'getDeprecatedOptionalInt32',
],
'@deprecated'
],
];
}
}
3 changes: 3 additions & 0 deletions php/tests/proto/test.proto
Expand Up @@ -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 {
Expand Down
42 changes: 30 additions & 12 deletions src/google/protobuf/compiler/php/php_generator.cc
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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();
Expand Down Expand Up @@ -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");
}

Expand Down