Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add typehints #403

Merged
merged 6 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 2 additions & 3 deletions .github/sync-repo-settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ branchProtectionRules:
- pattern: main
isAdminEnforced: true
requiredStatusCheckContexts:
- 'PHP 5.6 Unit Test'
- 'PHP 5.6 Unit Test protobuf-3.12.2,grpc'
- 'PHP 7.0 Unit Test protobuf-3.12.2,grpc'
- 'PHP 7.0 Unit Test'
- 'PHP 7.1 Unit Test'
- 'PHP 7.2 Unit Test'
- 'PHP 7.3 Unit Test'
- 'PHP 7.3 Unit Test protobuf-3.12.2,grpc'
- 'PHP 7.4 Unit Test'
- 'PHP 8.0 Unit Test'
- 'PHP 8.1 Unit Test'
- 'PHP 8.1 Unit Test protobuf-3.12.2,grpc'
- 'Composer Conflict Test'
- 'PHP Style Check'
- 'cla/google'
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ 5.6, "7.0", 7.1, 7.2, 7.3, 7.4, "8.0", 8.1 ]
php: [ "7.0", 7.1, 7.2, 7.3, 7.4, "8.0", 8.1 ]
extensions: [""]
tools: [""]
include:
- php: 5.6
- php: "7.0"
extensions: "protobuf-3.12.2,grpc"
tools: "pecl"
- php: 7.3
- php: 8.1
extensions: "protobuf-3.12.2,grpc"
tools: "pecl"
name: "PHP ${{ matrix.php }} Unit Test ${{ matrix.extensions }}"
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"homepage": "https://github.com/googleapis/gax-php",
"license": "BSD-3-Clause",
"require": {
"php": ">=5.6",
"php": ">=7.0",
"google/auth": "^1.18.0",
"google/grpc-gcp": "^0.2",
"grpc/grpc": "^1.13",
"google/protobuf": "^3.12.2",
"google/protobuf": "^3.21.4",
"guzzlehttp/promises": "^1.3",
"guzzlehttp/psr7": "^1.7.0||^2",
"google/common-protos": "^1.0||^2.0||^3.0",
Expand Down
4 changes: 2 additions & 2 deletions src/AgentHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class AgentHeader
* }
* @return array Agent header array
*/
public static function buildAgentHeader($headerInfo)
public static function buildAgentHeader(array $headerInfo)
{
$metricsHeaders = [];

Expand Down Expand Up @@ -138,7 +138,7 @@ public static function buildAgentHeader($headerInfo)
* @return string the gapic version
* @throws \ReflectionException
*/
public static function readGapicVersionFromFile($callingClass)
public static function readGapicVersionFromFile(string $callingClass)
{
$callingClassFile = (new \ReflectionClass($callingClass))->getFileName();
$versionFile = substr(
Expand Down
23 changes: 15 additions & 8 deletions src/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
use Google\Protobuf\Internal\RepeatedField;
use Google\Rpc\Status;
use GuzzleHttp\Exception\RequestException;
use Google\ApiCore\Testing\MockStatus;
use stdClass;

/**
* Represents an exception thrown during an RPC.
Expand All @@ -58,9 +60,9 @@ class ApiException extends Exception
* }
*/
public function __construct(
$message,
$code,
$status,
string $message,
int $code,
string $status = null,
array $optionalArgs = []
) {
$optionalArgs += [
Expand Down Expand Up @@ -137,10 +139,10 @@ public function getErrorInfoMetadata()
}

/**
* @param \stdClass $status
* @param stdClass $status
* @return ApiException
*/
public static function createFromStdClass($status)
public static function createFromStdClass(stdClass $status)
{
$metadata = property_exists($status, 'metadata') ? $status->metadata : null;
return self::create(
Expand Down Expand Up @@ -237,8 +239,13 @@ private static function containsErrorInfo(array $decodedMetadata)
* @param Exception|null $previous
* @return ApiException
*/
private static function create($basicMessage, $rpcCode, $metadata, array $decodedMetadata, $previous = null)
{
private static function create(
string $basicMessage,
int $rpcCode,
$metadata = null,
array $decodedMetadata = null,
Exception $previous = null
) {
$containsErrorInfo = self::containsErrorInfo($decodedMetadata);
$rpcStatus = ApiStatus::statusFromRpcCode($rpcCode);
$messageData = [
Expand Down Expand Up @@ -286,7 +293,7 @@ public static function createFromRpcStatus(Status $status)
* @return ApiException
* @throws ValidationException
*/
public static function createFromRequestException(RequestException $ex, $isStream = false)
public static function createFromRequestException(RequestException $ex, bool $isStream = false)
{
$res = $ex->getResponse();
$body = (string) $res->getBody();
Expand Down
8 changes: 4 additions & 4 deletions src/ApiStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class ApiStatus
* @param string $status
* @return bool
*/
public static function isValidStatus($status)
public static function isValidStatus(string $status)
{
return array_key_exists($status, self::$apiStatusToCodeMap);
}
Expand All @@ -121,7 +121,7 @@ public static function isValidStatus($status)
* @param int $code
* @return string
*/
public static function statusFromRpcCode($code)
public static function statusFromRpcCode(int $code)
{
if (array_key_exists($code, self::$codeToApiStatusMap)) {
return self::$codeToApiStatusMap[$code];
Expand All @@ -133,7 +133,7 @@ public static function statusFromRpcCode($code)
* @param string $status
* @return int
*/
public static function rpcCodeFromStatus($status)
public static function rpcCodeFromStatus(string $status)
{
if (array_key_exists($status, self::$apiStatusToCodeMap)) {
return self::$apiStatusToCodeMap[$status];
Expand All @@ -148,7 +148,7 @@ public static function rpcCodeFromStatus($status)
* @param int $httpStatusCode
* @return int
*/
public static function rpcCodeFromHttpStatusCode($httpStatusCode)
public static function rpcCodeFromHttpStatusCode(int $httpStatusCode)
{
if (array_key_exists($httpStatusCode, self::$httpStatusCodeToRpcCodeMap)) {
return self::$httpStatusCodeToRpcCodeMap[$httpStatusCode];
Expand Down
6 changes: 3 additions & 3 deletions src/ArrayTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ trait ArrayTrait
* @return mixed|null
* @throws \InvalidArgumentException
*/
private function pluck($key, array &$arr, $isRequired = true)
private function pluck(string $key, array &$arr, bool $isRequired = true)
{
if (!array_key_exists($key, $arr)) {
if ($isRequired) {
Expand All @@ -72,7 +72,7 @@ private function pluck($key, array &$arr, $isRequired = true)
* @param array $arr
* @return array
*/
private function pluckArray(array $keys, &$arr)
private function pluckArray(array $keys, array &$arr)
{
$values = [];

Expand Down Expand Up @@ -120,7 +120,7 @@ private function arrayFilterRemoveNull(array $arr)
* @param array $arr
* @return array
*/
private function subsetArray(array $keys, $arr)
private function subsetArray(array $keys, array $arr)
{
return array_intersect_key(
$arr,
Expand Down
7 changes: 4 additions & 3 deletions src/BidiStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
namespace Google\ApiCore;

use Google\Rpc\Code;
use Grpc\BidiStreamingCall;

/**
* BidiStream is the response object from a gRPC bidirectional streaming API call.
Expand All @@ -47,10 +48,10 @@ class BidiStream
/**
* BidiStream constructor.
*
* @param \Grpc\BidiStreamingCall $bidiStreamingCall The gRPC bidirectional streaming call object
* @param BidiStreamingCall $bidiStreamingCall The gRPC bidirectional streaming call object
* @param array $streamingDescriptor
*/
public function __construct($bidiStreamingCall, array $streamingDescriptor = [])
public function __construct(BidiStreamingCall $bidiStreamingCall, array $streamingDescriptor = [])
{
$this->call = $bidiStreamingCall;
if (array_key_exists('resourcesGetMethod', $streamingDescriptor)) {
Expand Down Expand Up @@ -82,7 +83,7 @@ public function write($request)
*
* @throws ValidationException
*/
public function writeAll($requests = [])
public function writeAll(array $requests = [])
{
foreach ($requests as $request) {
$this->write($request);
Expand Down
8 changes: 4 additions & 4 deletions src/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ class Call
* @param string $method
* @param string $decodeType
* @param mixed|Message $message
* @param array $descriptor
* @param array|null $descriptor
* @param int $callType
*/
public function __construct(
$method,
$decodeType,
string $method,
string $decodeType = null,
$message = null,
$descriptor = [],
$callType = Call::UNARY_CALL
int $callType = Call::UNARY_CALL
) {
$this->method = $method;
$this->decodeType = $decodeType;
Expand Down
7 changes: 4 additions & 3 deletions src/ClientStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
namespace Google\ApiCore;

use Google\Rpc\Code;
use Grpc\ClientStreamingCall;

/**
* ClientStream is the response object from a gRPC client streaming API call.
Expand All @@ -43,11 +44,11 @@ class ClientStream
/**
* ClientStream constructor.
*
* @param \Grpc\ClientStreamingCall $clientStreamingCall The gRPC client streaming call object
* @param ClientStreamingCall $clientStreamingCall The gRPC client streaming call object
* @param array $streamingDescriptor
*/
public function __construct( // @phpstan-ignore-line
$clientStreamingCall,
ClientStreamingCall $clientStreamingCall,
array $streamingDescriptor = []
) {
$this->call = $clientStreamingCall;
Expand Down Expand Up @@ -86,7 +87,7 @@ public function readResponse()
* @param mixed[] $requests An iterator of request objects to write to the server
* @return mixed The response object from the server
*/
public function writeAllAndReadResponse($requests)
public function writeAllAndReadResponse(array $requests)
{
foreach ($requests as $request) {
$this->write($request);
Expand Down
10 changes: 8 additions & 2 deletions src/CredentialsWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function getBearerString()
* @param string $audience optional audience for self-signed JWTs.
* @return callable Callable function that returns an authorization header.
*/
public function getAuthorizationHeaderCallback($audience = null)
public function getAuthorizationHeaderCallback(string $audience = null)
{
$credentialsFetcher = $this->credentialsFetcher;
$authHttpHandler = $this->authHttpHandler;
Expand Down Expand Up @@ -269,7 +269,7 @@ private static function buildApplicationDefaultCredentials(
}
}

private static function getToken(FetchAuthTokenInterface $credentialsFetcher, $authHttpHandler)
private static function getToken(FetchAuthTokenInterface $credentialsFetcher, callable $authHttpHandler)
{
$token = $credentialsFetcher->getLastReceivedToken();
if (self::isExpired($token)) {
Expand All @@ -281,12 +281,18 @@ private static function getToken(FetchAuthTokenInterface $credentialsFetcher, $a
return $token['access_token'];
}

/**
* @param mixed $token
*/
private static function isValid($token)
{
return is_array($token)
&& array_key_exists('access_token', $token);
}

/**
* @param mixed $token
*/
private static function isExpired($token)
{
return !(self::isValid($token)
Expand Down
8 changes: 4 additions & 4 deletions src/FixedSizeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class FixedSizeCollection implements IteratorAggregate
/**
* FixedSizeCollection constructor.
* @param Page $initialPage
* @param integer $collectionSize
* @param int $collectionSize
*/
public function __construct($initialPage, $collectionSize)
public function __construct(Page $initialPage, int $collectionSize)
{
if ($collectionSize <= 0) {
throw new InvalidArgumentException(
Expand Down Expand Up @@ -165,10 +165,10 @@ private function getLastPage()

/**
* @param Page $initialPage
* @param integer $collectionSize
* @param int $collectionSize
* @return Page[]
*/
private static function createPageArray($initialPage, $collectionSize)
private static function createPageArray(Page $initialPage, int $collectionSize)
{
$pageList = [$initialPage];
$currentPage = $initialPage;
Expand Down
Loading