Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions dev/src/DocFx/Node/XrefTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace Google\Cloud\Dev\DocFx\Node;

use Google\Cloud\Dev\DocFx\ReservedNames;

/**
* @internal
*/
Expand Down Expand Up @@ -115,6 +117,14 @@ function ($matches) {

$classParts = empty($class) ? [] : explode('.', $class);

// check for reserved names
$classParts = array_map(
fn ($name) => (false === array_search(
strtolower($name), ReservedNames::RESERVED_NAMES
) ? '' : 'PB') . $name,
$classParts
);

if ($property) {
// Convert the underscore property name to camel case getter method name
$property = ltrim($property, '.');
Expand Down
31 changes: 31 additions & 0 deletions dev/src/DocFx/ReservedNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Google\Cloud\Dev\DocFx;

class ReservedNames
{
/**
* Copied from protocolbuffers `src/google/protobuf/compiler/php/names.cc`.
* Keep this list up-to-date if modified for future PHP versions
*
* @see https://github.com/protocolbuffers/protobuf/blob/00a9043d0d79b65b47a048a20d01b16618c8396a/src/google/protobuf/compiler/php/names.cc#L18-L34
*/
public const RESERVED_NAMES = [
'abstract', 'and', 'array', 'as', 'break',
'callable', 'case', 'catch', 'class', 'clone',
'const', 'continue', 'declare', 'default', 'die',
'do', 'echo', 'else', 'elseif', 'empty',
'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch',
'endwhile', 'eval', 'exit', 'extends', 'final',
'finally', 'fn', 'for', 'foreach', 'function',
'global', 'goto', 'if', 'implements', 'include',
'include_once', 'instanceof', 'insteadof', 'interface', 'isset',
'list', 'match', 'namespace', 'new', 'or',
'parent', 'print', 'private', 'protected', 'public',
'readonly', 'require', 'require_once', 'return', 'self',
'static', 'switch', 'throw', 'trait', 'try',
'unset', 'use', 'var', 'while', 'xor',
'yield', 'int', 'float', 'bool', 'string',
'true', 'false', 'null', 'void', 'iterable',
];
}
Comment thread
bshaffer marked this conversation as resolved.
Outdated
13 changes: 13 additions & 0 deletions dev/tests/Unit/DocFx/NodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ public function testReplaceProtoRefWithXref(string $description, string $expecte
private $protoPackages = [
'google.cloud.aiplatform.v1' => 'Google\\Cloud\\AIPlatform\\V1',
'google.bigtable.admin.v2' => 'Google\\Cloud\\Bigtable\\Admin\\V2',
'google.bigtable.v2' => 'Google\\Cloud\\Bigtable\\V2',
'google.logging.v2' => 'Google\\Cloud\\Logging\\V2',
'google.cloud.servicedirectory.v1' => 'Google\\Cloud\\ServiceDirectory\\V1',
];
public function replace(string $description) {
return $this->replaceProtoRef($description);
Expand Down Expand Up @@ -273,6 +275,17 @@ public function provideReplaceProtoRefWithXref()
. ' <xref uid="\Google\Cloud\AIPlatform\V1\PredictSchemata::getInstanceSchemaUri()">instance_schema_uri</xref>.'
],
[
// Reserved word used in class name
'[Namespace][google.cloud.servicedirectory.v1.Namespace]',
'<xref uid="\Google\Cloud\ServiceDirectory\V1\PBNamespace">Namespace</xref>',
],
[
// Reserved word used in path name
'[Encoding][google.bigtable.v2.Type.String.Encoding]',
'<xref uid="\Google\Cloud\Bigtable\V2\Type\PBString\Encoding">Encoding</xref>',
],
[
// Parenthesis in code samples should not be modified
'Testing that a code sample like $foo["bar"]["baz"] does not get replaced',
'Testing that a code sample like $foo["bar"]["baz"] does not get replaced'
],
Expand Down