From e400e2432475f0ab85d3fdd6eddbc995ba787a21 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 11 Dec 2024 10:10:54 -0800 Subject: [PATCH] fix: add PHP 8.4 compatibility to last release compatible with PHP 7.4 (#600) --- .github/workflows/tests.yml | 4 ++-- src/ApiException.php | 12 +++++------ src/Call.php | 2 +- src/CredentialsWrapper.php | 14 ++++++------- src/GapicClientTrait.php | 20 +++++++++---------- src/Page.php | 2 +- src/PathTemplate.php | 2 +- src/RequestBuilder.php | 2 +- src/ResourceHelperTrait.php | 2 +- src/ResourceTemplate/Parser.php | 2 +- .../RelativeResourceTemplate.php | 2 +- src/ResourceTemplate/Segment.php | 6 +++--- src/Testing/MockBidiStreamingCall.php | 2 +- src/Testing/MockClientStreamingCall.php | 2 +- src/Testing/MockServerStreamingCall.php | 2 +- src/Testing/MockStatus.php | 2 +- src/Testing/MockStubTrait.php | 8 ++++---- src/Testing/MockUnaryCall.php | 2 +- src/Transport/GrpcTransport.php | 2 +- 19 files changed, 45 insertions(+), 45 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9f3228a8c..106743fa9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: [ "7.4", "8.0", "8.1", "8.2", "8.3" ] + php: [ "7.4", "8.0", "8.1", "8.2", "8.3", "8.4" ] extensions: [""] tools: [""] composerflags: [""] @@ -74,5 +74,5 @@ jobs: - name: Run Script run: | composer install - composer global require phpstan/phpstan + composer global require phpstan/phpstan:^1.0 ~/.composer/vendor/bin/phpstan analyse diff --git a/src/ApiException.php b/src/ApiException.php index 7385f8b43..2b4128e23 100644 --- a/src/ApiException.php +++ b/src/ApiException.php @@ -62,7 +62,7 @@ class ApiException extends Exception public function __construct( string $message, int $code, - string $status = null, + ?string $status = null, array $optionalArgs = [] ) { $optionalArgs += [ @@ -163,8 +163,8 @@ public static function createFromStdClass(stdClass $status) public static function createFromApiResponse( $basicMessage, $rpcCode, - array $metadata = null, - Exception $previous = null + ?array $metadata = null, + ?Exception $previous = null ) { return self::create( $basicMessage, @@ -187,8 +187,8 @@ public static function createFromApiResponse( public static function createFromRestApiResponse( $basicMessage, $rpcCode, - array $metadata = null, - Exception $previous = null + ?array $metadata = null, + ?Exception $previous = null ) { return self::create( $basicMessage, @@ -244,7 +244,7 @@ private static function create( int $rpcCode, $metadata, array $decodedMetadata, - Exception $previous = null + ?Exception $previous = null ) { $containsErrorInfo = self::containsErrorInfo($decodedMetadata); $rpcStatus = ApiStatus::statusFromRpcCode($rpcCode); diff --git a/src/Call.php b/src/Call.php index b4c403579..53c5d32bb 100644 --- a/src/Call.php +++ b/src/Call.php @@ -61,7 +61,7 @@ class Call */ public function __construct( string $method, - string $decodeType = null, + ?string $decodeType = null, $message = null, $descriptor = [], int $callType = Call::UNARY_CALL diff --git a/src/CredentialsWrapper.php b/src/CredentialsWrapper.php index 6988188fa..1057596bd 100644 --- a/src/CredentialsWrapper.php +++ b/src/CredentialsWrapper.php @@ -77,7 +77,7 @@ class CredentialsWrapper implements ProjectIdProviderInterface */ public function __construct( FetchAuthTokenInterface $credentialsFetcher, - callable $authHttpHandler = null, + ?callable $authHttpHandler = null, string $universeDomain = GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN ) { $this->credentialsFetcher = $credentialsFetcher; @@ -203,7 +203,7 @@ public function getQuotaProject() return null; } - public function getProjectId(callable $httpHandler = null): ?string + public function getProjectId(?callable $httpHandler = null): ?string { // Ensure that FetchAuthTokenCache does not throw an exception if ($this->credentialsFetcher instanceof FetchAuthTokenCache @@ -313,12 +313,12 @@ private static function buildHttpHandlerFactory() * @throws ValidationException */ private static function buildApplicationDefaultCredentials( - array $scopes = null, - callable $authHttpHandler = null, - array $authCacheOptions = null, - CacheItemPoolInterface $authCache = null, + ?array $scopes = null, + ?callable $authHttpHandler = null, + ?array $authCacheOptions = null, + ?CacheItemPoolInterface $authCache = null, $quotaProject = null, - array $defaultScopes = null + ?array $defaultScopes = null ) { try { return ApplicationDefaultCredentials::getCredentials( diff --git a/src/GapicClientTrait.php b/src/GapicClientTrait.php index 500eb207b..d54601d0b 100644 --- a/src/GapicClientTrait.php +++ b/src/GapicClientTrait.php @@ -315,7 +315,7 @@ private function createTransport( string $apiEndpoint, $transport, $transportConfig, - callable $clientCertSource = null + ?callable $clientCertSource = null ) { if (!is_string($transport)) { throw new ValidationException( @@ -511,7 +511,7 @@ private function startAsyncCall( */ private function startApiCall( string $methodName, - Message $request = null, + ?Message $request = null, array $optionalArgs = [] ) { $methodDescriptors =$this->validateCallConfig($methodName); @@ -573,9 +573,9 @@ private function startCall( string $methodName, string $decodeType, array $optionalArgs = [], - Message $request = null, + ?Message $request = null, int $callType = Call::UNARY_CALL, - string $interfaceName = null + ?string $interfaceName = null ) { $optionalArgs = $this->configureCallOptions($optionalArgs); $callStack = $this->createCallStack( @@ -713,8 +713,8 @@ private function startOperationsCall( array $optionalArgs, Message $request, $client, - string $interfaceName = null, - string $operationClass = null + ?string $interfaceName = null, + ?string $operationClass = null ) { $optionalArgs = $this->configureCallOptions($optionalArgs); $callStack = $this->createCallStack( @@ -769,7 +769,7 @@ private function getPagedListResponse( array $optionalArgs, string $decodeType, Message $request, - string $interfaceName = null + ?string $interfaceName = null ) { return $this->getPagedListResponseAsync( $methodName, @@ -794,7 +794,7 @@ private function getPagedListResponseAsync( array $optionalArgs, string $decodeType, Message $request, - string $interfaceName = null + ?string $interfaceName = null ) { $optionalArgs = $this->configureCallOptions($optionalArgs); $callStack = $this->createCallStack( @@ -825,7 +825,7 @@ private function getPagedListResponseAsync( * * @return string */ - private function buildMethod(string $interfaceName = null, string $methodName = null) + private function buildMethod(?string $interfaceName = null, ?string $methodName = null) { return sprintf( '%s/%s', @@ -840,7 +840,7 @@ private function buildMethod(string $interfaceName = null, string $methodName = * * @return array */ - private function buildRequestParamsHeader(array $headerParams, Message $request = null) + private function buildRequestParamsHeader(array $headerParams, ?Message $request = null) { $headers = []; diff --git a/src/Page.php b/src/Page.php index fded0dcca..04ade0639 100644 --- a/src/Page.php +++ b/src/Page.php @@ -110,7 +110,7 @@ public function getNextPageToken() * @throws ApiException if the call to fetch the next page fails. * @return Page */ - public function getNextPage(int $pageSize = null) + public function getNextPage(?int $pageSize = null) { if (!$this->hasNextPage()) { throw new ValidationException( diff --git a/src/PathTemplate.php b/src/PathTemplate.php index b7cd5aa2e..4556acf07 100644 --- a/src/PathTemplate.php +++ b/src/PathTemplate.php @@ -54,7 +54,7 @@ class PathTemplate implements ResourceTemplateInterface * @param string $path A path template string * @throws ValidationException When $path cannot be parsed into a valid PathTemplate */ - public function __construct(string $path = null) + public function __construct(?string $path = null) { if (empty($path)) { throw new ValidationException('Cannot construct PathTemplate from empty string'); diff --git a/src/RequestBuilder.php b/src/RequestBuilder.php index c32f8fd97..e59e112f6 100644 --- a/src/RequestBuilder.php +++ b/src/RequestBuilder.php @@ -233,7 +233,7 @@ private function buildBindings(array $placeholders, Message $message) foreach ($placeholders as $placeholder => $metadata) { $value = array_reduce( $metadata['getters'], - function (Message $result = null, $getter) { + function (?Message $result = null, $getter = null) { if ($result) { return $result->$getter(); } diff --git a/src/ResourceHelperTrait.php b/src/ResourceHelperTrait.php index 7eafa99be..5ff489c84 100644 --- a/src/ResourceHelperTrait.php +++ b/src/ResourceHelperTrait.php @@ -84,7 +84,7 @@ private static function getPathTemplate(string $key) return self::$templateMap[$key] ?? null; } - private static function parseFormattedName(string $formattedName, string $template = null): array + private static function parseFormattedName(string $formattedName, ?string $template = null): array { if (is_null(self::$templateMap)) { self::registerPathTemplates(); diff --git a/src/ResourceTemplate/Parser.php b/src/ResourceTemplate/Parser.php index 9431aecdd..eb2f43ffc 100644 --- a/src/ResourceTemplate/Parser.php +++ b/src/ResourceTemplate/Parser.php @@ -48,7 +48,7 @@ class Parser * @return array * @throws ValidationException */ - public static function parseSegments(string $path = null) + public static function parseSegments(?string $path = null) { if (empty($path)) { throw new ValidationException("Cannot parse empty path"); diff --git a/src/ResourceTemplate/RelativeResourceTemplate.php b/src/ResourceTemplate/RelativeResourceTemplate.php index 6bc745103..7a674e274 100644 --- a/src/ResourceTemplate/RelativeResourceTemplate.php +++ b/src/ResourceTemplate/RelativeResourceTemplate.php @@ -287,7 +287,7 @@ private function renderingException(array $bindings, string $reason) * @param string|null $separator An optional string separator * @return array[] A list of [string, Segment] tuples */ - private static function buildKeySegmentTuples(array $segments, string $separator = null) + private static function buildKeySegmentTuples(array $segments, ?string $separator = null) { $keySegmentTuples = []; $positionalArgumentCounter = 0; diff --git a/src/ResourceTemplate/Segment.php b/src/ResourceTemplate/Segment.php index b9ae4fbff..0fedeb026 100644 --- a/src/ResourceTemplate/Segment.php +++ b/src/ResourceTemplate/Segment.php @@ -65,9 +65,9 @@ class Segment */ public function __construct( int $segmentType, - string $value = null, - string $key = null, - RelativeResourceTemplate $template = null, + ?string $value = null, + ?string $key = null, + ?RelativeResourceTemplate $template = null, string $separator = '/' ) { $this->segmentType = $segmentType; diff --git a/src/Testing/MockBidiStreamingCall.php b/src/Testing/MockBidiStreamingCall.php index 7ac698015..7a33c36f5 100644 --- a/src/Testing/MockBidiStreamingCall.php +++ b/src/Testing/MockBidiStreamingCall.php @@ -59,7 +59,7 @@ class MockBidiStreamingCall extends Grpc\BidiStreamingCall * @param mixed|null $deserialize An optional deserialize method for the response object. * @param stdClass|null $status An optional status object. If set to null, a status of OK is used. */ - public function __construct(array $responses, $deserialize = null, stdClass $status = null) + public function __construct(array $responses, $deserialize = null, ?stdClass $status = null) { $this->responses = $responses; $this->deserialize = $deserialize; diff --git a/src/Testing/MockClientStreamingCall.php b/src/Testing/MockClientStreamingCall.php index 6bf35c685..e53054fd8 100644 --- a/src/Testing/MockClientStreamingCall.php +++ b/src/Testing/MockClientStreamingCall.php @@ -62,7 +62,7 @@ class MockClientStreamingCall extends Grpc\ClientStreamingCall * @param callable|array|null $deserialize An optional deserialize method for the response object. * @param stdClass|null $status An optional status object. If set to null, a status of OK is used. */ - public function __construct($response, $deserialize = null, stdClass $status = null) + public function __construct($response, $deserialize = null, ?stdClass $status = null) { $this->mockUnaryCall = new MockUnaryCall($response, $deserialize, $status); } diff --git a/src/Testing/MockServerStreamingCall.php b/src/Testing/MockServerStreamingCall.php index dd6378bef..e21fcbb33 100644 --- a/src/Testing/MockServerStreamingCall.php +++ b/src/Testing/MockServerStreamingCall.php @@ -57,7 +57,7 @@ class MockServerStreamingCall extends \Grpc\ServerStreamingCall implements Serve * @param callable|array|null $deserialize An optional deserialize method for the response object. * @param stdClass|null $status An optional status object. If set to null, a status of OK is used. */ - public function __construct(array $responses, $deserialize = null, stdClass $status = null) + public function __construct(array $responses, $deserialize = null, ?stdClass $status = null) { $this->responses = $responses; $this->deserialize = $deserialize; diff --git a/src/Testing/MockStatus.php b/src/Testing/MockStatus.php index fb5ea340e..6f2906356 100644 --- a/src/Testing/MockStatus.php +++ b/src/Testing/MockStatus.php @@ -44,7 +44,7 @@ class MockStatus extends stdClass public $code; public $details; public $metadata; - public function __construct($code, string $details = null, array $metadata = []) + public function __construct($code, ?string $details = null, array $metadata = []) { $this->code = $code; $this->details = $details; diff --git a/src/Testing/MockStubTrait.php b/src/Testing/MockStubTrait.php index 7d7e58ae0..bb8180ba1 100644 --- a/src/Testing/MockStubTrait.php +++ b/src/Testing/MockStubTrait.php @@ -52,7 +52,7 @@ trait MockStubTrait private $callObjects = []; private $deserialize; - public function __construct(callable $deserialize = null) + public function __construct(?callable $deserialize = null) { $this->deserialize = $deserialize; } @@ -196,7 +196,7 @@ public static function stripStatusFromResponses($responses) * @param \Google\Protobuf\Internal\Message $response * @param stdClass $status */ - public function addResponse($response, stdClass $status = null) + public function addResponse($response, ?stdClass $status = null) { if (!$this->deserialize && $response) { $this->deserialize = [get_class($response), 'decode']; @@ -263,7 +263,7 @@ public function isExhausted() * @param callable $deserialize * @return static An instance of the current class type. */ - public static function create($responseObject, stdClass $status = null, callable $deserialize = null) + public static function create($responseObject, ?stdClass $status = null, ?callable $deserialize = null) { $stub = new static($deserialize); // @phpstan-ignore-line $stub->addResponse($responseObject, $status); @@ -277,7 +277,7 @@ public static function create($responseObject, stdClass $status = null, callable * @param stdClass $finalStatus * @return static An instance of the current class type. */ - public static function createWithResponseSequence(array $sequence, callable $deserialize = null, stdClass $finalStatus = null) + public static function createWithResponseSequence(array $sequence, ?callable $deserialize = null, ?stdClass $finalStatus = null) { $stub = new static($deserialize); // @phpstan-ignore-line foreach ($sequence as $elem) { diff --git a/src/Testing/MockUnaryCall.php b/src/Testing/MockUnaryCall.php index 99afce996..4100e67ec 100644 --- a/src/Testing/MockUnaryCall.php +++ b/src/Testing/MockUnaryCall.php @@ -59,7 +59,7 @@ class MockUnaryCall extends \Grpc\UnaryCall * @param callable|array|null $deserialize An optional deserialize method for the response object. * @param stdClass|null $status An optional status object. If set to null, a status of OK is used. */ - public function __construct($response = null, $deserialize = null, stdClass $status = null) + public function __construct($response = null, $deserialize = null, ?stdClass $status = null) { $this->response = $response; $this->deserialize = $deserialize; diff --git a/src/Transport/GrpcTransport.php b/src/Transport/GrpcTransport.php index 3bc3328e8..74f6081f7 100644 --- a/src/Transport/GrpcTransport.php +++ b/src/Transport/GrpcTransport.php @@ -77,7 +77,7 @@ class GrpcTransport extends BaseStub implements TransportInterface * extends {@see Grpc\Interceptor}. * @throws Exception */ - public function __construct(string $hostname, array $opts, Channel $channel = null, array $interceptors = []) + public function __construct(string $hostname, array $opts, ?Channel $channel = null, array $interceptors = []) { if ($interceptors) { $channel = Interceptor::intercept(