Conditions | 17 |
Paths | 97 |
Total Lines | 186 |
Code Lines | 94 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
251 | public function customXPath($xPath, $newGroupFlag = false, $value = '', $attributeOnly = false) |
||
252 | { |
||
253 | if (!$attributeOnly) { |
||
254 | // Explode xPath |
||
255 | $newPath = explode('%', $xPath); |
||
256 | |||
257 | $praedicateFlag = false; |
||
258 | $explodedXPath = explode('[', $newPath[0]); |
||
259 | if (count($explodedXPath) > 1) { |
||
260 | // praedicate is given |
||
261 | if (substr($explodedXPath[1], 0, 1) == "@") { |
||
262 | // attribute |
||
263 | $path = $newPath[0]; |
||
264 | } else { |
||
265 | // path |
||
266 | $path = $explodedXPath[0]; |
||
267 | } |
||
268 | |||
269 | $praedicateFlag = true; |
||
270 | } else { |
||
271 | $path = $newPath[0]; |
||
272 | } |
||
273 | |||
274 | if (!empty($value)) { |
||
275 | $newPath[1] = $newPath[1] . '="' . $value . '"'; |
||
276 | } |
||
277 | |||
278 | $modsDataXPath = \EWW\Dpf\Helper\XPath::create($this->xmlData); |
||
279 | |||
280 | if (!$newGroupFlag && $modsDataXPath->query('/data/' . $newPath[0])->length > 0) { |
||
281 | // first xpath path exist |
||
282 | |||
283 | // build xml from second xpath part |
||
284 | $xml = $this->parseXPath($newPath[1]); |
||
285 | |||
286 | // check if xpath [] are nested |
||
287 | $search = '/(\/\w*:\w*)\[(.*)\]/'; |
||
288 | preg_match($search, $newPath[1], $match); |
||
289 | preg_match($search, $match[2], $secondMatch); |
||
290 | // first part nested xpath |
||
291 | if ($match[2] && $secondMatch[2]) { |
||
292 | $nested = $match[2]; |
||
293 | |||
294 | $nestedXml = $this->parseXPath($nested); |
||
295 | |||
296 | // object xpath without nested element [] |
||
297 | $newPath[1] = str_replace('['.$match[2].']', '', $newPath[1]); |
||
298 | |||
299 | $xml = $this->parseXPath($newPath[1]); |
||
300 | |||
301 | } |
||
302 | |||
303 | // FIXME: XPATHXmlGenerator XPATH does not generate any namespaces, |
||
304 | // which DOMDocument cannot cope with. Actually, namespaces should not be necessary here, |
||
305 | // since it is about child elements that are then added to the overall XML. |
||
306 | libxml_use_internal_errors(true); |
||
307 | $docXML = new \DOMDocument(); |
||
308 | $docXML->loadXML($xml); |
||
309 | libxml_use_internal_errors(false); |
||
310 | |||
311 | $domXPath = \EWW\Dpf\Helper\XPath::create($this->xmlData); |
||
312 | |||
313 | // second part nested xpath |
||
314 | if ($match[2] && $secondMatch[2]) { |
||
315 | |||
316 | // import node from nested |
||
317 | // FIXME: XPATHXmlGenerator XPATH does not generate any namespaces, |
||
318 | // which DOMDocument cannot cope with. Actually, namespaces should not be necessary here, |
||
319 | // since it is about child elements that are then added to the overall XML. |
||
320 | libxml_use_internal_errors(true); |
||
321 | $docXMLNested = new \DOMDocument(); |
||
322 | $docXMLNested->loadXML($nestedXml); |
||
323 | libxml_use_internal_errors(false); |
||
324 | |||
325 | $xPath = \EWW\Dpf\Helper\XPath::create($docXML); |
||
326 | |||
327 | $nodeList = $xPath->query($match[1]); |
||
328 | $node = $nodeList->item(0); |
||
329 | |||
330 | $importNode = $docXML->importNode($docXMLNested->getElementsByTagName("mods")->item(0)->firstChild, true); |
||
331 | |||
332 | $node->appendChild($importNode); |
||
333 | } |
||
334 | |||
335 | $domNode = $domXPath->query('/data/' . $path); |
||
336 | $node = $docXML->documentElement; |
||
337 | |||
338 | $nodeAppendModsData = $this->xmlData->importNode($node, true); |
||
339 | $domNode->item($domNode->length - 1)->appendChild($nodeAppendModsData); |
||
340 | } else { |
||
341 | // first xpath doesn't exist |
||
342 | // parse first xpath part |
||
343 | $xml1 = $this->parseXPathWrapped($newPath[0]); |
||
344 | |||
345 | $doc1 = new \DOMDocument(); |
||
346 | $doc1->loadXML($xml1); |
||
347 | |||
348 | $domXPath = \EWW\Dpf\Helper\XPath::create($doc1); |
||
349 | |||
350 | $domNode = $domXPath->query('//' . $path); |
||
351 | |||
352 | // parse second xpath part |
||
353 | $xml2 = $this->parseXPathWrapped($path . $newPath[1]); |
||
354 | |||
355 | // check if xpath [] are nested |
||
356 | $search = '/(\/\w*:?\w*)\[(.*)\]/'; |
||
357 | preg_match($search, $newPath[1], $match); |
||
358 | preg_match($search, $match[2], $secondMatch); |
||
359 | |||
360 | // first part nested xpath |
||
361 | if ($match[2] && $secondMatch[2]) { |
||
362 | $nested = $match[2]; |
||
363 | |||
364 | $nestedXml = $this->parseXPathWrapped($nested); |
||
365 | |||
366 | // object xpath without nested element [] |
||
367 | $newPath[1] = str_replace('['.$match[2].']', '', $newPath[1]); |
||
368 | |||
369 | $xml2 = $this->parseXPathWrapped($path . $newPath[1]); |
||
370 | } |
||
371 | |||
372 | $doc2 = new \DOMDocument(); |
||
373 | $doc2->loadXML($xml2); |
||
374 | |||
375 | $domXPath2 = \EWW\Dpf\Helper\XPath::create($doc2); |
||
376 | |||
377 | // second part nested xpath |
||
378 | if ($match[2] && $secondMatch[2]) { |
||
379 | // import node from nested |
||
380 | $docXMLNested = new \DOMDocument(); |
||
381 | $docXMLNested->loadXML($nestedXml); |
||
382 | |||
383 | $xPath = \EWW\Dpf\Helper\XPath::create($doc2); |
||
384 | $nodeList = $xPath->query('//' . $path . $match[1]); |
||
385 | $node = $nodeList->item(0); |
||
386 | |||
387 | $importNode = $doc2->importNode($docXMLNested->documentElement, true); |
||
388 | |||
389 | $node->appendChild($importNode); |
||
390 | } |
||
391 | $domNode2 = $domXPath2->query('//' . $path)->item(0)->childNodes->item(0); |
||
392 | |||
393 | // merge xml nodes |
||
394 | $nodeToBeAppended = $doc1->importNode($domNode2, true); |
||
395 | $domNode->item(0)->appendChild($nodeToBeAppended); |
||
396 | |||
397 | // add to modsData (merge not required) |
||
398 | // get mods tag |
||
399 | foreach ($this->xmlData->childNodes as $childNode) { |
||
400 | // Skip comments inside the xml. |
||
401 | if ($childNode instanceof \DOMElement) { |
||
402 | $firstChild = $childNode; |
||
403 | break; |
||
404 | } |
||
405 | } |
||
406 | //$firstChild = $this->xmlData->childNodes->item(0); |
||
407 | $firstItem = $doc1->documentElement->firstChild; |
||
408 | $nodeAppendModsData = $this->xmlData->importNode($firstItem, true); |
||
409 | $firstChild->appendChild($nodeAppendModsData); |
||
410 | |||
411 | return $doc1->saveXML(); |
||
412 | } |
||
413 | } else { |
||
414 | // attribute only |
||
415 | $xml = $this->parseXPath($xPath); |
||
416 | |||
417 | // FIXME: XPATHXmlGenerator XPATH does not generate any namespaces, |
||
418 | // which DOMDocument cannot cope with. Actually, namespaces should not be necessary here, |
||
419 | // since it is about child elements that are then added to the overall XML. |
||
420 | libxml_use_internal_errors(true); |
||
421 | $docXML = new \DOMDocument(); |
||
422 | $docXML->loadXML($xml); |
||
423 | libxml_use_internal_errors(false); |
||
424 | |||
425 | $domXPath = \EWW\Dpf\Helper\XPath::create($this->xmlData); |
||
426 | $domNode = $domXPath->query('/data'); |
||
427 | |||
428 | $node = $docXML->documentElement; |
||
429 | |||
430 | $nodeAppendModsData = $this->xmlData->importNode($node, true); |
||
431 | $domNode->item($domNode->length - 1)->appendChild($nodeAppendModsData); |
||
432 | |||
433 | return $docXML->saveXML(); |
||
434 | } |
||
435 | |||
436 | return $this->xmlData->saveXML(); |
||
437 | } |
||
447 |