Completed
Pull Request — master (#2092)
by Joas
08:22 queued 01:00
created

InfoParser::parse()   F

Complexity

Conditions 48
Paths > 20000

Size

Total Lines 127
Code Lines 82

Duplication

Lines 21
Ratio 16.54 %

Importance

Changes 0
Metric Value
cc 48
eloc 82
nc 1610612742
nop 1
dl 21
loc 127
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 * @copyright Copyright (c) 2016, Lukas Reschke <[email protected]>
5
 *
6
 * @author Andreas Fischer <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 * @author Lukas Reschke <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Thomas Müller <[email protected]>
11
 *
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
28
namespace OC\App;
29
30
use OCP\ICache;
31
32
class InfoParser {
33
	/** @var \OCP\ICache|null */
34
	private $cache;
35
36
	/**
37
	 * @param ICache|null $cache
38
	 */
39
	public function __construct(ICache $cache = null) {
40
		$this->cache = $cache;
41
	}
42
43
	/**
44
	 * @param string $file the xml file to be loaded
45
	 * @return null|array where null is an indicator for an error
46
	 */
47
	public function parse($file) {
48
		if (!file_exists($file)) {
49
			return null;
50
		}
51
52
		if(!is_null($this->cache)) {
53
			$fileCacheKey = $file . filemtime($file);
54
			if ($cachedValue = $this->cache->get($fileCacheKey)) {
55
				return json_decode($cachedValue, true);
56
			}
57
		}
58
59
		libxml_use_internal_errors(true);
60
		$loadEntities = libxml_disable_entity_loader(false);
61
		$xml = simplexml_load_file($file);
62
63
		libxml_disable_entity_loader($loadEntities);
64
		if ($xml === false) {
65
			libxml_clear_errors();
66
			return null;
67
		}
68
		$array = $this->xmlToArray($xml);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->xmlToArray($xml); of type string|array adds the type string to the return on line 172 which is incompatible with the return type documented by OC\App\InfoParser::parse of type null|array.
Loading history...
69
70
		if (is_null($array)) {
71
			return null;
72
		}
73
74
		if (!array_key_exists('info', $array)) {
75
			$array['info'] = [];
76
		}
77
		if (!array_key_exists('remote', $array)) {
78
			$array['remote'] = [];
79
		}
80
		if (!array_key_exists('public', $array)) {
81
			$array['public'] = [];
82
		}
83
		if (!array_key_exists('types', $array)) {
84
			$array['types'] = [];
85
		}
86
		if (!array_key_exists('repair-steps', $array)) {
87
			$array['repair-steps'] = [];
88
		}
89
		if (!array_key_exists('install', $array['repair-steps'])) {
90
			$array['repair-steps']['install'] = [];
91
		}
92
		if (!array_key_exists('pre-migration', $array['repair-steps'])) {
93
			$array['repair-steps']['pre-migration'] = [];
94
		}
95
		if (!array_key_exists('post-migration', $array['repair-steps'])) {
96
			$array['repair-steps']['post-migration'] = [];
97
		}
98
		if (!array_key_exists('live-migration', $array['repair-steps'])) {
99
			$array['repair-steps']['live-migration'] = [];
100
		}
101
		if (!array_key_exists('uninstall', $array['repair-steps'])) {
102
			$array['repair-steps']['uninstall'] = [];
103
		}
104
		if (!array_key_exists('background-jobs', $array)) {
105
			$array['background-jobs'] = [];
106
		}
107
		if (!array_key_exists('two-factor-providers', $array)) {
108
			$array['two-factor-providers'] = [];
109
		}
110
		if (!array_key_exists('commands', $array)) {
111
			$array['commands'] = [];
112
		}
113
		if (!array_key_exists('activity', $array)) {
114
			$array['activity'] = [];
115
		}
116
		if (!array_key_exists('filters', $array['activity'])) {
117
			$array['activity']['filters'] = [];
118
		}
119
		if (!array_key_exists('settings', $array['activity'])) {
120
			$array['activity']['settings'] = [];
121
		}
122
		if (!array_key_exists('providers', $array['activity'])) {
123
			$array['activity']['providers'] = [];
124
		}
125
126
		if (array_key_exists('types', $array)) {
127
			if (is_array($array['types'])) {
128
				foreach ($array['types'] as $type => $v) {
129
					unset($array['types'][$type]);
130
					if (is_string($type)) {
131
						$array['types'][] = $type;
132
					}
133
				}
134
			} else {
135
				$array['types'] = [];
136
			}
137
		}
138 View Code Duplication
		if (isset($array['repair-steps']['install']['step']) && is_array($array['repair-steps']['install']['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...
139
			$array['repair-steps']['install'] = $array['repair-steps']['install']['step'];
140
		}
141 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...
142
			$array['repair-steps']['pre-migration'] = $array['repair-steps']['pre-migration']['step'];
143
		}
144 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...
145
			$array['repair-steps']['post-migration'] = $array['repair-steps']['post-migration']['step'];
146
		}
147 View Code Duplication
		if (isset($array['repair-steps']['live-migration']['step']) && is_array($array['repair-steps']['live-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...
148
			$array['repair-steps']['live-migration'] = $array['repair-steps']['live-migration']['step'];
149
		}
150 View Code Duplication
		if (isset($array['repair-steps']['uninstall']['step']) && is_array($array['repair-steps']['uninstall']['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...
151
			$array['repair-steps']['uninstall'] = $array['repair-steps']['uninstall']['step'];
152
		}
153 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...
154
			$array['background-jobs'] = $array['background-jobs']['job'];
155
		}
156 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...
157
			$array['commands'] = $array['commands']['command'];
158
		}
159
		if (isset($array['activity']['filters']['filter']) && is_array($array['activity']['filters']['filter'])) {
160
			$array['activity']['filters'] = $array['activity']['filters']['filter'];
161
		}
162
		if (isset($array['activity']['settings']['setting']) && is_array($array['activity']['settings']['setting'])) {
163
			$array['activity']['settings'] = $array['activity']['settings']['setting'];
164
		}
165
		if (isset($array['activity']['providers']['provider']) && is_array($array['activity']['providers']['provider'])) {
166
			$array['activity']['providers'] = $array['activity']['providers']['provider'];
167
		}
168
169
		if(!is_null($this->cache)) {
170
			$this->cache->set($fileCacheKey, json_encode($array));
0 ignored issues
show
Bug introduced by
The variable $fileCacheKey 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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
171
		}
172
		return $array;
173
	}
174
175
	/**
176
	 * @param \SimpleXMLElement $xml
177
	 * @return array
178
	 */
179
	function xmlToArray($xml) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
180
		if (!$xml->children()) {
181
			return (string)$xml;
182
		}
183
184
		$array = [];
185
		foreach ($xml->children() as $element => $node) {
186
			$totalElement = count($xml->{$element});
187
188
			if (!isset($array[$element])) {
189
				$array[$element] = $totalElement > 1 ? [] : "";
190
			}
191
			/** @var \SimpleXMLElement $node */
192
			// Has attributes
193
			if ($attributes = $node->attributes()) {
194
				$data = [
195
					'@attributes' => [],
196
				];
197
				if (!count($node->children())){
198
					$value = (string)$node;
199
					if (!empty($value)) {
200
						$data['@value'] = (string)$node;
201
					}
202
				} else {
203
					$data = array_merge($data, $this->xmlToArray($node));
204
				}
205
				foreach ($attributes as $attr => $value) {
206
					$data['@attributes'][$attr] = (string)$value;
207
				}
208
209 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...
210
					$array[$element][] = $data;
211
				} else {
212
					$array[$element] = $data;
213
				}
214
				// Just a value
215 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...
216
				if ($totalElement > 1) {
217
					$array[$element][] = $this->xmlToArray($node);
218
				} else {
219
					$array[$element] = $this->xmlToArray($node);
220
				}
221
			}
222
		}
223
224
		return $array;
225
	}
226
}
227