@@ -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