Skip to content

Commit d5d05a5

Browse files
committed
phpstan level 9
1 parent 2013e47 commit d5d05a5

22 files changed

+166
-96
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"test:infection": "vendor/bin/infection --threads=$(nproc) --min-msi=100 --verbose --coverage=build/phpunit",
7474
"test:integration": "vendor/bin/phpunit --testsuite=Integration --cache-directory=build/phpunit",
7575
"test:lint": "mkdir -p build && find src tests -name '*.php' -print0 | xargs -0 -n1 -P$(nproc) php -l | tee build/phplint.log",
76-
"test:static-analysis": "mkdir -p build && bash -c 'vendor/bin/phpstan analyse src --no-progress --level=8 --error-format=junit | tee build/phpstan.junit.xml; if [ ${PIPESTATUS[0]} -ne \"0\" ]; then exit 1; fi'",
76+
"test:static-analysis": "mkdir -p build && bash -c 'vendor/bin/phpstan analyse src --no-progress --level=9 --error-format=junit | tee build/phpstan.junit.xml; if [ ${PIPESTATUS[0]} -ne \"0\" ]; then exit 1; fi'",
7777
"test:unit": "vendor/bin/phpunit --testsuite=Unit --coverage-text --coverage-clover=build/phpunit/clover.xml --coverage-html=build/phpunit/coverage-html --coverage-xml=build/phpunit/coverage-xml --log-junit=build/phpunit/junit.xml --cache-directory=build/phpunit"
7878
}
7979
}

phpstan.neon

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1 @@
11
parameters:
2-
ignoreErrors:
3-
-
4-
message: '/has no value type specified in iterable type array/'
5-
path: %currentWorkingDirectory%/src/Decoder/*.php
6-
-
7-
message: '//'
8-
path: %currentWorkingDirectory%/src/Decoder/JsonxTypeDecoder.php
9-
-
10-
message: '//'
11-
path: %currentWorkingDirectory%/src/Decoder/UrlEncodedTypeDecoder.php
12-
-
13-
message: '/with no value type specified in iterable type array/'
14-
path: %currentWorkingDirectory%/src/Encoder/*.php
15-
-
16-
message: '//'
17-
path: %currentWorkingDirectory%/src/Encoder/JsonxTypeEncoder.php

src/Decoder/Decoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function getContentTypes(): array
3333
}
3434

3535
/**
36-
* @return array<string, null|array|bool|float|int|string>
36+
* @return array<string, mixed>
3737
*
3838
* @throws LogicException
3939
* @throws RuntimeException

src/Decoder/DecoderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface DecoderInterface
1515
public function getContentTypes(): array;
1616

1717
/**
18-
* @return array<string, null|array|bool|float|int|string>
18+
* @return array<string, mixed>
1919
*
2020
* @throws LogicException
2121
* @throws RuntimeException

src/Decoder/JsonTypeDecoder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function getContentType(): string
1414
}
1515

1616
/**
17-
* @return array<string, null|array|bool|float|int|string>
17+
* @return array<string, mixed>
1818
*
1919
* @throws RuntimeException
2020
*/
@@ -30,6 +30,8 @@ public function decode(string $data): array
3030
throw RuntimeException::createNotParsable($this->getContentType(), 'Not an object');
3131
}
3232

33+
/** @var array<string, mixed> $decoded */
34+
3335
return $decoded;
3436
}
3537
}

src/Decoder/JsonxTypeDecoder.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function getContentType(): string
2424
}
2525

2626
/**
27-
* @return array<string, null|array|bool|float|int|string>
27+
* @return array<string, mixed>
2828
*
2929
* @throws RuntimeException
3030
*/
@@ -36,10 +36,27 @@ public function decode(string $data): array
3636
throw RuntimeException::createNotParsable($this->getContentType());
3737
}
3838

39-
return $this->decodeNode($document->documentElement);
39+
$documentElement = $document->documentElement;
40+
41+
if (null === $documentElement) {
42+
throw RuntimeException::createNotParsable($this->getContentType());
43+
}
44+
45+
$result = $this->decodeNode($documentElement);
46+
47+
if (!\is_array($result)) {
48+
throw RuntimeException::createNotParsable($this->getContentType());
49+
}
50+
51+
/** @var array<string, mixed> $result */
52+
53+
return $result;
4054
}
4155

42-
private function decodeNode(\DOMNode $node): array|bool|float|int|string|null
56+
/**
57+
* @return null|array<mixed>|bool|float|int|string
58+
*/
59+
private function decodeNode(\DOMElement $node): array|bool|float|int|string|null
4360
{
4461
$nodeName = $node->nodeName;
4562

@@ -73,13 +90,13 @@ private function decodeNode(\DOMNode $node): array|bool|float|int|string|null
7390
}
7491

7592
/**
76-
* @return array<string, null|array|bool|float|int|string>
93+
* @return array<string, mixed>
7794
*/
78-
private function decodeObjectNode(\DOMNode $node): array
95+
private function decodeObjectNode(\DOMElement $node): array
7996
{
8097
$data = [];
8198
foreach ($node->childNodes as $childNode) {
82-
if ($childNode instanceof \DOMText) {
99+
if (!$childNode instanceof \DOMElement) {
83100
continue;
84101
}
85102

@@ -90,13 +107,13 @@ private function decodeObjectNode(\DOMNode $node): array
90107
}
91108

92109
/**
93-
* @return array<int, null|array|bool|float|int|string>
110+
* @return array<int, mixed>
94111
*/
95-
private function decodeArrayNode(\DOMNode $node): array
112+
private function decodeArrayNode(\DOMElement $node): array
96113
{
97114
$data = [];
98115
foreach ($node->childNodes as $childNode) {
99-
if ($childNode instanceof \DOMText) {
116+
if (!$childNode instanceof \DOMElement) {
100117
continue;
101118
}
102119

@@ -106,19 +123,19 @@ private function decodeArrayNode(\DOMNode $node): array
106123
return $data;
107124
}
108125

109-
private function decodeBooleanNode(\DOMNode $node): bool
126+
private function decodeBooleanNode(\DOMElement $node): bool
110127
{
111128
return 'true' === $node->nodeValue;
112129
}
113130

114-
private function decodeStringNode(\DOMNode $node): string
131+
private function decodeStringNode(\DOMElement $node): string
115132
{
116-
return $node->nodeValue;
133+
return $node->nodeValue ?? '';
117134
}
118135

119-
private function decodeNumberNode(\DOMNode $node): float|int
136+
private function decodeNumberNode(\DOMElement $node): float|int
120137
{
121-
$value = $node->nodeValue;
138+
$value = $node->nodeValue ?? '0';
122139

123140
if ($value === (string) (int) $value) {
124141
return (int) $value;

src/Decoder/TypeDecoderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface TypeDecoderInterface
1111
public function getContentType(): string;
1212

1313
/**
14-
* @return array<string, null|array|bool|float|int|string>
14+
* @return array<string, mixed>
1515
*
1616
* @throws RuntimeException
1717
*/

src/Decoder/UrlEncodedTypeDecoder.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,44 @@ public function getContentType(): string
1414
}
1515

1616
/**
17-
* @return array<string, null|array|bool|float|int|string>
17+
* @return array<string, mixed>
1818
*
1919
* @throws RuntimeException
2020
*/
2121
public function decode(string $data): array
2222
{
23+
/** @var array<string, mixed> $rawData */
2324
$rawData = [];
2425
parse_str($data, $rawData);
2526

2627
if ('' !== $data && [] === $rawData) {
2728
throw RuntimeException::createNotParsable($this->getContentType());
2829
}
2930

31+
/** @var array<string, mixed> */
3032
return $this->fixValues($rawData);
3133
}
3234

3335
/**
34-
* @param array<int|string, null|array|bool|float|int|string> $rawData
36+
* @param array<int|string, mixed> $rawData
3537
*
36-
* @return array<int|string, null|array|bool|float|int|string>
38+
* @return array<int|string, mixed>
3739
*/
3840
private function fixValues(array $rawData): array
3941
{
4042
$data = [];
4143
foreach ($rawData as $key => $value) {
42-
$data[$key] = \is_array($value) ? $this->fixValues($value) : $this->fixValue($value);
44+
if (\is_array($value)) {
45+
$data[$key] = $this->fixValues($value);
46+
} elseif (\is_string($value)) {
47+
$data[$key] = $this->fixValue($value);
48+
}
4349
}
4450

4551
return $data;
4652
}
4753

48-
private function fixValue(string $value): bool|float|int|string|null
54+
private function fixValue(string $value): bool|float|int|string
4955
{
5056
if ('true' === $value) {
5157
return true;

src/Decoder/XmlTypeDecoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function getContentType(): string
1919
}
2020

2121
/**
22-
* @return array<string, null|array|bool|float|int|string>
22+
* @return array<string, mixed>
2323
*/
2424
public function decode(string $data): array
2525
{

src/Decoder/YamlTypeDecoder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function getContentType(): string
1616
}
1717

1818
/**
19-
* @return array<string, null|array|bool|float|int|string>
19+
* @return array<string, mixed>
2020
*
2121
* @throws RuntimeException
2222
*/
@@ -32,6 +32,8 @@ public function decode(string $data): array
3232
throw RuntimeException::createNotParsable($this->getContentType(), 'Not an object');
3333
}
3434

35+
/** @var array<string, mixed> $decoded */
36+
3537
return $decoded;
3638
}
3739
}

0 commit comments

Comments
 (0)