Skip to content

Commit 4128e78

Browse files
committed
PHPStan level max
1 parent c7f0c0b commit 4128e78

File tree

6 files changed

+37
-45
lines changed

6 files changed

+37
-45
lines changed

bin/prepare-commit

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
set -e
33
cd $(dirname $0)/..
44

5-
php-cs-fixer fix
5+
vendor/bin/php-cs-fixer fix
66
vendor/bin/phpstan
77
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text
88

99
[ ! -f "./infection.phar" ] && bin/install-infection
10-
./infection.phar --threads=4
10+
./infection.phar --threads=max
1111

1212
echo "All good, ready for commit!"

composer.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@
2222
"league/oauth2-client": "^2.4.1"
2323
},
2424
"require-dev": {
25-
"guzzlehttp/psr7": "^2.4.1",
25+
"friendsofphp/php-cs-fixer": "^3.37.1",
26+
"guzzlehttp/psr7": "^2.6.1",
2627
"http-interop/http-factory-guzzle": "^1.2",
27-
"mockery/mockery": "^1.5.1",
28-
"m4tthumphrey/php-gitlab-api": "^11.8",
28+
"m4tthumphrey/php-gitlab-api": "^11.12",
29+
"mockery/mockery": "^1.6.6",
2930
"php-http/guzzle7-adapter": "^1.0.0",
30-
"phpunit/phpunit": "^10.4.2",
31-
"phpstan/phpstan": "^1.8.10",
32-
"phpstan/phpstan-mockery": "^1.1",
33-
"phpstan/extension-installer": "^1.2",
34-
"phpstan/phpstan-phpunit": "^1.1.1"
31+
"phpstan/extension-installer": "^1.3.1",
32+
"phpstan/phpstan": "^1.10.41",
33+
"phpstan/phpstan-mockery": "^1.1.1",
34+
"phpstan/phpstan-phpunit": "^1.3.15",
35+
"phpunit/phpunit": "^10.4.2"
3536
},
3637
"suggest": {
3738
"m4tthumphrey/php-gitlab-api": "For further API usage using the acquired OAuth2 token"
@@ -52,6 +53,7 @@
5253
}
5354
},
5455
"config": {
56+
"sort-packages": true,
5557
"allow-plugins": {
5658
"phpstan/extension-installer": true,
5759
"php-http/discovery": true

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 8
2+
level: max
33
paths:
44
- src
55
- test

src/Provider/Exception/GitlabIdentityProviderException.php

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,14 @@
2020
*/
2121
final class GitlabIdentityProviderException extends IdentityProviderException
2222
{
23-
/**
24-
* Creates client exception from response.
25-
*
26-
* @param array<string, mixed>|string $data Parsed response data
27-
*/
28-
public static function clientException(ResponseInterface $response, array|string $data): IdentityProviderException
29-
{
30-
return self::fromResponse(
31-
$response,
32-
$data['message'] ?? $response->getReasonPhrase()
33-
);
34-
}
35-
36-
/**
37-
* Creates oauth exception from response.
38-
*
39-
* @param ResponseInterface $response Response received from upstream
40-
* @param array<string, mixed> $data Parsed response data
41-
*/
42-
public static function oauthException(ResponseInterface $response, array $data): IdentityProviderException
43-
{
44-
return self::fromResponse(
45-
$response,
46-
$data['error'] ?? $response->getReasonPhrase()
47-
);
48-
}
49-
5023
/**
5124
* Creates identity exception from response.
5225
*
5326
* @param ResponseInterface $response Response received from upstream
54-
* @param string|null $message Parsed message
27+
* @param ?string $message Parsed message
5528
*/
56-
private static function fromResponse(ResponseInterface $response, string $message = null): IdentityProviderException
29+
public static function fromResponse(ResponseInterface $response, string $message = null): IdentityProviderException
5730
{
58-
return new self($message ?? self::class, $response->getStatusCode(), $response->getBody()->getContents());
31+
return new self($message ?? $response->getReasonPhrase() ?: self::class, $response->getStatusCode(), $response->getBody()->getContents());
5932
}
6033
}

src/Provider/Gitlab.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,17 @@ protected function getScopeSeparator(): string
9090
* Check a provider response for errors.
9191
*
9292
* @param ResponseInterface $response Parsed response data
93-
* @param array<string, mixed> $data
93+
* @param array{error?: string, message?: string}|mixed $data
9494
* @throws IdentityProviderException
9595
*/
9696
protected function checkResponse(ResponseInterface $response, mixed $data): void
9797
{
98-
if ($response->getStatusCode() >= 400) {
99-
throw GitlabIdentityProviderException::clientException($response, $data);
98+
if (!is_array($data)) {
99+
throw GitlabIdentityProviderException::fromResponse($response, 'Corrupted response');
100+
} elseif ($response->getStatusCode() >= 400) {
101+
throw GitlabIdentityProviderException::fromResponse($response, $data['message'] ?? $response->getReasonPhrase());
100102
} elseif (isset($data['error'])) {
101-
throw GitlabIdentityProviderException::oauthException($response, $data);
103+
throw GitlabIdentityProviderException::fromResponse($response, $data['error']);
102104
}
103105
}
104106

test/src/Provider/GitlabTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,19 @@ public function testExceptionThrownWhenOAuthErrorReceived(): void
272272
$this->expectExceptionMessage('bad_verification_code');
273273
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
274274
}
275+
276+
public function testExceptionThrownWhenUnknownErrorReceived(): void
277+
{
278+
$response = new Response(200, ['content-type' => 'json'], '684');
279+
280+
$client = m::mock('GuzzleHttp\ClientInterface');
281+
$client->shouldReceive('send')
282+
->times(1)
283+
->andReturn($response);
284+
$this->provider->setHttpClient($client);
285+
286+
$this->expectException(IdentityProviderException::class);
287+
$this->expectExceptionMessage('Corrupted response');
288+
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
289+
}
275290
}

0 commit comments

Comments
 (0)