Completed
Push — master ( 42fff5...6429c0 )
by Paweł
07:08
created

Config::isFixProblemsEnabled()   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 $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 null|function
27
	 */
28
	protected $versionGetter = null;
29
30
	/**
31
	 * @param array $options
32
	 */
33 8
	public function __construct(array $options = array(), $versionGetter = null) {
34 8
		$this->options = (array)$options;
35 8
		if (is_callable($versionGetter)) {
36 5
			$this->versionGetter = $versionGetter;
0 ignored issues
show
Documentation Bug introduced by
It seems like $versionGetter of type callable is incompatible with the declared type null|object<CodeReview\function> of property $versionGetter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37 5
		} else {
38
			//TODO possibly further decouple Elgg core dependency
39 3
			$this->versionGetter = 'elgg_get_version';
0 ignored issues
show
Documentation Bug introduced by
It seems like 'elgg_get_version' of type string is incompatible with the declared type null|object<CodeReview\function> of property $versionGetter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
		}
41 8
	}
42
43
	/**
44
	 * @param $key
45
	 * @return mixed
46
	 */
47 5
	public function __get($key) {
48 5
		return isset($this->options[$key]) ? $this->options[$key] : null;
49
	}
50
51
	/**
52
	 * @param      $key
53
	 * @param null $default
54
	 * @return null
55
	 */
56 7
	public function getOption($key, $default = null) {
57 7
		return isset($this->options[$key]) ? $this->options[$key] : $default;
58
	}
59
60
	/**
61
	 * @param $key
62
	 * @param $value
63
	 */
64 4
	public function __set($key, $value) {
65 4
		$this->options[$key] = $value;
66 4
	}
67
68
	/**
69
	 * @param $type
70
	 * @return array
71
	 */
72 1
	public function getPluginIds($type) {
73 1
		$pluginsDirs = false;
74
75 1
		$config = \code_review::getConfig();
76
77
		switch ($type) {
78 1
			case self::T_PLUGINS_INACTIVE:
79 1
				$pluginsDirs = $this->getPluginIds(self::T_PLUGINS_ALL);
80 1
				$actives = call_user_func($config['plugins_getter'], 'active');
81 1
				foreach ($actives as $plugin) {
82 1
					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...
83
						$pluginsDirs = array_diff($pluginsDirs, array($plugin->getID()));
84
					} else {
85 1
						$pluginsDirs = array_diff($pluginsDirs, array($plugin));
86
					}
87 1
				}
88 1
				break;
89 1
			case self::T_PLUGINS_ACTIVE:
90
				$pluginsDirs = call_user_func($config['plugins_getter'], 'active');
91
				foreach ($pluginsDirs as $key => $plugin) {
92
					if ($plugin instanceof \ElggPlugin) {
93
						$pluginsDirs[$key] = $plugin->getID();
94
					}
95
				}
96
				break;
97 1
			case self::T_PLUGINS_ALL:
98 1
				$pluginsDirs = \code_review::getPluginDirsInDir($config['pluginspath']);
99 1
				break;
100
101
		}
102 1
		return $pluginsDirs;
103
	}
104
105
	/*
106
	 * Shorthand methods
107
	 */
108
109
	/**
110
	 * @param       $key
111
	 * @param array $array
112
	 * @param null  $default
113
	 * @param bool  $strict
114
	 * @return null
115
	 *
116
	 * Function is a part of Elgg framework with following license:
117
118
		Copyright (c) 2013. See COPYRIGHT.txt
119
		http://elgg.org/
120
121
		Permission is hereby granted, free of charge, to any person obtaining
122
		a copy of this software and associated documentation files (the
123
		"Software"), to deal in the Software without restriction, including
124
		without limitation the rights to use, copy, modify, merge, publish,
125
		distribute, sublicense, and/or sell copies of the Software, and to
126
		permit persons to whom the Software is furnished to do so, subject to
127
		the following conditions:
128
129
		The above copyright notice and this permission notice shall be
130
		included in all copies or substantial portions of the Software.
131
132
		Except as contained in this notice, the name(s) of the above copyright
133
		holders shall not be used in advertising or otherwise to promote the
134
		sale, use or other dealings in this Software without prior written
135
		authorization.
136
137
		THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
138
		EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
139
		MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
140
		NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
141
		LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
142
		OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
143
		WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
144
	 */
145 4
	private function elggExtract($key, array $array, $default = null, $strict = true) {
146 4
		if (!is_array($array)) {
147
			return $default;
148
		}
149
150 4
		if ($strict) {
151 4
			return (isset($array[$key])) ? $array[$key] : $default;
152
		} else {
153
			return (isset($array[$key]) && !empty($array[$key])) ? $array[$key] : $default;
154
		}
155
	}
156
157
	/**
158
	 * @param array $vars
159
	 */
160 4
	public function parseInput(array $vars) {
161
162
		//sanitize provided path
163 4
		$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...
164 4
		$subPath = trim($subPath, '/\\');
165 4
		$subPath = str_replace('\\', '/', $subPath);
166 4
		$subPath = str_replace('..', '', $subPath);
167 4
		$subPath = $subPath . '/';
168
169 4
		$this->subPath = $subPath;
170 4
		$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...
171 4
		$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...
172 4
		$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...
173 4
		$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...
174 4
		$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...
175 4
	}
176
177
	/**
178
	 * @return bool
179
	 */
180 5
	public function isFixProblemsEnabled() {
181 5
		return (bool)$this->getOption('fixProblems', false);
182
	}
183
184
	/**
185
	 * @return string
186
	 */
187 5
	public function getMaxVersion() {
188 5
		if (!$this->maxVersion) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->maxVersion of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
189 3
			return call_user_func($this->versionGetter, true);
190
		}
191 3
		return $this->maxVersion;
192
	}
193
194
	/**
195
	 * @return string
196
	 */
197 5
	public function getSubPath() {
198 5
		return (string)$this->subPath;
199
	}
200
201
	/**
202
	 * @return bool
203
	 */
204 7
	public function isIncludeDisabledPluginsEnabled() {
205 7
		return (bool)$this->getOption('includeDisabledPlugins', false);
206
	}
207
208
	/**
209
	 * @return bool
210
	 */
211 7
	public function isSkipInactivePluginsEnabled() {
212 7
		return !$this->isIncludeDisabledPluginsEnabled();
213
	}
214
215
	/**
216
	 * @return bool
217
	 */
218 5
	public function isDeprecatedFunctionsTestEnabled() {
219 5
		return (bool)$this->getOption('findDeprecatedFunctions', true);
220
	}
221
222
	/**
223
	 * @return bool
224
	 */
225 5
	public function isPrivateFunctionsTestEnabled() {
226 5
		return (bool)$this->getOption('findPrivateFunctions', true);
227
	}
228
}