Total Complexity | 44 |
Total Lines | 212 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Complex classes like FromArrayBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FromArrayBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class FromArrayBuilder |
||
16 | { |
||
17 | |||
18 | protected $input; |
||
19 | |||
20 | /** |
||
21 | * @var DOMDocument |
||
22 | */ |
||
23 | protected $dom; |
||
24 | protected $options = []; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * @param $input |
||
29 | * @param array $options |
||
30 | * |
||
31 | * @return DOMDocument|SimpleXMLElement |
||
32 | */ |
||
33 | public static function build($input, array $options = []) |
||
34 | { |
||
35 | if (is_object($input) && method_exists($input, 'toArray') && is_callable([$input, 'toArray'])) { |
||
36 | $input = $input->toArray(); |
||
37 | } |
||
38 | if (!is_array($input) || count($input) !== 1) { |
||
39 | throw new XmlException('Invalid input.'); |
||
40 | } |
||
41 | $key = key($input); |
||
42 | if (is_int($key)) { |
||
43 | throw new XmlException('The key of input must be alphanumeric'); |
||
44 | } |
||
45 | |||
46 | return (new static($input, $options))->buildXml(); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * FromArrayBuilder constructor. |
||
51 | * |
||
52 | * @param $input |
||
53 | * @param array $options |
||
54 | */ |
||
55 | protected function __construct($input, array $options = []) |
||
73 | } |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return SimpleXMLElement|DOMDocument |
||
78 | */ |
||
79 | protected function buildXml() |
||
80 | { |
||
81 | $this->addData($this->dom, $this->dom, $this->input, $this->options['format']); |
||
82 | |||
83 | $return = strtolower($this->options['return']); |
||
84 | if ($return === 'simplexml' || $return === 'simplexmlelement') { |
||
85 | return new SimpleXMLElement($this->dom->saveXML()); |
||
86 | } |
||
87 | |||
88 | return $this->dom; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Recursive method to create childs from array |
||
93 | * |
||
94 | * @param \DOMDocument $dom Handler to DOMDocument |
||
95 | * @param \DOMDocument|\DOMElement $node Handler to DOMElement (child) |
||
96 | * @param array $data Array of data to append to the $node. |
||
97 | * @param string $format Either 'attributes' or 'tags'. This determines where nested keys go. |
||
98 | * |
||
99 | * @return void |
||
100 | * @throws XmlException |
||
101 | */ |
||
102 | protected function addData(DOMDocument $dom, $node, &$data, $format): void |
||
112 | } |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param DOMDocument $dom |
||
117 | * @param $node |
||
118 | * @param $key |
||
119 | * @param $value |
||
120 | * @param $format |
||
121 | */ |
||
122 | protected function addDataItem(DOMDocument $dom, $node, $key, $value, $format): void |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Helper to _fromArray(). It will create childs of arrays |
||
179 | * |
||
180 | * @param array $data Array with information to create childs |
||
181 | * |
||
182 | * @return void |
||
183 | */ |
||
184 | protected function createChild(array $data): void |
||
227 | } |
||
228 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths