diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3abf16029..bb1454ca7 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -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" > diff --git a/src/Ast/HasAccess.php b/src/Ast/HasAccess.php index 13c109b04..61cd5d1ae 100644 --- a/src/Ast/HasAccess.php +++ b/src/Ast/HasAccess.php @@ -29,6 +29,8 @@ class Access trait HasAccess { + private Vector $access; + /** * Create a version of this ast element with access modifiers. * diff --git a/src/Collections/Vector.php b/src/Collections/Vector.php index 70f7d7006..f3efaeb63 100644 --- a/src/Collections/Vector.php +++ b/src/Collections/Vector.php @@ -145,8 +145,8 @@ public function getHash(): int { $hash = 1; foreach ($this->data as $item) { - $hash = (int) ($hash * 13); - $hash ^= static::hash($item); + // Modulo before multiplying so $hash * 13 never overflows PHP_INT_MAX into a float. + $hash = (($hash % intdiv(PHP_INT_MAX, 13)) * 13) ^ static::hash($item); } return $hash; } diff --git a/src/Generation/MethodDetails.php b/src/Generation/MethodDetails.php index 828ac534e..4b74f66ac 100644 --- a/src/Generation/MethodDetails.php +++ b/src/Generation/MethodDetails.php @@ -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); diff --git a/src/Utils/ProtoAugmenter.php b/src/Utils/ProtoAugmenter.php index 63f7fc278..aac76cdc8 100644 --- a/src/Utils/ProtoAugmenter.php +++ b/src/Utils/ProtoAugmenter.php @@ -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); @@ -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); @@ -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); diff --git a/src/Utils/ProtoHelpers.php b/src/Utils/ProtoHelpers.php index 1dc815c68..4119f9d7f 100644 --- a/src/Utils/ProtoHelpers.php +++ b/src/Utils/ProtoHelpers.php @@ -348,7 +348,6 @@ private static function getCustomOptionRaw(Message $message, int $optionId, bool if (!$messageUnknown) { $ref = new \ReflectionClass('Google\Protobuf\Internal\Message'); $messageUnknown = $ref->getProperty('unknown'); - $messageUnknown->setAccessible(true); } $values = []; @@ -388,30 +387,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); @@ -423,15 +410,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(); diff --git a/tests/Unit/Collections/MapTest.php b/tests/Unit/Collections/MapTest.php index 0d3292c49..5443b2242 100644 --- a/tests/Unit/Collections/MapTest.php +++ b/tests/Unit/Collections/MapTest.php @@ -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