Skip to content

Commit 0aace7d

Browse files
Merge pull request #9 from cleverage/8_phpstan
Improve phpstan quality level
2 parents 95dea37 + 60a0b82 commit 0aace7d

File tree

4 files changed

+89
-19
lines changed

4 files changed

+89
-19
lines changed

phpstan.neon

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
parameters:
2-
level: 6
2+
level: 10
33
paths:
44
- src
55
- tests
66
ignoreErrors:
7-
- '#type has no value type specified in iterable type#'
8-
- '#has parameter .* with no value type specified in iterable type#'
9-
- '#has no value type specified in iterable type array#'
10-
- '#configureOptions\(\) has no return type specified.#'
11-
- '#configure\(\) has no return type specified#'
12-
- '#process\(\) has no return type specified#'
13-
- '#should return Iterator but returns Traversable#'
14-
- '#Negated boolean expression is always false#'
15-
checkGenericClassInNonGenericObjectType: false
16-
reportUnmatchedIgnoredErrors: false
17-
inferPrivatePropertyTypeFromConstructor: true
18-
treatPhpDocTypesAsCertain: false
7+
- identifier: parameter.defaultValue

src/Client/Client.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use Symfony\Contracts\HttpClient\HttpClientInterface;
2020
use Symfony\Contracts\HttpClient\ResponseInterface;
2121

22+
/**
23+
* @phpstan-import-type RequestOptions from \CleverAge\RestProcessBundle\Task\RequestTask
24+
*/
2225
class Client implements ClientInterface
2326
{
2427
public function __construct(
@@ -50,6 +53,8 @@ public function setUri(string $uri): void
5053
}
5154

5255
/**
56+
* @param RequestOptions $options
57+
*
5358
* @throws RestRequestException
5459
*/
5560
public function call(array $options = []): ResponseInterface
@@ -70,7 +75,7 @@ public function call(array $options = []): ResponseInterface
7075
$this->getRequestUri($options),
7176
$this->getRequestOptions($options),
7277
);
73-
} catch (\Exception|\Throwable $e) {
78+
} catch (\Throwable $e) {
7479
$this->logger->error(
7580
'Rest request failed',
7681
[
@@ -110,19 +115,40 @@ protected function configureOptions(OptionsResolver $resolver): void
110115
$resolver->setAllowedTypes('data', ['array', 'string', 'null']);
111116
}
112117

118+
/**
119+
* @param RequestOptions $options
120+
*
121+
* @return RequestOptions
122+
*/
113123
protected function getOptions(array $options = []): array
114124
{
115125
$resolver = new OptionsResolver();
116126
$this->configureOptions($resolver);
117127

118-
return $resolver->resolve($options);
128+
/** @var RequestOptions $resolved */
129+
$resolved = $resolver->resolve($options);
130+
131+
return $resolved;
119132
}
120133

134+
/**
135+
* @param RequestOptions $options
136+
*/
121137
protected function getRequestUri(array $options = []): string
122138
{
123139
return $this->replaceParametersInUri($this->constructUri($options), $options);
124140
}
125141

142+
/**
143+
* @param RequestOptions $options
144+
*
145+
* @return array{
146+
* 'headers': array<mixed>,
147+
* 'json'?: array<mixed>|string|null,
148+
* 'query'?: array<mixed>|string|null,
149+
* 'body'?: array<mixed>|string|null
150+
* }
151+
*/
126152
protected function getRequestOptions(array $options = []): array
127153
{
128154
$requestOptions = [];
@@ -144,6 +170,9 @@ protected function getRequestOptions(array $options = []): array
144170
return $requestOptions;
145171
}
146172

173+
/**
174+
* @param RequestOptions $options
175+
*/
147176
protected function constructUri(array $options): string
148177
{
149178
$uri = ltrim((string) $options['url'], '/');
@@ -156,16 +185,21 @@ protected function getApiUrl(): string
156185
return $this->geUri();
157186
}
158187

188+
/**
189+
* @param RequestOptions $options
190+
*/
159191
protected function replaceParametersInUri(string $uri, array $options = []): string
160192
{
161-
if (\array_key_exists('url_parameters', $options) && $options['url_parameters']) {
193+
if ($options['url_parameters']) {
194+
/** @var array<string> $search */
162195
$search = array_keys($options['url_parameters']);
163196
array_walk(
164197
$search,
165198
static function (&$item, $key) {
166199
$item = '{'.$item.'}';
167200
}
168201
);
202+
/** @var array<string> $replace */
169203
$replace = array_values($options['url_parameters']);
170204
array_walk(
171205
$replace,

src/Client/ClientInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
use Symfony\Contracts\HttpClient\ResponseInterface;
1717

18+
/**
19+
* @phpstan-import-type RequestOptions from \CleverAge\RestProcessBundle\Task\RequestTask
20+
*/
1821
interface ClientInterface
1922
{
2023
/**
@@ -26,5 +29,8 @@ public function geUri(): string;
2629

2730
public function setUri(string $uri): void;
2831

32+
/**
33+
* @param RequestOptions $options
34+
*/
2935
public function call(array $options = []): ResponseInterface;
3036
}

src/Task/RequestTask.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,34 @@
2222
use Symfony\Component\OptionsResolver\Exception\AccessException;
2323
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
2424
use Symfony\Component\OptionsResolver\OptionsResolver;
25-
25+
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
26+
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
27+
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
28+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
29+
30+
/**
31+
* @phpstan-type Options array{
32+
* 'client': string,
33+
* 'url': string,
34+
* 'method': string,
35+
* 'headers': array<mixed>,
36+
* 'url_parameters': array<mixed>,
37+
* 'data': array<mixed>|string|null,
38+
* 'sends': string,
39+
* 'expects': string,
40+
* 'valid_response_code': array<int>,
41+
* 'log_response': bool,
42+
* }
43+
* @phpstan-type RequestOptions array{
44+
* 'url': string,
45+
* 'method': string,
46+
* 'headers': array<mixed>,
47+
* 'url_parameters': array<mixed>,
48+
* 'sends': string,
49+
* 'expects': string,
50+
* 'data': array<mixed>|string|null
51+
* }
52+
*/
2653
class RequestTask extends AbstractConfigurableTask
2754
{
2855
public function __construct(protected LoggerInterface $logger, protected ClientRegistry $registry)
@@ -31,9 +58,15 @@ public function __construct(protected LoggerInterface $logger, protected ClientR
3158

3259
/**
3360
* @throws MissingClientException
61+
* @throws ClientExceptionInterface
62+
* @throws RedirectionExceptionInterface
63+
* @throws ServerExceptionInterface
64+
* @throws TransportExceptionInterface
65+
* @throws \Throwable
3466
*/
3567
public function execute(ProcessState $state): void
3668
{
69+
/** @var Options $options */
3770
$options = $this->getOptions($state);
3871

3972
$requestOptions = $this->getRequestOptions($state);
@@ -68,7 +101,7 @@ public function execute(ProcessState $state): void
68101
}
69102

70103
$state->setOutput($response->getContent());
71-
} catch (\Exception|\Throwable $e) {
104+
} catch (\Throwable $e) {
72105
$this->logger->error(
73106
'REST request failed',
74107
[
@@ -116,8 +149,12 @@ protected function configureOptions(OptionsResolver $resolver): void
116149
$resolver->setAllowedTypes('log_response', ['bool']);
117150
}
118151

152+
/**
153+
* @return RequestOptions
154+
*/
119155
protected function getRequestOptions(ProcessState $state): array
120156
{
157+
/** @var Options $options */
121158
$options = $this->getOptions($state);
122159

123160
$requestOptions = [
@@ -130,8 +167,12 @@ protected function getRequestOptions(ProcessState $state): array
130167
'data' => $options['data'],
131168
];
132169

170+
/** @var array<mixed> $input */
133171
$input = $state->getInput() ?: [];
134172

135-
return array_merge($requestOptions, $input);
173+
/** @var RequestOptions $mergedOptions */
174+
$mergedOptions = array_merge($requestOptions, $input);
175+
176+
return $mergedOptions;
136177
}
137178
}

0 commit comments

Comments
 (0)