Skip to content

Commit 8be2186

Browse files
authored
chore: add phpstan and fix resulting issues (#2252)
1 parent b567d09 commit 8be2186

File tree

14 files changed

+108
-67
lines changed

14 files changed

+108
-67
lines changed

.github/workflows/tests.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,18 @@ jobs:
5454
command: composer install
5555
- name: Run Script
5656
run: vendor/bin/phpcs src tests examples --standard=phpcs.xml.dist -nps
57+
staticanalysis:
58+
runs-on: ubuntu-latest
59+
name: PHPStan Static Analysis
60+
steps:
61+
- uses: actions/checkout@v2
62+
- name: Install PHP
63+
uses: shivammathur/setup-php@v2
64+
with:
65+
php-version: '8.0'
66+
- name: Run Script
67+
run: |
68+
composer install
69+
composer global require phpstan/phpstan
70+
~/.composer/vendor/bin/phpstan analyse
5771

phpstan.neon.dist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
treatPhpDocTypesAsCertain: false
3+
level: 5
4+
paths:
5+
- src
6+
excludePaths:
7+
- src/AuthHandler/Guzzle5AuthHandler.php
8+

src/AccessToken/Verify.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ class Verify
5555
*/
5656
private $cache;
5757

58+
/**
59+
* @var \Firebase\JWT\JWT
60+
*/
61+
public $jwt;
62+
5863
/**
5964
* Instantiates the class, but does not initiate the login flow, leaving it
6065
* to the discretion of the caller.
@@ -85,7 +90,7 @@ public function __construct(
8590
*
8691
* @param string $idToken the ID token in JWT format
8792
* @param string $audience Optional. The audience to verify against JWt "aud"
88-
* @return array the token payload, if successful
93+
* @return array|false the token payload, if successful
8994
*/
9095
public function verifyIdToken($idToken, $audience = null)
9196
{
@@ -124,7 +129,7 @@ public function verifyIdToken($idToken, $audience = null)
124129
}
125130

126131
return (array) $payload;
127-
} catch (ExpiredException $e) {
132+
} catch (ExpiredException $e) { // @phpstan-ignore-line
128133
return false;
129134
} catch (ExpiredExceptionV3 $e) {
130135
return false;
@@ -146,7 +151,7 @@ private function getCache()
146151
/**
147152
* Retrieve and cache a certificates file.
148153
*
149-
* @param $url string location
154+
* @param string $url location
150155
* @throws \Google\Exception
151156
* @return array certificates
152157
*/
@@ -164,6 +169,7 @@ private function retrieveCertsFromLocation($url)
164169
return json_decode($file, true);
165170
}
166171

172+
// @phpstan-ignore-next-line
167173
$response = $this->http->get($url);
168174

169175
if ($response->getStatusCode() == 200) {
@@ -224,6 +230,7 @@ private function getJwtService()
224230
$jwtClass::$leeway = 1;
225231
}
226232

233+
// @phpstan-ignore-next-line
227234
return new $jwtClass;
228235
}
229236

src/AuthHandler/AuthHandlerFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static function build($cache = null, array $cacheConfig = [])
3535
if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
3636
$guzzleVersion = ClientInterface::MAJOR_VERSION;
3737
} elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) {
38+
// @phpstan-ignore-next-line
3839
$guzzleVersion = (int) substr(ClientInterface::VERSION, 0, 1);
3940
}
4041

src/Client.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use DomainException;
4444
use InvalidArgumentException;
4545
use LogicException;
46+
use UnexpectedValueException;
4647

4748
/**
4849
* The Google API Client
@@ -58,7 +59,7 @@ class Client
5859
const API_BASE_PATH = 'https://www.googleapis.com';
5960

6061
/**
61-
* @var OAuth2 $auth
62+
* @var ?OAuth2 $auth
6263
*/
6364
private $auth;
6465

@@ -68,7 +69,7 @@ class Client
6869
private $http;
6970

7071
/**
71-
* @var CacheItemPoolInterface $cache
72+
* @var ?CacheItemPoolInterface $cache
7273
*/
7374
private $cache;
7475

@@ -83,12 +84,12 @@ class Client
8384
private $config;
8485

8586
/**
86-
* @var LoggerInterface $logger
87+
* @var ?LoggerInterface $logger
8788
*/
8889
private $logger;
8990

9091
/**
91-
* @var CredentialsLoader $credentials
92+
* @var ?CredentialsLoader $credentials
9293
*/
9394
private $credentials;
9495

@@ -225,7 +226,7 @@ public function getLibraryVersion()
225226
* For backwards compatibility
226227
* alias for fetchAccessTokenWithAuthCode
227228
*
228-
* @param $code string code from accounts.google.com
229+
* @param string $code string code from accounts.google.com
229230
* @return array access token
230231
* @deprecated
231232
*/
@@ -238,7 +239,7 @@ public function authenticate($code)
238239
* Attempt to exchange a code for an valid authentication token.
239240
* Helper wrapped around the OAuth 2.0 implementation.
240241
*
241-
* @param $code string code from accounts.google.com
242+
* @param string $code code from accounts.google.com
242243
* @return array access token
243244
*/
244245
public function fetchAccessTokenWithAuthCode($code)
@@ -714,7 +715,7 @@ public function setDeveloperKey($developerKey)
714715
* Set the hd (hosted domain) parameter streamlines the login process for
715716
* Google Apps hosted accounts. By including the domain of the user, you
716717
* restrict sign-in to accounts at that domain.
717-
* @param $hd string - the domain to use.
718+
* @param string $hd the domain to use.
718719
*/
719720
public function setHostedDomain($hd)
720721
{
@@ -725,7 +726,7 @@ public function setHostedDomain($hd)
725726
* Set the prompt hint. Valid values are none, consent and select_account.
726727
* If no value is specified and the user has not previously authorized
727728
* access, then the user is shown a consent screen.
728-
* @param $prompt string
729+
* @param string $prompt
729730
* {@code "none"} Do not display any authentication or consent screens. Must not be specified with other values.
730731
* {@code "consent"} Prompt the user for consent.
731732
* {@code "select_account"} Prompt the user to select an account.
@@ -739,7 +740,7 @@ public function setPrompt($prompt)
739740
* openid.realm is a parameter from the OpenID 2.0 protocol, not from OAuth
740741
* 2.0. It is used in OpenID 2.0 requests to signify the URL-space for which
741742
* an authentication request is valid.
742-
* @param $realm string - the URL-space to use.
743+
* @param string $realm the URL-space to use.
743744
*/
744745
public function setOpenidRealm($realm)
745746
{
@@ -750,7 +751,7 @@ public function setOpenidRealm($realm)
750751
* If this is provided with the value true, and the authorization request is
751752
* granted, the authorization will include any previous authorizations
752753
* granted to this user/application combination for other scopes.
753-
* @param $include boolean - the URL-space to use.
754+
* @param bool $include the URL-space to use.
754755
*/
755756
public function setIncludeGrantedScopes($include)
756757
{
@@ -834,7 +835,7 @@ public function setScopes($scope_or_scopes)
834835
* Will append any scopes not previously requested to the scope parameter.
835836
* A single string will be treated as a scope to request. An array of strings
836837
* will each be appended.
837-
* @param $scope_or_scopes string|array e.g. "profile"
838+
* @param string|string[] $scope_or_scopes e.g. "profile"
838839
*/
839840
public function addScope($scope_or_scopes)
840841
{
@@ -873,10 +874,11 @@ public function prepareScopes()
873874
/**
874875
* Helper method to execute deferred HTTP requests.
875876
*
876-
* @param $request RequestInterface|\Google\Http\Batch
877-
* @param string $expectedClass
877+
* @template T
878+
* @param RequestInterface $request
879+
* @param class-string<T> $expectedClass
878880
* @throws \Google\Exception
879-
* @return mixed|$expectedClass|ResponseInterface
881+
* @return mixed|T|ResponseInterface
880882
*/
881883
public function execute(RequestInterface $request, $expectedClass = null)
882884
{
@@ -902,7 +904,7 @@ public function execute(RequestInterface $request, $expectedClass = null)
902904
if ($this->config['api_format_v2']) {
903905
$request = $request->withHeader(
904906
'X-GOOG-API-FORMAT-VERSION',
905-
2
907+
'2'
906908
);
907909
}
908910

@@ -1182,6 +1184,7 @@ protected function createDefaultHttpClient()
11821184
if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
11831185
$guzzleVersion = ClientInterface::MAJOR_VERSION;
11841186
} elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) {
1187+
// @phpstan-ignore-next-line
11851188
$guzzleVersion = (int)substr(ClientInterface::VERSION, 0, 1);
11861189
}
11871190

@@ -1191,9 +1194,11 @@ protected function createDefaultHttpClient()
11911194
'defaults' => ['exceptions' => false],
11921195
];
11931196
if ($this->isAppEngine()) {
1194-
// set StreamHandler on AppEngine by default
1195-
$options['handler'] = new StreamHandler();
1196-
$options['defaults']['verify'] = '/etc/ca-certificates.crt';
1197+
if (class_exists(StreamHandler::class)) {
1198+
// set StreamHandler on AppEngine by default
1199+
$options['handler'] = new StreamHandler();
1200+
$options['defaults']['verify'] = '/etc/ca-certificates.crt';
1201+
}
11971202
}
11981203
} elseif (6 === $guzzleVersion || 7 === $guzzleVersion) {
11991204
// guzzle 6 or 7

src/Collection.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function key()
4545
}
4646
}
4747

48-
/** @return void */
48+
/** @return mixed */
4949
#[\ReturnTypeWillChange]
5050
public function next()
5151
{
@@ -71,6 +71,7 @@ public function count()
7171
}
7272

7373
/** @return bool */
74+
#[\ReturnTypeWillChange]
7475
public function offsetExists($offset)
7576
{
7677
if (!is_numeric($offset)) {
@@ -90,6 +91,7 @@ public function offsetGet($offset)
9091
}
9192

9293
/** @return void */
94+
#[\ReturnTypeWillChange]
9395
public function offsetSet($offset, $value)
9496
{
9597
if (!is_numeric($offset)) {
@@ -99,6 +101,7 @@ public function offsetSet($offset, $value)
99101
}
100102

101103
/** @return void */
104+
#[\ReturnTypeWillChange]
102105
public function offsetUnset($offset)
103106
{
104107
if (!is_numeric($offset)) {

src/Http/Batch.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function execute()
9393
9494
EOF;
9595

96-
/** @var RequestInterface $req */
96+
/** @var RequestInterface $request */
9797
foreach ($this->requests as $key => $request) {
9898
$firstLine = sprintf(
9999
'%s %s HTTP/%s',
@@ -126,7 +126,7 @@ public function execute()
126126
$url = $this->rootUrl . '/' . $this->batchPath;
127127
$headers = array(
128128
'Content-Type' => sprintf('multipart/mixed; boundary=%s', $this->boundary),
129-
'Content-Length' => strlen($body),
129+
'Content-Length' => (string) strlen($body),
130130
);
131131

132132
$request = new Request(
@@ -170,9 +170,9 @@ public function parseResponse(ResponseInterface $response, $classes = array())
170170
$status = explode(" ", $status);
171171
$status = $status[1];
172172

173-
list($partHeaders, $partBody) = $this->parseHttpResponse($part, false);
173+
list($partHeaders, $partBody) = $this->parseHttpResponse($part, 0);
174174
$response = new Response(
175-
$status,
175+
(int) $status,
176176
$partHeaders,
177177
Psr7\Utils::streamFor($partBody)
178178
);
@@ -219,8 +219,8 @@ private function parseRawHeaders($rawHeaders)
219219
/**
220220
* Used by the IO lib and also the batch processing.
221221
*
222-
* @param $respData
223-
* @param $headerSize
222+
* @param string $respData
223+
* @param int $headerSize
224224
* @return array
225225
*/
226226
private function parseHttpResponse($respData, $headerSize)

0 commit comments

Comments
 (0)