Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="tests/Unit/autoload.php"
colors="true"
convertDeprecationsToExceptions="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<coverage>
Expand Down
2 changes: 2 additions & 0 deletions src/Ast/HasAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Access

trait HasAccess
{
private Vector $access;

/**
* Create a version of this ast element with access modifiers.
*
Expand Down
5 changes: 4 additions & 1 deletion src/Generation/MethodDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ public function __construct($svc, $desc, $outputMsg, $pageSize, $pageToken, $nex
$this->resourcesSetter = AST::method($resources->getSetter());
$this->resourceType = Type::fromField($svc->catalog, $resources, false);
$this->resourcesFieldName = Helpers::toCamelCase($resources->getName());
$this->resourcesField = new FieldDetails($svc->catalog, $outputMsg, $resources->underlyingProto);
$resourcesProto = Vector::new($outputMsg->getField())
->filter(fn ($f) => $f->desc === $resources)
->firstOrNull();
$this->resourcesField = new FieldDetails($svc->catalog, $outputMsg, $resourcesProto);
$this->methodReturnType = Type::fromName(PagedListResponse::class);
// Override docs for page_size and page_token fields.
$this->requiredFields = $this->overrideFieldDocs($svc->catalog, $outputMsg, $this->requiredFields);
Expand Down
6 changes: 0 additions & 6 deletions src/Utils/ProtoAugmenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ private static function augmentFile(FileDescriptorProto $fileProto)
$fnMergeEnums = function (Vector $path, int $pathId, $proto, $desc) use ($locsByPath, $fnLeadingComments) {
$enums = Vector::zip(Vector::new($proto->getEnumType()), Vector::new($desc->getEnumType()));
foreach ($enums as $enumIndex => [$enumProto, $enumDesc]) {
// Link proto and desc in both directions.
$enumProto->desc = $enumDesc;
$enumDesc->underlyingProto = $enumProto;
// Link proto comments.
$enumPath = $path->concat(Vector::new([$pathId, $enumIndex]));
$enumLocations = $locsByPath->get($enumPath, null);
Expand All @@ -88,9 +86,7 @@ private static function augmentFile(FileDescriptorProto $fileProto)

$fnMergeMsgs = null;
$fnMergeMsgs = function (Vector $msgPath, DescriptorProto $msgProto, Descriptor $msgDesc) use (&$fnMergeMsgs, $locsByPath, $fnLeadingComments, $fnTrailingComments, $fnMergeEnums) {
// Link proto and desc in both directions.
$msgProto->desc = $msgDesc;
$msgDesc->underlyingProto = $msgProto;
// Link proto comments.
$msgLocations = $locsByPath->get($msgPath, null);
$msgProto->leadingComments = static::getComments($msgLocations, $fnLeadingComments);
Expand All @@ -103,9 +99,7 @@ private static function augmentFile(FileDescriptorProto $fileProto)
// Handle fields:
$fields = Vector::zip(Vector::new($msgProto->getField()), Vector::new($msgDesc->getField()));
foreach ($fields as $fieldIndex => [$fieldProto, $fieldDesc]) {
// Link proto and desc in both directions.
$fieldProto->desc = $fieldDesc;
$fieldDesc->underlyingProto = $fieldProto;
// Link proto comments.
$fieldPath = $msgPath->concat(Vector::new([static::MESSAGE_FIELD, $fieldIndex]));
$fieldLocations = $locsByPath->get($fieldPath, null);
Expand Down
25 changes: 6 additions & 19 deletions src/Utils/ProtoHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,30 +388,18 @@ private static function getCustomOptionRaw(Message $message, int $optionId, bool
return $repeated ? Vector::new($values) : null;
}

private static function conformMessage($message): Message
{
if (isset($message->underlyingProto)) {
$message = $message->underlyingProto;
}
if (!($message instanceof Message)) {
throw new Exception('Can only get custom option of Message or HasPublicDescriptorTrait');
}
return $message;
}

/**
* Get a non-repeated custom option. The option can be of any type, which determines the return type.
*
* @param mixed $message The message containing the custom option. Must be of type Message, or
* a descriptor with an `underlyingProto` property.
* @param Message $message The message containing the custom option.
* @param int $optionId The option-id of the option to get.
* @param ?string $msgClass Optional; to return a proto msg, set this to the PHP class of the msg.
*
* @return mixed Will be null if the option does not exist.
*/
public static function getCustomOption($message, int $optionId, ?string $msgClass = null)
public static function getCustomOption(Message $message, int $optionId, ?string $msgClass = null)
{
$result = static::getCustomOptionRaw(static::conformMessage($message), $optionId, false);
$result = static::getCustomOptionRaw($message, $optionId, false);
if (!is_null($msgClass) && !is_null($result)) {
$msg = new $msgClass();
$msg->mergeFromString($result);
Expand All @@ -423,15 +411,14 @@ public static function getCustomOption($message, int $optionId, ?string $msgClas
/**
* Get a repeated custom option. The option can be of any type, which determines to content of the returned Vector.
*
* @param mixed $message The message containing the custom option. Must be of type Message, or
* a descriptor with an `underlyingProto` property.
* @param Message $message The message containing the custom option.
* @param int $optionId The option-id of the option to get.
*
* @return Vector Will be an empty Vector if the option does not exist.
*/
public static function getCustomOptionRepeated($message, int $optionId, ?string $msgClass = null): Vector
public static function getCustomOptionRepeated(Message $message, int $optionId, ?string $msgClass = null): Vector
{
$result = static::getCustomOptionRaw(static::conformMessage($message), $optionId, true);
$result = static::getCustomOptionRaw($message, $optionId, true);
if (!is_null($msgClass)) {
$result = $result->map(function ($x) use ($msgClass) {
$msg = new $msgClass();
Expand Down
3 changes: 1 addition & 2 deletions tests/Unit/Collections/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ class Obj

class ObjEq implements Equality
{
public function __construct($id)
public function __construct(private $id)
{
$this->id = $id;
}

public function getHash(): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Bar
* @runInSeparateProcess
* @dataProvider provideAstMethodReplacer
*/
public function testAstMethodReplacer(string $classContents, string $insertBeforeMethod = null)
public function testAstMethodReplacer(string $classContents, ?string $insertBeforeMethod = null)
{
// the class / method to insert into
// if no method is defined, the first method is used ("__construct" in this case)
Expand Down
Loading