Completed
Push — master ( ea64cb...6a06df )
by Morris
13:13
created

InfoChecker::analyse()   D

Complexity

Conditions 14
Paths 265

Size

Total Lines 70
Code Lines 42

Duplication

Lines 14
Ratio 20 %

Importance

Changes 0
Metric Value
cc 14
eloc 42
nc 265
nop 1
dl 14
loc 70
rs 4.1959
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
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OC\App\CodeChecker;
25
26
use OC\App\InfoParser;
27
use OC\Hooks\BasicEmitter;
28
29
class InfoChecker extends BasicEmitter {
30
31
	/** @var InfoParser */
32
	private $infoParser;
33
34
	private $mandatoryFields = [
35
		'author',
36
		'description',
37
		'dependencies',
38
		'id',
39
		'licence',
40
		'name',
41
		'version',
42
	];
43
	private $optionalFields = [
44
		'bugs',
45
		'category',
46
		'default_enable',
47
		'documentation',
48
		'namespace',
49
		'ocsid',
50
		'public',
51
		'remote',
52
		'repository',
53
		'types',
54
		'website',
55
	];
56
	private $deprecatedFields = [
57
		'info',
58
		'require',
59
		'requiremax',
60
		'requiremin',
61
		'shipped',
62
		'standalone',
63
	];
64
65
	public function __construct(InfoParser $infoParser) {
66
		$this->infoParser = $infoParser;
67
	}
68
69
	/**
70
	 * @param string $appId
71
	 * @return array
72
	 */
73
	public function analyse($appId) {
74
		$appPath = \OC_App::getAppPath($appId);
75
		if ($appPath === false) {
76
			throw new \RuntimeException("No app with given id <$appId> known.");
77
		}
78
79
		$errors = [];
80
81
		$info = $this->infoParser->parse($appPath . '/appinfo/info.xml');
82
83 View Code Duplication
		if (!isset($info['dependencies']['nextcloud']['@attributes']['min-version'])) {
84
			$errors[] = [
85
				'type' => 'missingRequirement',
86
				'field' => 'min',
87
			];
88
			$this->emit('InfoChecker', 'missingRequirement', ['min']);
89
		}
90
91 View Code Duplication
		if (!isset($info['dependencies']['nextcloud']['@attributes']['max-version'])) {
92
			$errors[] = [
93
				'type' => 'missingRequirement',
94
				'field' => 'max',
95
			];
96
			$this->emit('InfoChecker', 'missingRequirement', ['max']);
97
		}
98
99
		foreach ($info as $key => $value) {
100
			if(is_array($value)) {
101
				$value = json_encode($value);
102
			}
103
			if (in_array($key, $this->mandatoryFields)) {
104
				$this->emit('InfoChecker', 'mandatoryFieldFound', [$key, $value]);
105
				continue;
106
			}
107
108
			if (in_array($key, $this->optionalFields)) {
109
				$this->emit('InfoChecker', 'optionalFieldFound', [$key, $value]);
110
				continue;
111
			}
112
113
			if (in_array($key, $this->deprecatedFields)) {
114
				// skip empty arrays - empty arrays for remote and public are always added
115
				if($value === '[]' && in_array($key, ['public', 'remote', 'info'])) {
116
					continue;
117
				}
118
				$this->emit('InfoChecker', 'deprecatedFieldFound', [$key, $value]);
119
				continue;
120
			}
121
122
			$this->emit('InfoChecker', 'unusedFieldFound', [$key, $value]);
123
		}
124
125
		foreach ($this->mandatoryFields as $key) {
126
			if(!isset($info[$key])) {
127
				$this->emit('InfoChecker', 'mandatoryFieldMissing', [$key]);
128
				$errors[] = [
129
					'type' => 'mandatoryFieldMissing',
130
					'field' => $key,
131
				];
132
			}
133
		}
134
135
		$versionFile = $appPath . '/appinfo/version';
136
		if (is_file($versionFile)) {
137
			$version = trim(file_get_contents($versionFile));
138
			$this->emit('InfoChecker', 'migrateVersion', [$version]);
139
		}
140
141
		return $errors;
142
	}
143
}
144