InfoChecker::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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