Skip to content

Commit bcd3764

Browse files
committed
Upgrade PhpStan to level 9
1 parent e1ba475 commit bcd3764

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"scripts": {
3434
"phpstan": [
35-
"vendor/bin/phpstan analyse src -c phpstan.neon --level 6 --no-progress"
35+
"vendor/bin/phpstan analyse src -c phpstan.neon --level 9 --no-progress"
3636
]
3737
},
3838
"minimum-stability": "stable"

src/Convertor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static function covertToArray(string $xml): array
1818
$doc = new \DOMDocument;
1919
$doc->loadXML(str_replace(["\r\n", "\r"], "\n", trim($xml)));
2020
$root = $doc->documentElement;
21+
assert($root !== null);
2122
$output = (array) Helper::domNodeToArray($root);
2223
$output['@root'] = $root->tagName;
2324

src/Helper.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ public function __construct()
1616

1717
/**
1818
* @param \DOMElement|\DOMNode $node
19-
* @return mixed[]|string
19+
* @return array<int|string, mixed>|string
2020
*/
21-
public static function domNodeToArray($node): mixed
21+
public static function domNodeToArray($node): array|string
2222
{
2323
$output = [];
2424
switch ($node->nodeType) {
2525
case 4: // XML_CDATA_SECTION_NODE
2626
case 3: // XML_TEXT_NODE
27-
$output = trim($node->textContent);
28-
break;
27+
return trim($node->textContent);
2928
case 1: // XML_ELEMENT_NODE
3029
for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) {
3130
$child = $node->childNodes->item($i);
31+
assert($child !== null);
3232
$v = self::domNodeToArray($child);
3333
if (isset($child->tagName)) {
3434
$t = $child->tagName;
@@ -42,17 +42,18 @@ public static function domNodeToArray($node): mixed
4242
$output[$t][] = $v;
4343
/** @phpstan-ignore-next-line */
4444
} elseif ($v || $v === '0') {
45-
$output = (string) $v;
45+
$output = is_string($v) ? $v : implode(',', $v);
4646
}
4747
}
48-
if ($node->attributes->length > 0 && !is_array($output)) { // has attributes but isn't an array
48+
if ($node->attributes !== null && $node->attributes->length > 0 && !is_array($output)) { // has attributes but isn't an array
4949
$output = ['@content' => $output]; // change output into an array.
5050
}
5151
if (is_array($output)) {
52-
if ($node->attributes->length > 0) {
52+
if ($node->attributes !== null && $node->attributes->length > 0) {
5353
$a = [];
5454
foreach ($node->attributes as $attrName => $attrNode) {
55-
$a[$attrName] = (string) $attrNode->value;
55+
/** @var \DOMAttr $attrNode */
56+
$a[$attrName] = $attrNode->value;
5657
}
5758
$output['@attributes'] = $a;
5859
}
@@ -65,6 +66,7 @@ public static function domNodeToArray($node): mixed
6566
break;
6667
}
6768

69+
/** @phpstan-ignore-next-line */
6870
return $output;
6971
}
7072
}

0 commit comments

Comments
 (0)