Skip to content

Commit b94430d

Browse files
Merge pull request #9 from cleverage/8_phpstan
Improve phpstan quality level
2 parents e219a39 + 41809a1 commit b94430d

File tree

5 files changed

+69
-38
lines changed

5 files changed

+69
-38
lines changed

phpstan.neon

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
parameters:
2-
level: 6
2+
level: 10
33
paths:
44
- src
55
- tests
6-
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

src/Client/Client.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,21 @@
2222
*/
2323
class Client implements ClientInterface
2424
{
25-
/** @var array */
26-
private $soapOptions;
25+
/** @var array<mixed>|null */
26+
private ?array $soapOptions = null;
2727

2828
/** @var \SoapHeader[]|null */
29-
private $soapHeaders;
29+
private ?array $soapHeaders = null;
3030

31-
/** @var \SoapClient */
32-
private $soapClient;
31+
private ?\SoapClient $soapClient = null;
3332

34-
/** @var string */
35-
private $lastRequest;
33+
private ?string $lastRequest = null;
3634

37-
/** @var string */
38-
private $lastRequestHeaders;
35+
private ?string $lastRequestHeaders = null;
3936

40-
/** @var string */
41-
private $lastResponse;
37+
private ?string $lastResponse = null;
4238

43-
/** @var string */
44-
private $lastResponseHeaders;
39+
private ?string $lastResponseHeaders = null;
4540

4641
/**
4742
* Client constructor.
@@ -50,6 +45,7 @@ public function __construct(
5045
private readonly LoggerInterface $logger,
5146
private readonly string $code,
5247
private ?string $wsdl,
48+
/** @var array<mixed> */
5349
private array $options = [],
5450
) {
5551
}
@@ -167,9 +163,6 @@ public function setLastResponseHeaders(?string $lastResponseHeaders): void
167163
$this->lastResponseHeaders = $lastResponseHeaders;
168164
}
169165

170-
/**
171-
* @return bool|mixed
172-
*/
173166
public function call(string $method, array $input = []): mixed
174167
{
175168
$this->initializeSoapClient();
@@ -187,6 +180,8 @@ public function call(string $method, array $input = []): mixed
187180
}
188181

189182
/**
183+
* @param array<mixed> $input
184+
*
190185
* @return bool|mixed
191186
*/
192187
protected function doSoapCall(string $method, array $input = []): mixed
@@ -196,7 +191,7 @@ protected function doSoapCall(string $method, array $input = []): mixed
196191
}
197192
try {
198193
$result = $this->getSoapClient()->__soapCall($method, $input, $this->getSoapOptions(), $this->getSoapHeaders());
199-
} /* @noinspection PhpRedundantCatchClauseInspection */ catch (\SoapFault $e) {
194+
} catch (\SoapFault $e) {
200195
$this->getLastRequestTrace();
201196
$this->getLogger()->alert(
202197
\sprintf("Soap call '%s' on '%s' failed : %s", $method, $this->getWsdl(), $e->getMessage()),
@@ -231,14 +226,23 @@ protected function initializeSoapClient(): void
231226

232227
protected function getLastRequestTrace(): void
233228
{
234-
if ($this->getSoapClient() instanceof \SoapClient) {
235-
$this->setLastRequest($this->getSoapClient()->__getLastRequest());
236-
$this->setLastRequestHeaders($this->getSoapClient()->__getLastRequestHeaders());
237-
$this->setLastResponse($this->getSoapClient()->__getLastResponse());
238-
$this->setLastResponseHeaders($this->getSoapClient()->__getLastResponseHeaders());
229+
$soapClient = $this->getSoapClient();
230+
if ($soapClient instanceof \SoapClient) {
231+
$this->setLastRequest($soapClient->__getLastRequest());
232+
$this->setLastRequestHeaders($soapClient->__getLastRequestHeaders());
233+
$this->setLastResponse($soapClient->__getLastResponse());
234+
$this->setLastResponseHeaders($soapClient->__getLastResponseHeaders());
239235
}
240236
}
241237

238+
/**
239+
* @return array{
240+
* 'LastRequest': ?string,
241+
* 'LastRequestHeaders': ?string,
242+
* 'LastResponse': ?string,
243+
* 'LastResponseHeaders': ?string
244+
* }
245+
*/
242246
protected function getLastRequestTraceArray(): array
243247
{
244248
return [

src/Client/ClientInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,35 @@ public function setWsdl(?string $wsdl): void;
3434
* Return the Soap client options.
3535
*
3636
* @see http://php.net/manual/en/soapclient.soapclient.php
37+
*
38+
* @return array<mixed>
3739
*/
3840
public function getOptions(): array;
3941

4042
/**
4143
* Set the Soap client options.
4244
*
4345
* @see http://php.net/manual/en/soapclient.soapclient.php
46+
*
47+
* @param array<mixed> $options
4448
*/
4549
public function setOptions(array $options): void;
4650

4751
/**
4852
* Return the Soap call options.
4953
*
5054
* @see https://www.php.net/manual/en/soapclient.soapcall.php
55+
*
56+
* @return array<mixed>|null
5157
*/
5258
public function getSoapOptions(): ?array;
5359

5460
/**
5561
* Set the Soap call options.
5662
*
5763
* @see https://www.php.net/manual/en/soapclient.soapcall.php
64+
*
65+
* @param array<mixed>|null $options
5866
*/
5967
public function setSoapOptions(?array $options = null): void;
6068

@@ -87,6 +95,8 @@ public function getLastResponseHeaders(): ?string;
8795
/**
8896
* Call Soap method.
8997
*
98+
* @param array<mixed> $input
99+
*
90100
* @return bool|mixed
91101
*/
92102
public function call(string $method, array $input = []): mixed;

src/Task/RequestTask.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
use Symfony\Component\OptionsResolver\Options;
2222
use Symfony\Component\OptionsResolver\OptionsResolver;
2323

24+
/**
25+
* @phpstan-type RequestOptions array{
26+
* 'client': string,
27+
* 'method': string,
28+
* 'soap_call_options': array<mixed>|null,
29+
* 'soap_call_headers': array<\SoapHeader>|null,
30+
* }
31+
*/
2432
class RequestTask extends AbstractConfigurableTask
2533
{
2634
public function __construct(protected LoggerInterface $logger, protected ClientRegistry $registry)
@@ -29,14 +37,20 @@ public function __construct(protected LoggerInterface $logger, protected ClientR
2937

3038
public function execute(ProcessState $state): void
3139
{
40+
/** @var RequestOptions $options */
3241
$options = $this->getOptions($state);
3342

3443
$client = $this->registry->getClient($options['client']);
3544

45+
/** @var array<mixed> $input */
3646
$input = $state->getInput() ?: [];
3747

38-
$client->setSoapOptions($this->getOption($state, 'soap_call_options'));
39-
$client->setSoapHeaders($this->getOption($state, 'soap_call_headers'));
48+
/** @var array<mixed>|null $soapCallOptions */
49+
$soapCallOptions = $this->getOption($state, 'soap_call_options');
50+
$client->setSoapOptions($soapCallOptions);
51+
/** @var array<\SoapHeader>|null $soapCallHeaders */
52+
$soapCallHeaders = $this->getOption($state, 'soap_call_headers');
53+
$client->setSoapHeaders($soapCallHeaders);
4054

4155
$result = $client->call($options['method'], $input);
4256

@@ -92,7 +106,9 @@ protected function configureOptions(OptionsResolver $resolver): void
92106
$this->configureSoapCallHeaderOption($headerResolver);
93107

94108
$resolvedHeaders = [];
109+
/** @var array<string, array<mixed>> $headers */
95110
foreach ($headers as $name => $header) {
111+
/** @var array{'namespace': string, 'data': array<mixed>} $resolvedHeader */
96112
$resolvedHeader = $headerResolver->resolve($header);
97113
$resolvedHeaders[] = new \SoapHeader($resolvedHeader['namespace'], $name, $resolvedHeader['data']);
98114
}

src/Transformer/RequestTransformer.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,30 @@
1717
use CleverAge\SoapProcessBundle\Registry\ClientRegistry;
1818
use Symfony\Component\OptionsResolver\OptionsResolver;
1919

20+
/**
21+
* @phpstan-type TransformerOptions array{
22+
* 'client': string,
23+
* 'method': string,
24+
* }
25+
*/
2026
class RequestTransformer implements ConfigurableTransformerInterface
2127
{
2228
public function __construct(protected ClientRegistry $registry)
2329
{
2430
}
2531

32+
/**
33+
* @param array<mixed> $options
34+
*/
2635
public function transform(mixed $value, array $options = []): mixed
2736
{
37+
if (!\is_array($value)) {
38+
throw new \UnexpectedValueException('Expecting an array of value');
39+
}
40+
2841
$resolver = new OptionsResolver();
2942
$this->configureOptions($resolver);
43+
/** @var TransformerOptions $options */
3044
$options = $resolver->resolve($options);
3145

3246
$client = $this->registry->getClient($options['client']);

0 commit comments

Comments
 (0)