Completed
Push — master ( 6429c0...141c4a )
by Paweł
03:42
created

Config::isIncludeDisabledPluginsEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
namespace CodeReview;
3
4
/**
5
 * Simple configuration container handling basic options parsing nad providing convenient methods.
6
 *
7
 * @property string|null $subPath
8
 * @property string|null $maxVersion
9
 * @property bool $includeDisabledPlugins
10
 * @property bool $findDeprecatedFunctions
11
 * @property bool $findPrivateFunctions
12
 * @property bool $fixProblems
13
 */
14
class Config {
15
16
	const T_PLUGINS_ALL = 0;
17
	const T_PLUGINS_ACTIVE = 1;
18
	const T_PLUGINS_INACTIVE = 2;
19
20
	/**
21
	 * @var array
22
	 */
23
	protected $options = array();
24
25
	/**
26
	 * @var callable
27
	 */
28
	protected $versionGetter = null;
29
30
	/**
31
	 * @param array $options
32
	 */
33 19
	public function __construct(array $options = array(), $versionGetter = null) {
34 19
		$this->options = (array)$options;
35
36 19
		if (is_callable($versionGetter)) {
37 16
			$this->versionGetter = $versionGetter;
38 16
		} else {
39
			//TODO possibly further decouple Elgg core dependency
40 3
			$this->versionGetter = 'elgg_get_version';
41
		}
42 19
	}
43
44
	/**
45
	 * @param $key
46
	 * @return mixed
47
	 */
48 14
	public function __get($key) {
49 14
		return isset($this->options[$key]) ? $this->options[$key] : null;
50
	}
51
52
	/**
53
	 * @param      $key
54
	 * @param null $default
55
	 * @return null
56
	 */
57 16
	public function getOption($key, $default = null) {
58 16
		return isset($this->options[$key]) ? $this->options[$key] : $default;
59
	}
60
61
	/**
62
	 * @param $key
63
	 * @param $value
64
	 */
65 13
	public function __set($key, $value) {
66 13
		$this->options[$key] = $value;
67 13
	}
68
69
	/**
70
	 * @param $type
71
	 * @return array
72
	 */
73 5
	public function getPluginIds($type) {
74 5
		$pluginsDirs = false;
75
76 5
		$config = \code_review::getConfig();
77
78
		switch ($type) {
79 5
			case self::T_PLUGINS_INACTIVE:
80 5
				$pluginsDirs = $this->getPluginIds(self::T_PLUGINS_ALL);
81 5
				$actives = call_user_func($config['plugins_getter'], 'active');
82 5
				foreach ($actives as $plugin) {
83 5
					if ($plugin instanceof \ElggPlugin) {
1 ignored issue
show
Bug introduced by
The class 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...
84
						$pluginsDirs = array_diff($pluginsDirs, array($plugin->getID()));
85
					} else {
86 5
						$pluginsDirs = array_diff($pluginsDirs, array($plugin));
87
					}
88 5
				}
89 5
				break;
90 5
			case self::T_PLUGINS_ACTIVE:
91 2
				$pluginsDirs = call_user_func($config['plugins_getter'], 'active');
92 2
				foreach ($pluginsDirs as $key => $plugin) {
93 2
					if ($plugin instanceof \ElggPlugin) {
94
						$pluginsDirs[$key] = $plugin->getID();
95
					}
96 2
				}
97 2
				break;
98 5
			case self::T_PLUGINS_ALL:
99 5
				$pluginsDirs = \code_review::getPluginDirsInDir($config['pluginspath']);
100 5
				break;
101
102
		}
103 5
		return $pluginsDirs;
104
	}
105
106
	/*
107
	 * Shorthand methods
108
	 */
109
110
	/**
111
	 * @param       $key
112
	 * @param array $array
113
	 * @param null  $default
114
	 * @param bool  $strict
115
	 * @return null
116
	 *
117
	 * Function is a part of Elgg framework with following license:
118
	 *
119
	 * Copyright (c) 2013. See COPYRIGHT.txt
120
	 * http://elgg.org/
121
	 *
122
	 * Permission is hereby granted, free of charge, to any person obtaining
123
	 * a copy of this software and associated documentation files (the
124
	 * "Software"), to deal in the Software without restriction, including
125
	 * without limitation the rights to use, copy, modify, merge, publish,
126
	 * distribute, sublicense, and/or sell copies of the Software, and to
127
	 * permit persons to whom the Software is furnished to do so, subject to
128
	 * the following conditions:
129
	 *
130
	 * The above copyright notice and this permission notice shall be
131
	 * included in all copies or substantial portions of the Software.
132
	 *
133
	 * Except as contained in this notice, the name(s) of the above copyright
134
	 * holders shall not be used in advertising or otherwise to promote the
135
	 * sale, use or other dealings in this Software without prior written
136
	 * authorization.
137
	 *
138
	 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
139
	 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
140
	 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
141
	 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
142
	 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
143
	 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
144
	 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
145
	 */
146 13
	private function elggExtract($key, array $array, $default = null, $strict = true) {
147 13
		if (!is_array($array)) {
148
			return $default;
149
		}
150
151 13
		if ($strict) {
152 13
			return (isset($array[$key])) ? $array[$key] : $default;
153
		} else {
154
			return (isset($array[$key]) && !empty($array[$key])) ? $array[$key] : $default;
155
		}
156
	}
157
158
	/**
159
	 * @param array $vars
160
	 */
161 13
	public function parseInput(array $vars) {
162
163
		//sanitize provided path
164 13
		$subPath = $this->elggExtract('subpath', $vars, '/');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $subPath is correct as $this->elggExtract('subpath', $vars, '/') (which targets CodeReview\Config::elggExtract()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
165 13
		$subPath = trim($subPath, '/\\');
166 13
		$subPath = str_replace('\\', '/', $subPath);
167 13
		$subPath = str_replace('..', '', $subPath);
168 13
		$subPath = $subPath . '/';
169
170 13
		$this->subPath = $subPath;
171 13
		$this->maxVersion = $this->elggExtract('version', $vars, '');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->maxVersion is correct as $this->elggExtract('version', $vars, '') (which targets CodeReview\Config::elggExtract()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
172 13
		$this->includeDisabledPlugins = $this->elggExtract('include_disabled_plugins', $vars, false);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->includeDisabledPlugins is correct as $this->elggExtract('incl...plugins', $vars, false) (which targets CodeReview\Config::elggExtract()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
173 13
		$this->findDeprecatedFunctions = $this->elggExtract('find_deprecated_functions', $vars, true);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->findDeprecatedFunctions is correct as $this->elggExtract('find...unctions', $vars, true) (which targets CodeReview\Config::elggExtract()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
174 13
		$this->findPrivateFunctions = $this->elggExtract('find_private_functions', $vars, true);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->findPrivateFunctions is correct as $this->elggExtract('find...unctions', $vars, true) (which targets CodeReview\Config::elggExtract()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
175 13
		$this->fixProblems = $this->elggExtract('fix_problems', $vars, false);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->fixProblems is correct as $this->elggExtract('fix_problems', $vars, false) (which targets CodeReview\Config::elggExtract()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
176 13
	}
177
178
	/**
179
	 * @return bool
180
	 */
181 14
	public function isFixProblemsEnabled() {
182 14
		return (bool)$this->getOption('fixProblems', false);
183
	}
184
185
	/**
186
	 * @return string
187
	 */
188 14
	public function getMaxVersion() {
189 14
		if ($this->maxVersion === '' || $this->maxVersion === null) {
190 3
			return call_user_func($this->versionGetter, true);
191
		}
192 12
		return $this->maxVersion;
193
	}
194
195
	/**
196
	 * @return string
197
	 */
198 14
	public function getSubPath() {
199 14
		return (string)$this->subPath;
200
	}
201
202
	/**
203
	 * @return bool
204
	 */
205 15
	public function isIncludeDisabledPluginsEnabled() {
206 15
		return (bool)$this->getOption('includeDisabledPlugins', false);
207
	}
208
209
	/**
210
	 * @return bool
211
	 */
212 15
	public function isSkipInactivePluginsEnabled() {
213 15
		return !$this->isIncludeDisabledPluginsEnabled();
214
	}
215
216
	/**
217
	 * @return bool
218
	 */
219 13
	public function isDeprecatedFunctionsTestEnabled() {
220 13
		return (bool)$this->getOption('findDeprecatedFunctions', true);
221
	}
222
223
	/**
224
	 * @return bool
225
	 */
226 13
	public function isPrivateFunctionsTestEnabled() {
227 13
		return (bool)$this->getOption('findPrivateFunctions', true);
228
	}
229
}