Skip to content

Commit feb6771

Browse files
authored
Merge pull request #15 from keboola/adamvyborny-upgrade-php
Upgrade PHP
2 parents 94fe967 + 19887d1 commit feb6771

7 files changed

Lines changed: 51 additions & 25 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM php:7-cli
1+
FROM php:8.1-cli
22

33
ARG COMPOSER_FLAGS="--prefer-dist --no-interaction"
44
ARG DEBIAN_FRONTEND=noninteractive

composer.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^7.1",
14-
"guzzlehttp/guzzle": "^6.0",
13+
"php": "^8.1",
14+
"guzzlehttp/guzzle": "^7.0",
1515
"monolog/monolog": "^2.1"
1616
},
1717
"require-dev": {
18-
"keboola/coding-standard": "^9.0",
19-
"phpstan/phpstan": "^0.12.59",
18+
"keboola/coding-standard": "^15.0",
19+
"phpstan/phpstan": "^1.4",
2020
"php-parallel-lint/php-parallel-lint": "^1.2",
2121
"phpunit/phpunit": "^9.5"
2222
},
@@ -31,6 +31,7 @@
3131
"tests": "phpunit",
3232
"phpstan": "phpstan analyse ./src ./tests --level=max --no-progress -c phpstan.neon",
3333
"phpcs": "phpcs -n --ignore=vendor --extensions=php .",
34+
"phpcbf": "phpcbf -n --ignore=vendor --extensions=php .",
3435
"phplint": "parallel-lint -j 10 --exclude vendor .",
3536
"check": [
3637
"@phplint",
@@ -42,5 +43,12 @@
4243
"@check",
4344
"@tests"
4445
]
46+
},
47+
"config": {
48+
"sort-packages": true,
49+
"optimize-autoloader": true,
50+
"allow-plugins": {
51+
"dealerdirect/phpcodesniffer-composer-installer": true
52+
}
4553
}
4654
}

phpcs.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
<?xml version="1.0"?>
22
<ruleset name="Project">
33
<rule ref="vendor/keboola/coding-standard/src/ruleset.xml"/>
4+
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
5+
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification"/>
6+
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint"/>
7+
</rule>
8+
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
9+
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification"/>
10+
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint"/>
11+
</rule>
12+
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint">
13+
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint"/>
14+
</rule>
415
</ruleset>

src/Exception/RestApiException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Keboola\Google\ClientBundle\Exception;
66

7-
class RestApiException extends \Exception
7+
use Exception;
8+
9+
class RestApiException extends Exception
810
{
911
}

src/Google/RestApi.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __construct(
5353
string $clientSecret,
5454
string $accessToken = '',
5555
string $refreshToken = '',
56-
?Logger $logger = null
56+
?Logger $logger = null,
5757
) {
5858
$this->clientId = $clientId;
5959
$this->clientSecret = $clientSecret;
@@ -68,7 +68,7 @@ public function __construct(
6868
public static function createRetryMiddleware(
6969
callable $decider,
7070
callable $callback,
71-
?callable $delay = null
71+
?callable $delay = null,
7272
): callable {
7373
return function (callable $handler) use ($decider, $callback, $delay) {
7474
return new RetryCallbackMiddleware($decider, $callback, $handler, $delay);
@@ -80,7 +80,7 @@ public function createRetryDecider(int $maxRetries = 5): callable
8080
return function (
8181
$retries,
8282
RequestInterface $request,
83-
?ResponseInterface $response = null
83+
?ResponseInterface $response = null,
8484
) use ($maxRetries) {
8585
$decision = $this->decideRetry($retries, $maxRetries, $response);
8686
if ($decision) {
@@ -97,7 +97,7 @@ public function createRetryCallback(): callable
9797

9898
return function (
9999
RequestInterface $request,
100-
?ResponseInterface $response = null
100+
?ResponseInterface $response = null,
101101
) use ($api) {
102102
if ($response && $response->getStatusCode() === 401) {
103103
$tokens = $api->refreshToken();
@@ -114,7 +114,7 @@ protected function getClient(string $baseUri = self::API_URI): Client
114114
$handlerStack->push(self::createRetryMiddleware(
115115
$this->createRetryDecider($this->maxBackoffs),
116116
$this->createRetryCallback(),
117-
$this->delayFn
117+
$this->delayFn,
118118
));
119119

120120
return new Client([
@@ -172,7 +172,7 @@ public function getAuthorizationUrl(
172172
string $scope,
173173
string $approvalPrompt = 'force',
174174
string $accessType = 'offline',
175-
string $state = ''
175+
string $state = '',
176176
): string {
177177
$params = [
178178
'response_type=code',
@@ -209,6 +209,7 @@ public function authorize(string $code, string $redirectUri): array
209209
],
210210
]);
211211

212+
/** @var array<string, string> $responseBody */
212213
$responseBody = json_decode((string) $response->getBody(), true);
213214

214215
$this->accessToken = $responseBody['access_token'];
@@ -233,6 +234,7 @@ public function refreshToken(): array
233234
],
234235
]);
235236

237+
/** @var array<string, string> $responseBody */
236238
$responseBody = json_decode($response->getBody()->getContents(), true);
237239

238240
$this->accessToken = $responseBody['access_token'];
@@ -251,7 +253,7 @@ public function request(
251253
string $url,
252254
string $method = 'GET',
253255
array $addHeaders = [],
254-
array $options = []
256+
array $options = [],
255257
): Response {
256258
$method = strtolower($method);
257259
if (!in_array($method, ['get', 'head', 'post', 'put', 'patch', 'delete', 'options'])) {
@@ -279,7 +281,7 @@ public function request(
279281
protected function logRetryRequest(
280282
int $retries,
281283
RequestInterface $request,
282-
?ResponseInterface $response = null
284+
?ResponseInterface $response = null,
283285
): void {
284286
if ($this->logger !== null) {
285287
$headersForLog = $request->getHeaders();
@@ -305,7 +307,7 @@ protected function logRetryRequest(
305307

306308
$this->logger->info(
307309
sprintf('Retrying request (%sx) - reason: %s', $retries, $response->getReasonPhrase()),
308-
$context
310+
$context,
309311
);
310312
} else {
311313
$this->logger->info(sprintf('Retrying request (%sx)', $retries), $context);

src/Guzzle/RetryCallbackMiddleware.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct(
4545
callable $decider,
4646
callable $callback,
4747
callable $nextHandler,
48-
?callable $delay = null
48+
?callable $delay = null,
4949
) {
5050
$this->decider = $decider;
5151
$this->callback = $callback;
@@ -78,7 +78,7 @@ public function __invoke(RequestInterface $request, array $options): PromiseInte
7878
return $fn($request, $options)
7979
->then(
8080
$this->onFulfilled($request, $options),
81-
$this->onRejected($request, $options)
81+
$this->onRejected($request, $options),
8282
);
8383
}
8484

@@ -90,7 +90,7 @@ private function onFulfilled(RequestInterface $request, array $options): callabl
9090
$options['retries'],
9191
$request,
9292
$response,
93-
null
93+
null,
9494
)) {
9595
return $response;
9696
}
@@ -109,7 +109,7 @@ private function onRejected(RequestInterface $req, array $options): callable
109109
$options['retries'],
110110
$req,
111111
null,
112-
$reason
112+
$reason,
113113
)) {
114114
return new RejectedPromise($reason);
115115
}

tests/RestApiTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Keboola\Google\ClientBundle\Tests;
66

7+
use Exception;
78
use GuzzleHttp\Exception\ClientException;
89
use Keboola\Google\ClientBundle\Google\RestApi;
910
use Monolog\Handler\TestHandler;
@@ -42,7 +43,7 @@ protected function initApi(): RestApi
4243
$this->clientSecret,
4344
'',
4445
$this->refreshToken,
45-
$this->logger
46+
$this->logger,
4647
);
4748
}
4849

@@ -76,6 +77,7 @@ public function testRequest(): void
7677
$restApi = $this->initApi();
7778
$response = $restApi->request('/oauth2/v3/userinfo');
7879
$body = json_decode($response->getBody()->getContents(), true);
80+
self::assertIsArray($body);
7981

8082
$this->assertArrayHasKey('sub', $body);
8183
$this->assertArrayHasKey('email', $body);
@@ -99,6 +101,7 @@ public function testDelayFn(): void
99101
$time = $timer->stop()->asSeconds();
100102

101103
$body = json_decode($response->getBody()->getContents(), true);
104+
self::assertIsArray($body);
102105

103106
$this->assertArrayHasKey('sub', $body);
104107
$this->assertArrayHasKey('email', $body);
@@ -124,7 +127,7 @@ public function testRetries(): void
124127
'request' => [
125128
'uri' => 'https://www.googleapis.com/auth/invalid-scope',
126129
'headers' => [
127-
'User-Agent' => ['GuzzleHttp/6.5.5 curl/7.74.0 PHP/7.4.33'],
130+
'User-Agent' => ['GuzzleHttp/7'],
128131
'Host' => ['www.googleapis.com'],
129132
'Accept' => ['application/json'],
130133
'Authorization' => '*****',
@@ -145,7 +148,7 @@ public function testRetries(): void
145148
" it to craft your OAuth2 request.\n",
146149
],
147150
],
148-
$value['context']
151+
$value['context'],
149152
);
150153
}
151154
}
@@ -160,7 +163,7 @@ public function testDoNotRetryOnWrongCredentials(): void
160163
$this->getEnv('CLIENT_SECRET') . 'invalid',
161164
'',
162165
$this->getEnv('REFRESH_TOKEN'),
163-
$logger
166+
$logger,
164167
);
165168

166169
try {
@@ -172,7 +175,7 @@ public function testDoNotRetryOnWrongCredentials(): void
172175
$this->assertCount(1, $testHandler->getRecords());
173176
$this->assertStringContainsString(
174177
'Retrying request (0x) - reason: Unauthorized',
175-
$testHandler->getRecords()[0]['message']
178+
$testHandler->getRecords()[0]['message'],
176179
);
177180
}
178181

@@ -181,7 +184,7 @@ protected function getEnv(string $name): string
181184
$value = getenv($name);
182185

183186
if ($value === false) {
184-
throw new \Exception(sprintf('Environment variable "%s" cannot be empty', $name));
187+
throw new Exception(sprintf('Environment variable "%s" cannot be empty', $name));
185188
}
186189

187190
return $value;

0 commit comments

Comments
 (0)