Completed
Push — master ( 3c32f9...42fff5 )
by Paweł
03:24
created

Config::getPluginIds()   C

Complexity

Conditions 8
Paths 4

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10.9859

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 23
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 32
ccs 16
cts 25
cp 0.64
crap 10.9859
rs 5.3846
1
<?php
2
namespace CodeReview;
3
4
/**
5
 * Simple configuration container handling basic options parsing nad providing convenient methods.
6
 */
7
class Config {
8
9
	const T_PLUGINS_ALL = 0;
10
	const T_PLUGINS_ACTIVE = 1;
11
	const T_PLUGINS_INACTIVE = 2;
12
13
	/**
14
	 * @var array
15
	 */
16
	protected $options = array();
17
18
	/**
19
	 * Selected subdirectory of rootpath to find problems in
20
	 *
21
	 * @var string|null
22
	 */
23
	protected $subPath = null;
24
25
	/**
26
	 * Maximum version to analyze
27
	 *
28
	 * @var string
29
	 */
30
	protected $maxVersion = '';
31
32
	/**
33
	 * Should we include disabled plugins within mod/ directory for analysis
34
	 *
35
	 * @var bool
36
	 */
37
	protected $includeDisabledPlugins = false;
38
39
	/**
40
	 * Should we perform deprecated functions usage search.
41
	 *
42
	 * @var bool
43
	 */
44
	protected $findDeprecatedFunctions = true;
45
46
	/**
47
	 * Should we perform private functions usage search.
48
	 *
49
	 * @var bool
50
	 */
51
	protected $findPrivateFunctions = true;
52
53
	/**
54
	 * Should we attempt to fix bad code? THIS IS DANGEROUS OPTION, CAREFUL!
55
	 *
56
	 * @var bool
57
	 */
58
	protected $fixProblems = false;
59
60
	/**
61
	 * @param array $options
62
	 */
63 3
	public function __construct(array $options = array()) {
64 3
		$this->options = (array)$options;
65 3
	}
66
67
	/**
68
	 * @param $key
69
	 * @return mixed
70
	 */
71
	public function __get($key) {
72
		return $this->options[$key];
73
	}
74
75
	/**
76
	 * @param      $key
77
	 * @param null $default
78
	 * @return null
79
	 */
80 2
	public function getOption($key, $default = null) {
81 2
		return isset($this->options[$key]) ? $this->options[$key] : $default;
82
	}
83
84
	/**
85
	 * @param $key
86
	 * @param $value
87
	 */
88
	public function __set($key, $value) {
89
		$this->options[$key] = $value;
90
	}
91
92
	/**
93
	 * @param $type
94
	 * @return array
95
	 */
96 1
	public function getPluginIds($type) {
97 1
		$pluginsDirs = false;
98
99 1
		$config = \code_review::getConfig();
100
101
		switch ($type) {
102 1
			case self::T_PLUGINS_INACTIVE:
103 1
				$pluginsDirs = $this->getPluginIds(self::T_PLUGINS_ALL);
104 1
				$actives = call_user_func($config['plugins_getter'], 'active');
105 1
				foreach ($actives as $plugin) {
106 1
					if ($plugin instanceof ElggPlugin) {
0 ignored issues
show
Bug introduced by
The class CodeReview\ElggPlugin does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
107
						$pluginsDirs = array_diff($pluginsDirs, array($plugin->getID()));
108
					} else {
109 1
						$pluginsDirs = array_diff($pluginsDirs, array($plugin));
110
					}
111 1
				}
112 1
				break;
113 1
			case self::T_PLUGINS_ACTIVE:
114
				$pluginsDirs = call_user_func($config['plugins_getter'], 'active');
115
				foreach ($pluginsDirs as $key => $plugin) {
116
					if ($plugin instanceof ElggPlugin) {
0 ignored issues
show
Bug introduced by
The class CodeReview\ElggPlugin does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
117
						$pluginsDirs[$key] = $plugin->getID();
118
					}
119
				}
120
				break;
121 1
			case self::T_PLUGINS_ALL:
122 1
				$pluginsDirs = \code_review::getPluginDirsInDir($config['pluginspath']);
123 1
				break;
124
125
		}
126 1
		return $pluginsDirs;
127
	}
128
129
	/*
130
	 * Shorthand methods
131
	 */
132
133
	/**
134
	 * @param array $vars
135
	 */
136
	public function parseInput(array $vars) {
137
138
		//sanitize provided path
139
		$subPath = elgg_extract('subpath', $vars, '/');
140
		$subPath = trim($subPath, '/\\');
141
		$subPath = str_replace('\\', '/', $subPath);
142
		$subPath = str_replace('..', '', $subPath);
143
		$subPath = $subPath . '/';
144
145
		$this->subPath = $subPath;
146
		$this->maxVersion = elgg_extract('version', $vars, '');
147
		$this->includeDisabledPlugins = elgg_extract('include_disabled_plugins', $vars, false);
148
		$this->findDeprecatedFunctions = elgg_extract('find_deprecated_functions', $vars, true);
149
		$this->findPrivateFunctions = elgg_extract('find_private_functions', $vars, true);
150
		$this->fixProblems = elgg_extract('fix_problems', $vars);
151
	}
152
153
	/**
154
	 * @return bool
155
	 */
156
	public function isFixProblemsEnabled() {
157
		return (bool)$this->getOption('fixProblems', false);
158
	}
159
160
	/**
161
	 * @return string
162
	 */
163
	public function getMaxVersion() {
164
		if (!$this->maxVersion) {
165
			//TODO decouple Elgg core dependency
166
			return elgg_get_version(true);
167
		}
168
		return $this->maxVersion;
169
	}
170
171
	/**
172
	 * @return string
173
	 */
174
	public function getSubPath() {
175
		return (string)$this->subPath;
176
	}
177
178
	/**
179
	 * @return bool
180
	 */
181 2
	public function isIncludeDisabledPluginsEnabled() {
182 2
		return (bool)$this->getOption('includeDisabledPlugins', false);
183
	}
184
185
	/**
186
	 * @return bool
187
	 */
188 2
	public function isSkipInactivePluginsEnabled() {
189 2
		return !$this->isIncludeDisabledPluginsEnabled();
190
	}
191
192
	/**
193
	 * @return bool
194
	 */
195
	public function isDeprecatedFunctionsTestEnabled() {
196
		return (bool)$this->getOption('findDeprecatedFunctions', true);
197
	}
198
199
	/**
200
	 * @return bool
201
	 */
202
	public function isPrivateFunctionsTestEnabled() {
203
		return (bool)$this->getOption('findPrivateFunctions', true);
204
	}
205
}