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 1 commit
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 and will be removed in the next major release') {
$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
36 changes: 29 additions & 7 deletions src/google/protobuf/compiler/php/php_generator.cc
Expand Up @@ -644,43 +644,58 @@ void GenerateFieldAccessor(const FieldDescriptor* field, const Options& options,
// Generate getter.
GenerateFieldDocComment(printer, field, options, kFieldGetter);

// deprecation
std::string deprecation_std_string = (field->options().deprecated()) ? "@trigger_error('" +
cawolf marked this conversation as resolved.
Show resolved Hide resolved
field->name() +
" is deprecated and will be removed in the next major release', E_USER_DEPRECATED);" : "";
cawolf marked this conversation as resolved.
Show resolved Hide resolved
const char* deprecation_string = deprecation_std_string.c_str();

if (oneof != NULL) {
printer->Print(
"public function get^camel_name^()\n"
"{\n"
" ^deprecation_trigger^\n"
cawolf marked this conversation as resolved.
Show resolved Hide resolved
" return $this->readOneof(^number^);\n"
"}\n\n"
"public function has^camel_name^()\n"
"{\n"
" ^deprecation_trigger^\n"
" return $this->hasOneof(^number^);\n"
"}\n\n",
"camel_name", UnderscoresToCamelCase(field->name(), true),
"number", IntToString(field->number()));
"number", IntToString(field->number()),
"deprecation_trigger", deprecation_string);
} else if (field->has_presence()) {
printer->Print(
"public function get^camel_name^()\n"
"{\n"
" ^deprecation_trigger^\n"
" return isset($this->^name^) ? $this->^name^ : ^default_value^;\n"
"}\n\n"
"public function has^camel_name^()\n"
"{\n"
" ^deprecation_trigger^\n"
" return isset($this->^name^);\n"
"}\n\n"
"public function clear^camel_name^()\n"
"{\n"
" ^deprecation_trigger^\n"
" 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_string);
} else {
printer->Print(
"public function get^camel_name^()\n"
"{\n"
" ^deprecation_trigger^\n"
" 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_string);
}

// For wrapper types, generate an additional getXXXUnwrapped getter
Expand All @@ -692,18 +707,22 @@ void GenerateFieldAccessor(const FieldDescriptor* field, const Options& options,
printer->Print(
"public function get^camel_name^Unwrapped()\n"
"{\n"
" ^deprecation_trigger^\n"
" 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_string);
}

// Generate setter.
GenerateFieldDocComment(printer, field, options, kFieldSetter);
printer->Print(
"public function set^camel_name^($var)\n"
"{\n",
"camel_name", UnderscoresToCamelCase(field->name(), true));
"{\n"
" ^deprecation_trigger^\n",
"camel_name", UnderscoresToCamelCase(field->name(), true),
"deprecation_trigger", deprecation_string);

Indent(printer);

Expand Down Expand Up @@ -1741,6 +1760,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