This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace PHPDaemon\XMLStream; |
||
3 | |||
4 | class XMLStream |
||
5 | { |
||
6 | use \PHPDaemon\Traits\EventHandlers; |
||
7 | use \PHPDaemon\Traits\ClassWatchdog; |
||
8 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
9 | |||
10 | protected $parser; |
||
11 | protected $xml_depth = 0; |
||
12 | protected $current_ns = []; |
||
13 | protected $idhandlers = []; |
||
14 | protected $xpathhandlers = []; |
||
15 | protected $default_ns; |
||
16 | |||
17 | /** |
||
18 | * Constructor |
||
19 | * @return void |
||
0 ignored issues
–
show
|
|||
20 | */ |
||
21 | public function __construct() |
||
22 | { |
||
23 | $this->parser = xml_parser_create('UTF-8'); |
||
24 | xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1); |
||
25 | xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); |
||
26 | xml_set_object($this->parser, $this); |
||
27 | xml_set_element_handler($this->parser, 'startXML', 'endXML'); |
||
28 | xml_set_character_data_handler($this->parser, 'charXML'); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Set default namespace |
||
33 | * @param string $ns |
||
34 | * @return void |
||
35 | */ |
||
36 | public function setDefaultNS($ns) |
||
37 | { |
||
38 | $this->default_ns = $ns; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Finishes the stream |
||
43 | * @return void |
||
44 | */ |
||
45 | public function finish() |
||
46 | { |
||
47 | $this->xml_depth = 0; |
||
48 | $this->current_ns = []; |
||
49 | $this->idhandlers = []; |
||
50 | $this->xpathhandlers = []; |
||
51 | $this->eventHandlers = []; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Destructor |
||
56 | * @return void |
||
57 | */ |
||
58 | public function __destroy() |
||
59 | { |
||
60 | if ($this->parser) { |
||
61 | xml_parse($this->parser, '', true); |
||
62 | xml_parser_free($this->parser); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Feed stream |
||
68 | * @param string $buf |
||
69 | * @return void |
||
70 | */ |
||
71 | public function feed($buf) |
||
72 | { |
||
73 | xml_parse($this->parser, $buf, false); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Finalize stream |
||
78 | * @return void |
||
79 | */ |
||
80 | public function finalize() |
||
81 | { |
||
82 | xml_parse($this->parser, '', true); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * XML start callback |
||
87 | * |
||
88 | * @see xml_set_element_handler |
||
89 | * @param resource $parser |
||
90 | * @param string $name |
||
91 | * @param array $attr |
||
92 | * @return void |
||
93 | */ |
||
94 | public function startXML($parser, $name, $attr) |
||
0 ignored issues
–
show
|
|||
95 | { |
||
96 | ++$this->xml_depth; |
||
97 | if (array_key_exists('XMLNS', $attr)) { |
||
98 | $this->current_ns[$this->xml_depth] = $attr['XMLNS']; |
||
99 | } else { |
||
100 | $this->current_ns[$this->xml_depth] = $this->current_ns[$this->xml_depth - 1]; |
||
101 | if (!$this->current_ns[$this->xml_depth]) { |
||
102 | $this->current_ns[$this->xml_depth] = $this->default_ns; |
||
103 | } |
||
104 | } |
||
105 | $ns = $this->current_ns[$this->xml_depth]; |
||
106 | foreach ($attr as $key => $value) { |
||
107 | View Code Duplication | if (mb_orig_strpos($key, ':') !== false) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
108 | $key = explode(':', $key); |
||
109 | $key = $key[1]; |
||
110 | $this->ns_map[$key] = $value; |
||
0 ignored issues
–
show
The property
ns_map does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
![]() |
|||
111 | } |
||
112 | } |
||
113 | View Code Duplication | if (mb_orig_strpos($name, ':') !== false) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
114 | $name = explode(':', $name); |
||
115 | $ns = $this->ns_map[$name[0]]; |
||
116 | $name = $name[1]; |
||
117 | } |
||
118 | $obj = new XMLStreamObject($name, $ns, $attr); |
||
119 | if ($this->xml_depth > 1) { |
||
120 | $this->xmlobj[$this->xml_depth - 1]->subs[] = $obj; |
||
0 ignored issues
–
show
The property
xmlobj does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
![]() |
|||
121 | } |
||
122 | $this->xmlobj[$this->xml_depth] = $obj; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * XML end callback |
||
127 | * |
||
128 | * @see xml_set_element_handler |
||
129 | * |
||
130 | * @param resource $parser |
||
131 | * @param string $name |
||
132 | * @return void |
||
133 | */ |
||
134 | public function endXML($parser, $name) |
||
0 ignored issues
–
show
|
|||
135 | { |
||
136 | --$this->xml_depth; |
||
137 | if ($this->xml_depth === 1) { |
||
138 | foreach ($this->xpathhandlers as $handler) { |
||
139 | if (is_array($this->xmlobj) && array_key_exists(2, $this->xmlobj)) { |
||
140 | $searchxml = $this->xmlobj[2]; |
||
141 | $nstag = array_shift($handler[0]); |
||
142 | if (($nstag[0] === null or $searchxml->ns === $nstag[0]) and ($nstag[1] === "*" or $nstag[1] === $searchxml->name)) { |
||
143 | foreach ($handler[0] as $nstag) { |
||
144 | if ($searchxml !== null and $searchxml->hasSub($nstag[1], $ns = $nstag[0])) { |
||
145 | $searchxml = $searchxml->sub($nstag[1], $ns = $nstag[0]); |
||
146 | } else { |
||
147 | $searchxml = null; |
||
148 | break; |
||
149 | } |
||
150 | } |
||
151 | if ($searchxml !== null) { |
||
152 | if ($handler[2] === null) { |
||
153 | $handler[2] = $this; |
||
154 | } |
||
155 | $handler[1]($this->xmlobj[2]); |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | foreach ($this->idhandlers as $id => $handler) { |
||
161 | if (array_key_exists('id', $this->xmlobj[2]->attrs) and $this->xmlobj[2]->attrs['id'] == $id) { |
||
162 | $handler($this->xmlobj[2]); |
||
163 | #id handlers are only used once |
||
164 | unset($this->idhandlers[$id]); |
||
165 | break; |
||
166 | } |
||
167 | } |
||
168 | if (is_array($this->xmlobj)) { |
||
169 | $this->xmlobj = array_slice($this->xmlobj, 0, 1); |
||
170 | if (isset($this->xmlobj[0]) && $this->xmlobj[0] instanceof XMLStreamObject) { |
||
171 | $this->xmlobj[0]->subs = null; |
||
0 ignored issues
–
show
It seems like
null of type null is incompatible with the declared type array of property $subs .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. ![]() |
|||
172 | } |
||
173 | } |
||
174 | unset($this->xmlobj[2]); |
||
175 | } |
||
176 | if ($this->xml_depth === 0) { |
||
177 | $this->event('streamEnd'); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * XML character callback |
||
183 | * @see xml_set_character_data_handler |
||
184 | * |
||
185 | * @param resource $parser |
||
186 | * @param string $data |
||
187 | */ |
||
188 | public function charXML($parser, $data) |
||
0 ignored issues
–
show
|
|||
189 | { |
||
190 | if (array_key_exists($this->xml_depth, $this->xmlobj)) { |
||
191 | $this->xmlobj[$this->xml_depth]->data .= $data; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Get next ID |
||
197 | * |
||
198 | * @return integer |
||
199 | */ |
||
200 | public function getId() |
||
201 | { |
||
202 | $this->lastid++; |
||
0 ignored issues
–
show
The property
lastid does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
![]() |
|||
203 | return $this->lastid; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Add ID Handler |
||
208 | * |
||
209 | * @param integer $id |
||
210 | * @param callable $cb |
||
211 | */ |
||
212 | public function addIdHandler($id, $cb) |
||
213 | { |
||
214 | if ($cb === null) { |
||
215 | return; |
||
216 | } |
||
217 | $this->idhandlers[$id] = $cb; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Add XPath Handler |
||
222 | * |
||
223 | * @param string $xpath |
||
224 | * @param \Closure $cb |
||
225 | * @param null $obj |
||
226 | */ |
||
227 | public function addXPathHandler($xpath, $cb, $obj = null) |
||
228 | { |
||
229 | if (preg_match_all("/\(?{[^\}]+}\)?(\/?)[^\/]+/", $xpath, $regs)) { |
||
230 | $ns_tags = $regs[0]; |
||
231 | } else { |
||
232 | $ns_tags = [$xpath]; |
||
233 | } |
||
234 | foreach ($ns_tags as $ns_tag) { |
||
235 | list($l, $r) = explode("}", $ns_tag); |
||
236 | if ($r !== null) { |
||
237 | $xpart = [substr($l, 1), $r]; |
||
238 | } else { |
||
239 | $xpart = [null, $l]; |
||
240 | } |
||
241 | $xpath_array[] = $xpart; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$xpath_array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $xpath_array = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop. ![]() |
|||
242 | } |
||
243 | $this->xpathhandlers[] = [$xpath_array, $cb, $obj, $xpath]; |
||
0 ignored issues
–
show
The variable
$xpath_array does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
![]() |
|||
244 | } |
||
245 | } |
||
246 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.