Completed
Push — master ( dac95d...cf9e7d )
by Thomas
02:37
created

SimpleXML   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 85
Duplicated Lines 12.94 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 11
loc 85
ccs 40
cts 40
cp 1
rs 10
wmc 15
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSupported() 0 3 1
B transferTo() 0 21 8
A transferChildTo() 0 12 4
A load() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Load a DOM document from a string or file that was the result of a
4
 * SimpleXMLElement encoded as JSON.
5
 *
6
 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
7
 * @copyright Copyright (c) 2009-2014 Bastian Feder, Thomas Weinert
8
 */
9
10
namespace FluentDOM\Loader\Json {
11
12
  use FluentDOM\Document;
13
  use FluentDOM\Element;
14
  use FluentDOM\Loadable;
15
  use FluentDOM\Loader\Result;
16
  use FluentDOM\Loader\Supports;
17
18
  /**
19
   * Load a DOM document from a string or file that was the result of a
20
   * SimpleXMLElement encoded as JSON.
21
   */
22
  class SimpleXML implements Loadable {
23
24
    use Supports\Json;
25
26
    const XMLNS = 'urn:carica-json-dom.2013';
27
28
    /**
29
     * Maximum recursions
30
     *
31
     * @var int
32
     */
33
    private $_recursions = 100;
34
35
    /**
36
     * @return string[]
37
     */
38 4
    public function getSupported() {
39 4
      return ['text/simplexml', 'text/simplexml+json', 'application/simplexml+json'];
40
    }
41
42
    /**
43
     * Load the json string into an DOMDocument
44
     *
45
     * @param mixed $source
46
     * @param string $contentType
47
     * @param array|\Traversable|Options $options
48
     * @return Document|Result|NULL
49
     */
50 4 View Code Duplication
    public function load($source, $contentType, $options = []) {
51 4
      if (FALSE !== ($json = $this->getJson($source, $contentType, $options))) {
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 50 can also be of type object<Traversable>; however, FluentDOM\Loader\Supports\Json::getJson() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
52 3
        $document = new Document('1.0', 'UTF-8');
53 3
        $document->appendChild(
54 3
          $root = $document->createElementNS(self::XMLNS, 'json:json')
55 3
        );
56 3
        $this->transferTo($root, $json);
57 3
        return $document;
58
      }
59 1
      return NULL;
60
    }
61
62
    /**
63
     * @param \DOMNode|\DOMElement $node
64
     * @param mixed $json
65
     */
66 3
    protected function transferTo(\DOMNode $node, $json) {
67
      /** @var Document $dom */
68 3
      $dom = $node->ownerDocument ?: $node;
69 3
      if ($json instanceof \stdClass) {
70 3
        foreach ($json as $name => $data) {
71 3
          if ($name == '@attributes') {
72 1
            if ($data instanceof \stdClass) {
73 1
              foreach ($data as $attributeName => $attributeValue) {
74 1
                $node->setAttribute($attributeName, $this->getValueAsString($attributeValue));
75 1
              }
76 1
            }
77 1
          } else {
78 3
            $this->transferChildTo($node, $name, $data);
79
          }
80 3
        }
81 3
      } elseif (is_scalar($json)) {
82 2
        $node->appendChild(
83 2
          $dom->createTextNode($this->getValueAsString($json))
84 2
        );
85 2
      }
86 3
    }
87
88
    /**
89
     * @param \DOMNode $node
90
     * @param string $name
91
     * @param mixed $data
92
     * @return array
93
     */
94 3
    protected function transferChildTo(\DOMNode $node, $name, $data) {
95
      /** @var Document $dom */
96 3
      $dom = $node->ownerDocument ?: $node;
97 3
      if (!is_array($data)) {
98 2
        $data = [$data];
99 2
      }
100 3
      foreach ($data as $dataChild) {
101 3
        $child = $node->appendChild($dom->createElement($name));
102 3
        $this->transferTo($child, $dataChild);
103 3
      }
104 3
      return $data;
105
    }
106
  }
107
}