Skip to content

Commit 2ba0d67

Browse files
committed
feat(composer): update phpstan
1 parent 880f343 commit 2ba0d67

27 files changed

+211
-99
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
runs-on: ubuntu-latest
3939
strategy:
4040
matrix:
41-
php: ['8.0', '8.1', '8.2', '8.3', '8.4']
41+
php: ['8.0', '8.1', '8.2', '8.3'] # exclude 8.4
4242
name: PHP Lowest Dependencies ${{ matrix.php }}
4343
steps:
4444
- uses: actions/checkout@v3

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
},
4949
"scripts": {
5050
"coverage": "vendor/bin/tester --coverage coverage.html --coverage-src src/ --colors 1 -s -C tests/src",
51-
"qa": "composer stan && composer tests",
51+
"qa": ["@tests", "@stan"],
5252
"stan": "vendor/bin/phpstan analyse",
5353
"tests": "vendor/bin/tester --colors 1 -s -C tests/src"
5454
},

phpstan.neon

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ parameters:
1010
message: "#^Variable property access on \\$this\\(h4kuna\\\\Ares\\\\Ares\\\\Core\\\\Data\\)\\.$#"
1111
count: 3
1212
path: src/Ares/Core/Data.php
13-
-
14-
message: "#^If condition is always false\\.$#"
15-
count: 1
16-
path: src/aliases.php
13+
- '/Method h4kuna\\Ares\\Tests\\.*? throws checked exception .*? but it.s missing from the PHPDoc @throws tag\./'
1714

1815
exceptions: #see https://www.youtube.com/watch?v=UQsP1U0sVZM
1916
check:

src/Adis/Client.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,61 @@
55
use h4kuna\Ares\Adis\Soap\Envelope;
66
use h4kuna\Ares\Exception\ServerResponseException;
77
use h4kuna\Ares\Http\TransportProvider;
8+
use h4kuna\Ares\Tool\Arrays;
9+
use h4kuna\Ares\Tool\Integer;
10+
use h4kuna\Ares\Tool\Strings;
811
use h4kuna\Ares\Tool\Xml;
12+
use SimpleXMLElement;
913
use stdClass;
1014

1115
final class Client
1216
{
1317
public static string $url = 'https://adisrws.mfcr.cz/adistc/axis2/services/rozhraniCRPDPH.rozhraniCRPDPHSOAP';
1418

15-
16-
public function __construct(private TransportProvider $transportProvider,)
17-
{
19+
public function __construct(
20+
private TransportProvider $transportProvider,
21+
) {
1822
}
1923

2024

2125
/**
2226
* @param array<string, string> $chunk
23-
* @return array<stdClass>
27+
* @return list<stdClass>
28+
*
29+
* @throws ServerResponseException
2430
*/
2531
public function statusBusinessSubjects(array $chunk): array
2632
{
2733
$xml = Envelope::StatusNespolehlivySubjektRozsireny(...$chunk);
2834
$data = $this->request($xml, 'StatusNespolehlivySubjektRozsirenyResponse');
2935
$attributes = '@attributes';
30-
assert($data->status instanceof stdClass);
31-
if (isset($data->status->$attributes) === false) {
32-
throw new ServerResponseException('Broken response xml.');
36+
37+
if (
38+
$data->status instanceof stdClass === false
39+
|| isset($data->status->$attributes) === false
40+
|| $data->status->$attributes instanceof stdClass === false
41+
) {
42+
throw ServerResponseException::brokenXml();
3343
}
3444
$element = $data->status->$attributes;
35-
assert($element instanceof stdClass);
36-
3745
if ($element->statusCode !== '0') {
38-
throw new ServerResponseException($element->statusText, (int) $element->statusCode);
46+
throw ServerResponseException::badResponse(Strings::fromMixedStrict($element->statusText), (int) Integer::fromMixed($element->statusCode));
3947
}
4048

41-
return is_array($data->statusSubjektu) ? $data->statusSubjektu : [$data->statusSubjektu];
49+
return Arrays::fromStdClass($data->statusSubjektu);
4250
}
4351

44-
52+
/**
53+
* @throws ServerResponseException
54+
*/
4555
private function request(string $xml, string $name): stdClass
4656
{
4757
$request = $this->transportProvider->createXmlRequest(self::$url, $xml);
4858
$response = $this->transportProvider->response($request);
4959
$xml = @simplexml_load_string($response->getBody()->getContents(), namespace_or_prefix: 'soapenv', is_prefix: true);
5060

51-
if ($xml === false || isset($xml->Body->children()->$name) === false) {
52-
throw new ServerResponseException(sprintf('Missing tag "%s" in response.', $name));
61+
if ($xml === false || ($xml->Body->children()->$name instanceof SimpleXMLElement) === false) {
62+
throw ServerResponseException::badResponse(sprintf('Missing tag "%s" in response.', $name));
5363
}
5464

5565
return Xml::toJson($xml->Body->children()->$name);

src/Adis/ContentProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use h4kuna\Ares\Adis\StatusBusinessSubjects\Subject;
88
use h4kuna\Ares\Ares\Helper;
99
use h4kuna\Ares\Exception\LogicException;
10+
use h4kuna\Ares\Exception\ServerResponseException;
1011
use h4kuna\Ares\Tool\Batch;
1112

1213
final class ContentProvider
@@ -16,6 +17,9 @@ public function __construct(private Client $client, private StatusBusinessSubjec
1617
}
1718

1819

20+
/**
21+
* @throws ServerResponseException
22+
*/
1923
public function statusBusinessSubject(string $tin): Subject
2024
{
2125
foreach ($this->statusBusinessSubjects([$tin => $tin]) as $subject) {
@@ -29,6 +33,8 @@ public function statusBusinessSubject(string $tin): Subject
2933
/**
3034
* @param array<string, string> $tin
3135
* @return Generator<string, Subject>
36+
*
37+
* @throws ServerResponseException
3238
*/
3339
public function statusBusinessSubjects(array $tin): Generator
3440
{

src/Ares.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use h4kuna\Ares\Ares\Core\Data;
88
use h4kuna\Ares\Exception\AdisResponseException;
99
use h4kuna\Ares\Exception\IdentificationNumberNotFoundException;
10+
use h4kuna\Ares\Exception\ResultException;
1011
use h4kuna\Ares\Exception\ServerResponseException;
1112
use h4kuna\Ares\Vies\ViesEntity;
1213
use stdClass;
@@ -21,11 +22,9 @@ public function __construct(
2122
public DataBox\ContentProvider $dataBoxContentProvider,
2223
public Adis\ContentProvider $adisContentProvider,
2324
public Vies\ContentProvider $viesContentProvider,
24-
)
25-
{
25+
) {
2626
}
2727

28-
2928
/**
3029
* @deprecated use property
3130
*/
@@ -34,43 +33,44 @@ public function getAdis(): Adis\ContentProvider
3433
return $this->adisContentProvider;
3534
}
3635

37-
3836
public function getAresClient(): Ares\Client
3937
{
4038
return $this->aresContentProvider->getClient();
4139
}
4240

43-
4441
/**
4542
* @template KeyName
4643
* @param array<KeyName, string|int> $identificationNumbers
4744
* @return Generator<(int&KeyName)|(KeyName&string), Data>
45+
*
46+
* @throws ResultException
47+
* @throws ServerResponseException
4848
*/
4949
public function loadBasicMulti(array $identificationNumbers): Generator
5050
{
5151
return $this->aresContentProvider->loadByIdentificationNumbers($identificationNumbers);
5252
}
5353

54-
5554
/**
56-
* @throws IdentificationNumberNotFoundException
5755
* @throws AdisResponseException
56+
* @throws IdentificationNumberNotFoundException
57+
* @throws ServerResponseException
5858
*/
5959
public function loadBasic(string $in): Data
6060
{
6161
return $this->aresContentProvider->load($in);
6262
}
6363

64-
6564
/**
6665
* @return array<stdClass>
66+
* @throws ResultException
67+
* @throws ServerResponseException
6768
*/
6869
public function loadDataBox(string $in): array
6970
{
7071
return $this->dataBoxContentProvider->load($in);
7172
}
7273

73-
7474
/**
7575
* @return object{countryCode: string, vatNumber: string, requestDate: string, valid: bool, requestIdentifier: string, name: string, address: string, traderName: string, traderStreet: string, traderPostalCode: string, traderCity: string, traderCompanyType: string, traderNameMatch: string, traderStreetMatch: string, traderPostalCodeMatch: string, traderCityMatch: string, traderCompanyTypeMatch: string}
7676
*

src/Ares/Client.php

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

55
use h4kuna\Ares\Exception\IdentificationNumberNotFoundException;
66
use h4kuna\Ares\Exception\ResultException;
7+
use h4kuna\Ares\Exception\ServerResponseException;
78
use h4kuna\Ares\Http\TransportProvider;
89
use Psr\Http\Message\ResponseInterface;
910
use stdClass;
@@ -13,14 +14,15 @@ class Client
1314

1415
public function __construct(
1516
private TransportProvider $transportProvider,
16-
)
17-
{
17+
) {
1818
}
1919

20-
2120
/**
2221
* @param Sources::SERVICE_*|Sources::CORE|Sources::DIAL $key
2322
* @param array<string, mixed> $data
23+
*
24+
* @throws ResultException
25+
* @throws ServerResponseException
2426
*/
2527
public function searchEndpoint(string $key, array $data = []): stdClass
2628
{
@@ -30,9 +32,10 @@ public function searchEndpoint(string $key, array $data = []): stdClass
3032
return $this->responseToStdClass($response);
3133
}
3234

33-
3435
/**
3536
* @param Sources::SERVICE_*|Sources::CORE $key
37+
* @throws IdentificationNumberNotFoundException
38+
* @throws ServerResponseException
3639
*/
3740
public function useEndpoint(string $key, string $in): stdClass
3841
{
@@ -48,7 +51,10 @@ public function useEndpoint(string $key, string $in): stdClass
4851
return $json;
4952
}
5053

51-
54+
/**
55+
* @throws ResultException
56+
* @throws ServerResponseException
57+
*/
5258
protected function responseToStdClass(ResponseInterface $response): stdClass
5359
{
5460
$json = $this->transportProvider->toJson($response);

src/Ares/Core/ContentProvider.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,33 @@
99
use h4kuna\Ares\Ares\Sources;
1010
use h4kuna\Ares\Exception\AdisResponseException;
1111
use h4kuna\Ares\Exception\IdentificationNumberNotFoundException;
12+
use h4kuna\Ares\Exception\ResultException;
1213
use h4kuna\Ares\Exception\ServerResponseException;
1314
use h4kuna\Ares\Tool\Batch;
1415

1516
final class ContentProvider
1617
{
1718
private const BATCH = 100; // max identification numbers per request
1819

19-
2020
public function __construct(
2121
private JsonToDataTransformer $jsonTransformer,
2222
private Client $client,
2323
private Adis\ContentProvider $adisContentProvider,
24-
)
25-
{
24+
) {
2625
}
2726

28-
2927
public function getClient(): Client
3028
{
3129
return $this->client;
3230
}
3331

34-
3532
/**
3633
* @template KeyName
3734
* @param array<KeyName, string|int> $identificationNumbers
3835
* @return Generator<(int&KeyName)|(KeyName&string), Data>
36+
*
37+
* @throws ResultException
38+
* @throws ServerResponseException
3939
*/
4040
public function loadByIdentificationNumbers(array $identificationNumbers): Generator
4141
{
@@ -74,10 +74,10 @@ public function loadByIdentificationNumbers(array $identificationNumbers): Gener
7474
}
7575
}
7676

77-
7877
/**
79-
* @throws IdentificationNumberNotFoundException
8078
* @throws AdisResponseException
79+
* @throws IdentificationNumberNotFoundException
80+
* @throws ServerResponseException
8181
*/
8282
public function load(string $in): Data
8383
{
@@ -88,7 +88,7 @@ public function load(string $in): Data
8888
try {
8989
$adis = $this->adisContentProvider->statusBusinessSubject($data->tin);
9090
} catch (ServerResponseException $e) {
91-
throw new AdisResponseException($data, previous: $e);
91+
throw AdisResponseException::fromServerException($data, $e);
9292
}
9393

9494
$data->setAdis($adis);

src/Ares/Helper.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
use h4kuna\Ares\Tool\Strings;
88
use Nette\Utils\Strings as NetteStrings;
99

10+
/**
11+
* @phpstan-type addressTypeRaw array{street?: string, zip?: string, city?: string, house_number?: string, country?: string}
12+
* @phpstan-type addressType array{street: ?string, zip: ?string, city: ?string, house_number: ?string, country: ?string}
13+
*/
1014
final class Helper
1115
{
1216
public static string $baseUrl = 'https://ares.gov.cz/ekonomicke-subjekty-v-be/rest';
@@ -126,25 +130,27 @@ public static function normalizeIN(string $in): string
126130

127131

128132
/**
129-
* @return array{street: ?string, zip: ?string, city: ?string, house_number: ?string, country: ?string}
133+
* @return addressType
130134
*/
131135
public static function parseAddress(string $address): array
132136
{
137+
/** @var ?addressTypeRaw $results */
133138
$results = NetteStrings::match($address, '~^(?<street>.+) (?<house_number>\d+(?:/\d+)?(\w)?)(?:, (?<district>.+?))?, (?<zip>\d{5}) (?<city>.+?)(, (?<country>.+))?$~');
134139

135140
if ($results !== null) {
136141
return self::prepareAddressData($results);
137142
}
138143

144+
/** @var ?addressTypeRaw $results */
139145
$results = NetteStrings::match($address, '~^(?<city>.+), (?<zip>\d{5})(?:, (?<district>.+?))?, (?<street>.+), (?<house_number>\d+(?:/\d+)?(\w)?)$~');
140146

141147
return self::prepareAddressData($results ?? []);
142148
}
143149

144150

145151
/**
146-
* @param array{street?: string, zip?: string, city?: string, house_number?: string, country?: string} $results
147-
* @return array{street: ?string, zip: ?string, city: ?string, house_number: ?string, country: ?string}
152+
* @param addressTypeRaw $results
153+
* @return addressType
148154
*/
149155
private static function prepareAddressData(array $results): array
150156
{

src/DataBox/Client.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use h4kuna\Ares\Exception\ResultException;
66
use h4kuna\Ares\Exception\ServerResponseException;
77
use h4kuna\Ares\Http\TransportProvider;
8+
use h4kuna\Ares\Tool\Strings;
89
use h4kuna\Ares\Tool\Xml;
910
use Psr\Http\Message\StreamInterface;
1011
use stdClass;
@@ -16,14 +17,15 @@ final class Client
1617
{
1718
public static string $url = 'https://www.mojedatovaschranka.cz/sds/ws/call';
1819

19-
2020
public function __construct(
2121
private TransportProvider $requestProvider,
22-
)
23-
{
22+
) {
2423
}
2524

26-
25+
/**
26+
* @throws ResultException
27+
* @throws ServerResponseException
28+
*/
2729
public function request(StreamInterface $body): stdClass
2830
{
2931
$request = $this->requestProvider->createXmlRequest(self::$url, $body);
@@ -33,9 +35,9 @@ public function request(StreamInterface $body): stdClass
3335
$data = Xml::toJson($response);
3436

3537
if (isset($data->Message)) {
36-
throw new ResultException($data->Message);
38+
throw ResultException::withMessage(Strings::fromMixedStrict($data->Message));
3739
} elseif (isset($data->Osoba) === false) {
38-
throw new ServerResponseException('No content');
40+
throw ServerResponseException::badResponse('No content');
3941
}
4042

4143
return $data;

0 commit comments

Comments
 (0)