Skip to content

Commit d695e8b

Browse files
authored
Merge pull request #48 from h4kuna/update-phpstan
Update phpstan
2 parents 5e70d2b + 2ba0d67 commit d695e8b

40 files changed

+331
-236
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

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ try {
4141
$response = $ares->loadBasic('87744473');
4242
/* @var $response Ares\Ares\Core\Data */
4343
var_dump($response);
44-
} catch (Ares\Exceptions\IdentificationNumberNotFoundException $e) {
44+
} catch (Ares\Exception\IdentificationNumberNotFoundException $e) {
4545
// log identification number, why is bad? Or make nothing.
46-
} catch (Ares\Exceptions\AdisResponseException $e) {
46+
} catch (Ares\Exception\AdisResponseException $e) {
4747
// if validation by adis failed, but data from ares returned
4848
/* @var $response Ares\Ares\Core\Data */
4949
$response = $e->data;
5050
$response->adis === null; // true
5151
var_dump($e->getMessage());
52-
} catch (Ares\Exceptions\ServerResponseException $e) {
52+
} catch (Ares\Exception\ServerResponseException $e) {
5353
// no response from server or broken json
5454
}
5555
```
@@ -65,7 +65,7 @@ try {
6565
foreach ($ares->loadBasicMulti($numbers) as $name => $r) {
6666
var_dump($name, $r->company);
6767
}
68-
} catch (Ares\Exceptions\ServerResponseException $e) {
68+
} catch (Ares\Exception\ServerResponseException $e) {
6969
// no response from server or broken json
7070
}
7171
```
@@ -114,7 +114,7 @@ use h4kuna\Ares;
114114
try {
115115
$response = $ares->loadDataBox('87744473');
116116
var_dump($response->ISDS);
117-
} catch (h4kuna\Ares\Exceptions\ServerResponseException $e) {
117+
} catch (h4kuna\Ares\Exception\ServerResponseException $e) {
118118
// catch error
119119
}
120120
```
@@ -130,7 +130,7 @@ use h4kuna\Ares;
130130
try {
131131
$response = $ares->checkVatVies($vatNumber);
132132
var_dump($response->valid); // true / false
133-
} catch (Ares\Exceptions\ServerResponseException $e) {
133+
} catch (Ares\Exception\ServerResponseException $e) {
134134
// invalid VAT
135135
}
136136
```

bin/endpoints

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ try {
1313
/* @var $response Ares\Ares\Core\Data */
1414
$response->original = null;
1515
dump($response);
16-
} catch (Ares\Exceptions\IdentificationNumberNotFoundException $e) {
16+
} catch (Ares\Exception\IdentificationNumberNotFoundException $e) {
1717
dumpe($e->getMessage());
1818
// log identification number, why is bad? Or make nothing.
19-
} catch (Ares\Exceptions\AdisResponseException $e) {
19+
} catch (Ares\Exception\AdisResponseException $e) {
2020
dumpe($e->getMessage(), $e->data);
21-
} catch (Ares\Exceptions\ServerResponseException $e) {
21+
} catch (Ares\Exception\ServerResponseException $e) {
2222
// no response from server or broken json
2323
dumpe($e->getMessage());
2424
}
@@ -32,9 +32,9 @@ foreach ($response->sources as $name => $exists) {
3232
try {
3333
$result = $ares->getAresClient()->useEndpoint($name, $IN);
3434
dump($result);
35-
} catch (Ares\Exceptions\IdentificationNumberNotFoundException $e) {
35+
} catch (Ares\Exception\IdentificationNumberNotFoundException $e) {
3636
// log identification number, why is bad? Or make nothing.
37-
} catch (Ares\Exceptions\ServerResponseException $e) {
37+
} catch (Ares\Exception\ServerResponseException $e) {
3838
// no response from server or broken json
3939
}
4040
}

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: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ 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\./'
14+
15+
exceptions: #see https://www.youtube.com/watch?v=UQsP1U0sVZM
16+
check:
17+
missingCheckedExceptionInThrows: true
18+
tooWideThrowType: true
19+
implicitThrows: false
20+
checkedExceptionClasses:
21+
- h4kuna\Ares\Exception\RuntimeException
1722

1823
includes:
1924
- vendor/phpstan/phpstan-strict-rules/rules.neon

src/Adis/Client.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,63 @@
33
namespace h4kuna\Ares\Adis;
44

55
use h4kuna\Ares\Adis\Soap\Envelope;
6-
use h4kuna\Ares\Exceptions\ServerResponseException;
6+
use h4kuna\Ares\Exception\ServerResponseException;
77
use h4kuna\Ares\Http\TransportProvider;
8-
use h4kuna\Ares\Tools\Xml;
8+
use h4kuna\Ares\Tool\Arrays;
9+
use h4kuna\Ares\Tool\Integer;
10+
use h4kuna\Ares\Tool\Strings;
11+
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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use h4kuna\Ares\Adis\StatusBusinessSubjects\StatusBusinessSubjectsTransformer;
77
use h4kuna\Ares\Adis\StatusBusinessSubjects\Subject;
88
use h4kuna\Ares\Ares\Helper;
9-
use h4kuna\Ares\Exceptions\InvalidStateException;
10-
use h4kuna\Ares\Tools\Batch;
9+
use h4kuna\Ares\Exception\LogicException;
10+
use h4kuna\Ares\Exception\ServerResponseException;
11+
use h4kuna\Ares\Tool\Batch;
1112

1213
final class ContentProvider
1314
{
@@ -16,19 +17,24 @@ 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) {
2226
return $subject;
2327
}
2428

25-
throw new InvalidStateException('ADIS must return anything.');
29+
throw new LogicException('ADIS must return anything.');
2630
}
2731

2832

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: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
use Generator;
66
use h4kuna\Ares\Ares\Core;
77
use h4kuna\Ares\Ares\Core\Data;
8-
use h4kuna\Ares\Exceptions\AdisResponseException;
9-
use h4kuna\Ares\Exceptions\IdentificationNumberNotFoundException;
10-
use h4kuna\Ares\Exceptions\ServerResponseException;
8+
use h4kuna\Ares\Exception\AdisResponseException;
9+
use h4kuna\Ares\Exception\IdentificationNumberNotFoundException;
10+
use h4kuna\Ares\Exception\ResultException;
11+
use h4kuna\Ares\Exception\ServerResponseException;
1112
use h4kuna\Ares\Vies\ViesEntity;
1213
use stdClass;
1314

@@ -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: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
namespace h4kuna\Ares\Ares;
44

5-
use h4kuna\Ares\Exceptions\IdentificationNumberNotFoundException;
6-
use h4kuna\Ares\Exceptions\ResultException;
7-
use h4kuna\Ares\Exceptions\ServerResponseException;
5+
use h4kuna\Ares\Exception\IdentificationNumberNotFoundException;
6+
use h4kuna\Ares\Exception\ResultException;
7+
use h4kuna\Ares\Exception\ServerResponseException;
88
use h4kuna\Ares\Http\TransportProvider;
9-
use Nette\Utils\Json;
10-
use Nette\Utils\JsonException;
119
use Psr\Http\Message\ResponseInterface;
1210
use stdClass;
1311

@@ -16,14 +14,15 @@ class Client
1614

1715
public function __construct(
1816
private TransportProvider $transportProvider,
19-
)
20-
{
17+
) {
2118
}
2219

23-
2420
/**
2521
* @param Sources::SERVICE_*|Sources::CORE|Sources::DIAL $key
2622
* @param array<string, mixed> $data
23+
*
24+
* @throws ResultException
25+
* @throws ServerResponseException
2726
*/
2827
public function searchEndpoint(string $key, array $data = []): stdClass
2928
{
@@ -33,9 +32,10 @@ public function searchEndpoint(string $key, array $data = []): stdClass
3332
return $this->responseToStdClass($response);
3433
}
3534

36-
3735
/**
3836
* @param Sources::SERVICE_*|Sources::CORE $key
37+
* @throws IdentificationNumberNotFoundException
38+
* @throws ServerResponseException
3939
*/
4040
public function useEndpoint(string $key, string $in): stdClass
4141
{
@@ -51,7 +51,10 @@ public function useEndpoint(string $key, string $in): stdClass
5151
return $json;
5252
}
5353

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

0 commit comments

Comments
 (0)