SettingsArray   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 80
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSetting() 0 33 5
A setSetting() 0 3 1
A hasSetting() 0 3 1
1
<?php
2
3
namespace Wikibase\Lib;
4
5
use ArrayObject;
6
use Closure;
7
use MediaWiki\Logger\LoggerFactory;
8
use OutOfBoundsException;
9
use Psr\Log\LoggerInterface;
10
11
/**
12
 * Class representing a collection of settings.
13
 *
14
 * @note: settings can be dynamic: if a setting is given as a closure, the closure will be
15
 *        called to get the actual setting value the first time this setting is retrieved
16
 *        using getSetting(). The closure is called with the SettingsArray as the only argument.
17
 *
18
 * This program is free software; you can redistribute it and/or modify
19
 * it under the terms of the GNU General Public License as published by
20
 * the Free Software Foundation; either version 2 of the License, or
21
 * (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU General Public License
29
 * along with this program; if not, write to the Free Software
30
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
31
 *
32
 * @license GPL-2.0-or-later
33
 * @author Jeroen De Dauw < [email protected] >
34
 * @author Daniel Kinzler
35
 */
36
class SettingsArray extends ArrayObject {
37
38
	/**
39
	 * @var LoggerInterface
40
	 */
41
	private $logger;
42
43
	public function __construct( array $input = [] ) {
44
		parent::__construct( $input );
45
46
		// TODO: Inject?!
47
		$this->logger = LoggerFactory::getInstance( 'Wikibase' );
48
	}
49
50
	/**
51
	 * Gets the value of the specified setting.
52
	 *
53
	 * @param string $settingName
54
	 *
55
	 * @throws OutOfBoundsException
56
	 * @return mixed
57
	 */
58
	public function getSetting( $settingName ) {
59
		if ( !$this->offsetExists( $settingName ) && !property_exists( $this, $settingName ) ) {
60
			throw new OutOfBoundsException( 'Attempt to get non-existing setting "' . $settingName . '"' );
61
		}
62
63
		$value = $this[$settingName];
64
65
		// Allow closures to be used for deferred evaluation
66
		// of "magic" (dynamic) defaults.
67
		if ( $value instanceof Closure ) {
68
			$value = $value( $this );
69
70
			if ( is_object( $value ) ) {
71
				$logValue = 'instance of ' . get_class( $value );
72
			} else {
73
				$logValue = var_export( $value, true );
74
			}
75
76
			$this->logger->debug(
77
				'{method}: setting {settingName} was given as a closure, resolve it to {logValue}',
78
				[
79
					'method' => __METHOD__,
80
					'settingName' => $settingName,
81
					'logValue' => $logValue,
82
				]
83
			);
84
85
			// only eval once, then remember the value
86
			$this->setSetting( $settingName, $value );
0 ignored issues
show
Unused Code introduced by
The call to the method Wikibase\Lib\SettingsArray::setSetting() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
87
		}
88
89
		return $value;
90
	}
91
92
	/**
93
	 * Sets the value of the specified setting.
94
	 *
95
	 * @param string $settingName
96
	 * @param mixed $settingValue The desired value. If this is a closure, the closure will be
97
	 *        called to get the actual setting value the first time this setting is retrieved
98
	 *        using getSetting(). The closure is called with this SettingsArray as the only argument.
99
	 */
100
	public function setSetting( $settingName, $settingValue ) {
101
		$this[$settingName] = $settingValue;
102
	}
103
104
	/**
105
	 * Returns if the specified settings is set or not.
106
	 *
107
	 * @param string $settingName
108
	 *
109
	 * @return boolean
110
	 */
111
	public function hasSetting( $settingName ) {
112
		return $this->offsetExists( $settingName );
113
	}
114
115
}
116