@@ -16,19 +16,19 @@ public function __construct()
16
16
17
17
/**
18
18
* @param \DOMElement|\DOMNode $node
19
- * @return mixed[] |string
19
+ * @return array<int|string, mixed> |string
20
20
*/
21
- public static function domNodeToArray ($ node ): mixed
21
+ public static function domNodeToArray ($ node ): array | string
22
22
{
23
23
$ output = [];
24
24
switch ($ node ->nodeType ) {
25
25
case 4 : // XML_CDATA_SECTION_NODE
26
26
case 3 : // XML_TEXT_NODE
27
- $ output = trim ($ node ->textContent );
28
- break ;
27
+ return trim ($ node ->textContent );
29
28
case 1 : // XML_ELEMENT_NODE
30
29
for ($ i = 0 , $ m = $ node ->childNodes ->length ; $ i < $ m ; $ i ++) {
31
30
$ child = $ node ->childNodes ->item ($ i );
31
+ assert ($ child !== null );
32
32
$ v = self ::domNodeToArray ($ child );
33
33
if (isset ($ child ->tagName )) {
34
34
$ t = $ child ->tagName ;
@@ -42,17 +42,18 @@ public static function domNodeToArray($node): mixed
42
42
$ output [$ t ][] = $ v ;
43
43
/** @phpstan-ignore-next-line */
44
44
} elseif ($ v || $ v === '0 ' ) {
45
- $ output = ( string ) $ v ;
45
+ $ output = is_string ( $ v ) ? $ v : implode ( ' , ' , $ v ) ;
46
46
}
47
47
}
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
49
49
$ output = ['@content ' => $ output ]; // change output into an array.
50
50
}
51
51
if (is_array ($ output )) {
52
- if ($ node ->attributes ->length > 0 ) {
52
+ if ($ node ->attributes !== null && $ node -> attributes ->length > 0 ) {
53
53
$ a = [];
54
54
foreach ($ node ->attributes as $ attrName => $ attrNode ) {
55
- $ a [$ attrName ] = (string ) $ attrNode ->value ;
55
+ /** @var \DOMAttr $attrNode */
56
+ $ a [$ attrName ] = $ attrNode ->value ;
56
57
}
57
58
$ output ['@attributes ' ] = $ a ;
58
59
}
@@ -65,6 +66,7 @@ public static function domNodeToArray($node): mixed
65
66
break ;
66
67
}
67
68
69
+ /** @phpstan-ignore-next-line */
68
70
return $ output ;
69
71
}
70
72
}
0 commit comments