mirror of
https://github.com/protocolbuffers/protobuf
synced 2026-08-26 02:23:14 -04:00
See https://github.com/googleapis/google-cloud-php/issues/9193
`protoc` is generating the invalid class name `Object` when it should be prefixed with `PB` to prevent a conflict with PHP reserved words. I've submitted a fix for this in #27475
This PR introduces the script `php/update_reserved_words.sh` which updates the library in the **8 different classfiles** which all have various ways this logic is defined. Hopefully this makes these things easier for us to maintain in the future.
**NOTE**: prefixing constants with PB has been unnecessary since PHP 7, so we can and should open those up to every word but `class` in the next major version. I've added a comment so we (hopefully) don't forget
```sh
$ bash php/update_reserved_words.sh
Updated src/google/protobuf/compiler/php/names.cc
Updated php/ext/google/protobuf/names.c
Updated src/google/protobuf/compiler/php/php_generator.cc
Updated php/tests/proto/test_reserved_message_lower.proto
Updated php/tests/proto/test_reserved_message_upper.proto
Updated php/tests/proto/test_reserved_enum_lower.proto
Updated php/tests/proto/test_reserved_enum_upper.proto
Updated php/tests/proto/test_reserved_enum_value_lower.proto
Updated php/tests/proto/test_reserved_enum_value_upper.proto
Updated php/tests/GeneratedClassTest.php
Updated php/src/Google/Protobuf/Internal/GPBUtil.php
```
Closes #27456
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/27456 from bshaffer:php-update-reserved-words 522d93f1c1
PiperOrigin-RevId: 962903274
64 lines
2.4 KiB
PHP
64 lines
2.4 KiB
PHP
<?php
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class UpdateReservedTest extends TestCase
|
|
{
|
|
public function testBashScriptIdempotency()
|
|
{
|
|
$repo_root = dirname(dirname(__DIR__));
|
|
$script_path = $repo_root . '/php/update_reserved_words.sh';
|
|
|
|
$this->assertFileExists($script_path);
|
|
|
|
$files_to_check = [
|
|
'src/google/protobuf/compiler/php/names.cc',
|
|
'php/ext/google/protobuf/names.c',
|
|
'src/google/protobuf/compiler/php/php_generator.cc',
|
|
'php/tests/proto/test_reserved_message_lower.proto',
|
|
'php/tests/proto/test_reserved_message_upper.proto',
|
|
'php/tests/proto/test_reserved_enum_lower.proto',
|
|
'php/tests/proto/test_reserved_enum_upper.proto',
|
|
'php/tests/proto/test_reserved_enum_value_lower.proto',
|
|
'php/tests/proto/test_reserved_enum_value_upper.proto',
|
|
'php/tests/GeneratedClassTest.php',
|
|
];
|
|
|
|
// Store original contents
|
|
$original_contents = [];
|
|
foreach ($files_to_check as $file) {
|
|
$path = $repo_root . '/' . $file;
|
|
$this->assertFileExists($path);
|
|
$original_contents[$file] = file_get_contents($path);
|
|
}
|
|
|
|
try {
|
|
// Run the script (this will modify files in place)
|
|
$output = [];
|
|
$return_var = 0;
|
|
exec("bash " . escapeshellarg($script_path), $output, $return_var);
|
|
$this->assertSame(0, $return_var, "Script failed to execute: " . implode("\n", $output));
|
|
|
|
// Compare new contents with original
|
|
$changed_files = [];
|
|
foreach ($files_to_check as $file) {
|
|
$path = $repo_root . '/' . $file;
|
|
$new_content = file_get_contents($path);
|
|
if ($new_content !== $original_contents[$file]) {
|
|
$changed_files[] = $file;
|
|
}
|
|
}
|
|
|
|
$msg = "The following files were modified by the script (idempotency check failed). " .
|
|
"Please run 'bash php/update_reserved_words.sh' to sync them: " .
|
|
implode(", ", $changed_files);
|
|
$this->assertEmpty($changed_files, $msg);
|
|
} finally {
|
|
// ALWAYS restore original contents to keep filesystem clean
|
|
foreach ($original_contents as $file => $content) {
|
|
file_put_contents($repo_root . '/' . $file, $content);
|
|
}
|
|
}
|
|
}
|
|
}
|