InfoParser   B
last analyzed

Complexity

Total Complexity 46

Size/Duplication

Total Lines 158
Duplicated Lines 15.19 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 24
loc 158
rs 8.72
c 0
b 0
f 0
wmc 46
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
F parse() 12 97 35
B xmlToArray() 12 47 11

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like InfoParser 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 InfoParser, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @author Andreas Fischer <[email protected]>
4
 * @author Christoph Wurst <[email protected]>
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @copyright Copyright (c) 2018, ownCloud GmbH
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OC\App;
27
28
use InvalidArgumentException;
29
use OCP\App\AppNotFoundException;
30
31
class InfoParser {
32
33
	/**
34
	 * @param string $file the xml file to be loaded
35
	 * @return array
36
	 * @throws AppNotFoundException if file does not exist
37
	 * @throws InvalidArgumentException on malformed XML
38
	 */
39
	public function parse($file) {
40
		if (!\is_file($file)) {
41
			throw new AppNotFoundException(
42
				\sprintf('%s does not exist', $file)
43
			);
44
		}
45
46
		\libxml_use_internal_errors(true);
47
		$loadEntities = \libxml_disable_entity_loader(false);
48
		$xml = \simplexml_load_file($file);
49
50
		\libxml_disable_entity_loader($loadEntities);
51
		if ($xml === false) {
52
			\libxml_clear_errors();
53
			throw new InvalidArgumentException('Invalid XML');
54
		}
55
		$array = $this->xmlToArray($xml);
56
57
		if (!\is_array($array)) {
58
			throw new InvalidArgumentException('Could not convert XML to array');
59
		}
60
61
		if (!\array_key_exists('info', $array)) {
62
			$array['info'] = [];
63
		}
64
		if (!\array_key_exists('remote', $array)) {
65
			$array['remote'] = [];
66
		}
67
		if (!\array_key_exists('public', $array)) {
68
			$array['public'] = [];
69
		}
70
		if (!\array_key_exists('types', $array)) {
71
			$array['types'] = [];
72
		}
73
		if (!\array_key_exists('repair-steps', $array)) {
74
			$array['repair-steps'] = [];
75
		}
76
		if (!\array_key_exists('install', $array['repair-steps'])) {
77
			$array['repair-steps']['install'] = [];
78
		}
79
		if (!\array_key_exists('pre-migration', $array['repair-steps'])) {
80
			$array['repair-steps']['pre-migration'] = [];
81
		}
82
		if (!\array_key_exists('post-migration', $array['repair-steps'])) {
83
			$array['repair-steps']['post-migration'] = [];
84
		}
85
		if (!\array_key_exists('live-migration', $array['repair-steps'])) {
86
			$array['repair-steps']['live-migration'] = [];
87
		}
88
		if (!\array_key_exists('uninstall', $array['repair-steps'])) {
89
			$array['repair-steps']['uninstall'] = [];
90
		}
91
		if (!\array_key_exists('background-jobs', $array)) {
92
			$array['background-jobs'] = [];
93
		}
94
		if (!\array_key_exists('two-factor-providers', $array)) {
95
			$array['two-factor-providers'] = [];
96
		}
97
		if (!\array_key_exists('commands', $array)) {
98
			$array['commands'] = [];
99
		}
100
101
		if (\array_key_exists('types', $array)) {
102
			if (\is_array($array['types'])) {
103
				foreach ($array['types'] as $type => $v) {
104
					unset($array['types'][$type]);
105
					if (\is_string($type)) {
106
						$array['types'][] = $type;
107
					}
108
				}
109
			} else {
110
				$array['types'] = [];
111
			}
112
		}
113
		if (isset($array['repair-steps']['install']['step']) && \is_array($array['repair-steps']['install']['step'])) {
114
			$array['repair-steps']['install'] = $array['repair-steps']['install']['step'];
115
		}
116 View Code Duplication
		if (isset($array['repair-steps']['pre-migration']['step']) && \is_array($array['repair-steps']['pre-migration']['step'])) {
0 ignored issues
show
Duplication introduced by
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.

Loading history...
117
			$array['repair-steps']['pre-migration'] = $array['repair-steps']['pre-migration']['step'];
118
		}
119 View Code Duplication
		if (isset($array['repair-steps']['post-migration']['step']) && \is_array($array['repair-steps']['post-migration']['step'])) {
0 ignored issues
show
Duplication introduced by
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.

Loading history...
120
			$array['repair-steps']['post-migration'] = $array['repair-steps']['post-migration']['step'];
121
		}
122
		if (isset($array['repair-steps']['live-migration']['step']) && \is_array($array['repair-steps']['live-migration']['step'])) {
123
			$array['repair-steps']['live-migration'] = $array['repair-steps']['live-migration']['step'];
124
		}
125
		if (isset($array['repair-steps']['uninstall']['step']) && \is_array($array['repair-steps']['uninstall']['step'])) {
126
			$array['repair-steps']['uninstall'] = $array['repair-steps']['uninstall']['step'];
127
		}
128 View Code Duplication
		if (isset($array['background-jobs']['job']) && \is_array($array['background-jobs']['job'])) {
0 ignored issues
show
Duplication introduced by
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.

Loading history...
129
			$array['background-jobs'] = $array['background-jobs']['job'];
130
		}
131 View Code Duplication
		if (isset($array['commands']['command']) && \is_array($array['commands']['command'])) {
0 ignored issues
show
Duplication introduced by
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.

Loading history...
132
			$array['commands'] = $array['commands']['command'];
133
		}
134
		return $array;
135
	}
136
137
	/**
138
	 * @param \SimpleXMLElement $xml
139
	 * @return array|string
140
	 */
141
	protected function xmlToArray($xml) {
142
		if (!$xml->children()) {
143
			return (string)$xml;
144
		}
145
146
		$array = [];
147
		foreach ($xml->children() as $element => $node) {
148
			$totalElement = \count($xml->{$element});
149
150
			if (!isset($array[$element])) {
151
				$array[$element] = $totalElement > 1 ? [] : "";
152
			}
153
			/** @var \SimpleXMLElement $node */
154
			// Has attributes
155
			if ($attributes = $node->attributes()) {
156
				$data = [
157
					'@attributes' => [],
158
				];
159
				if (!\count($node->children())) {
160
					$value = (string)$node;
161
					if (!empty($value)) {
162
						$data['@value'] = (string)$node;
163
					}
164
				} else {
165
					$data = \array_merge($data, $this->xmlToArray($node));
166
				}
167
				foreach ($attributes as $attr => $value) {
168
					$data['@attributes'][$attr] = (string)$value;
169
				}
170
171 View Code Duplication
				if ($totalElement > 1) {
0 ignored issues
show
Duplication introduced by
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.

Loading history...
172
					$array[$element][] = $data;
173
				} else {
174
					$array[$element] = $data;
175
				}
176
				// Just a value
177 View Code Duplication
			} else {
0 ignored issues
show
Duplication introduced by
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.

Loading history...
178
				if ($totalElement > 1) {
179
					$array[$element][] = $this->xmlToArray($node);
180
				} else {
181
					$array[$element] = $this->xmlToArray($node);
182
				}
183
			}
184
		}
185
186
		return $array;
187
	}
188
}
189