These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Serialize a DOM to BadgerFish Json: http://badgerfish.ning.com/ |
||
4 | * |
||
5 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License |
||
6 | * @copyright Copyright (c) 2009-2017 FluentDOM Contributors |
||
7 | */ |
||
8 | |||
9 | namespace FluentDOM\Serializer\Json { |
||
10 | |||
11 | use FluentDOM\DOM\Xpath; |
||
12 | use FluentDOM\Serializer\Json; |
||
13 | |||
14 | /** |
||
15 | * Serialize a DOM to BadgerFish Json: http://badgerfish.ning.com/ |
||
16 | * |
||
17 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License |
||
18 | * @copyright Copyright (c) 2009-2017 FluentDOM Contributors |
||
19 | */ |
||
20 | class BadgerFish extends Json { |
||
21 | |||
22 | /** |
||
23 | * @param \DOMElement $node |
||
24 | * @return mixed |
||
25 | */ |
||
26 | 7 | protected function getNode(\DOMElement $node) { |
|
27 | 7 | $result = new \stdClass(); |
|
28 | 7 | $result->{$node->nodeName} = $this->getNodes($node); |
|
29 | 7 | return $result; |
|
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param \DOMElement $node |
||
34 | * @return mixed |
||
35 | */ |
||
36 | 7 | protected function getNodes(\DOMElement $node) { |
|
37 | 7 | $result = new \stdClass(); |
|
38 | 7 | $xpath = new Xpath($node->ownerDocument); |
|
39 | 7 | $this->addNamespaces($result, $node, $xpath); |
|
40 | 7 | $this->addAttributes($result, $node, $xpath); |
|
41 | 7 | $nodes = $xpath->evaluate('*|text()', $node); |
|
42 | 7 | foreach ($nodes as $childNode) { |
|
1 ignored issue
–
show
|
|||
43 | 7 | if ($childNode instanceof \DOMElement) { |
|
44 | 3 | $this->addElement($result, $childNode); |
|
45 | } else { |
||
46 | 7 | $this->addText($result, $childNode); |
|
47 | } |
||
48 | } |
||
49 | 7 | return $result; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param \stdClass $target |
||
54 | * @param \DOMElement $node |
||
55 | * @param Xpath $xpath |
||
56 | */ |
||
57 | 7 | protected function addAttributes(\stdClass $target, \DOMElement $node, Xpath $xpath) { |
|
58 | 7 | $nodes = $xpath->evaluate('@*', $node); |
|
59 | 7 | foreach ($nodes as $attribute) { |
|
1 ignored issue
–
show
The expression
$nodes of type string|double|boolean|object<DOMNodeList> is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
60 | 1 | $target->{'@'.$attribute->name} = $attribute->value; |
|
61 | } |
||
62 | 7 | } |
|
63 | |||
64 | /** |
||
65 | * @param \stdClass $target |
||
66 | * @param \DOMElement $node |
||
67 | * @param Xpath $xpath |
||
68 | */ |
||
69 | 7 | protected function addNamespaces(\stdClass $target, \DOMElement $node, Xpath $xpath) { |
|
70 | 7 | if ((string)$node->namespaceURI !== '' && $node->prefix === '') { |
|
71 | 3 | if (!isset($target->{'@xmlns'})) { |
|
72 | 3 | $target->{'@xmlns'} = new \stdClass(); |
|
73 | } |
||
74 | 3 | $target->{'@xmlns'}->{'$'} = $node->namespaceURI; |
|
75 | } |
||
76 | 7 | $nodes = $xpath->evaluate('namespace::*', $node); |
|
77 | 7 | foreach ($nodes as $namespaceNode) { |
|
1 ignored issue
–
show
The expression
$nodes of type string|double|boolean|object<DOMNodeList> is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
78 | 7 | if ($namespaceNode->localName === 'xml' || $namespaceNode->localName === 'xmlns') { |
|
79 | 7 | continue; |
|
80 | } |
||
81 | 2 | if (!isset($target->{'@xmlns'})) { |
|
82 | 1 | $target->{'@xmlns'} = new \stdClass(); |
|
83 | } |
||
84 | 2 | if ($namespaceNode->nodeName !== 'xmlns') { |
|
85 | 2 | $target->{'@xmlns'}->{$namespaceNode->localName} = $namespaceNode->namespaceURI; |
|
86 | } |
||
87 | } |
||
88 | 7 | } |
|
89 | |||
90 | /** |
||
91 | * @param \stdClass $target |
||
92 | * @param \DOMElement $node |
||
93 | */ |
||
94 | 3 | private function addElement(\stdClass $target, \DOMElement $node) { |
|
95 | 3 | $nodeName = $node->nodeName; |
|
96 | 3 | if (isset($target->$nodeName)) { |
|
97 | 1 | if (!is_array($target->$nodeName)) { |
|
98 | 1 | $target->{$nodeName} = [$target->{$nodeName}]; |
|
99 | } |
||
100 | 1 | $target->{$nodeName}[] = $this->getNodes($node); |
|
101 | } else { |
||
102 | 3 | $target->$nodeName = $this->getNodes($node); |
|
103 | } |
||
104 | 3 | } |
|
105 | |||
106 | /** |
||
107 | * @param \stdClass $target |
||
108 | * @param \DOMNode|\DOMText|\DOMCdataSection $node |
||
109 | */ |
||
110 | 7 | private function addText(\stdClass $target, \DOMNode $node) { |
|
111 | 7 | if (!$node->isWhitespaceInElementContent()) { |
|
112 | 7 | if (!isset($target->{'$'})) { |
|
113 | 7 | $target->{'$'} = ''; |
|
114 | } |
||
115 | 7 | $target->{'$'} .= $node->textContent; |
|
116 | } |
||
117 | 7 | } |
|
118 | } |
||
119 | } |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.