Completed
Push — master ( e11051...f173c0 )
by mw
16s queued 11s
created

Options::newFromGlobals()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
nop 0
crap 2
1
<?php
2
3
namespace SUC;
4
5
use InvalidArgumentException;
6
7
/**
8
 * @license GNU GPL v2+
9
 * @since 1.0
10
 *
11
 * @author mwjames
12
 */
13
class Options {
14
15
	/**
16
	 * @var array
17
	 */
18
	private $options = array();
19
20
	/**
21
	 * @since 1.0
22
	 *
23
	 * @param array $options
24
	 */
25 9
	public function __construct( array $options = array() ) {
26 9
		$this->options = $options;
27 9
	}
28
29
	/**
30
	 * @since 1.0
31
	 *
32
	 * @return self
33
	 */
34 6
	public static function newFromGlobals() {
0 ignored issues
show
Coding Style introduced by
newFromGlobals uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
35 6
		$GLOBALS['sucgCachePrefix'] = $GLOBALS['wgCachePrefix'] === false ? wfWikiID() : $GLOBALS['wgCachePrefix'];
36
37
		$configuration = array(
38 6
			'tooltipRequestCacheTTL'       => $GLOBALS['sucgTooltipRequestCacheLifetime'],
39 6
			'cachePrefix'                  => $GLOBALS['sucgCachePrefix'],
40 6
			'enabledNamespaceWithTemplate' => $GLOBALS['sucgEnabledNamespaceWithTemplate'],
41 6
			'enabledForAnonUsers'          => $GLOBALS['sucgEnabledForAnonUser'],
42 6
			'backendParserCacheLifetime'   => $GLOBALS['sucgBackendParserCacheLifetime'],
43 6
			'backendParserCacheType'       => $GLOBALS['sucgBackendParserCacheType']
44 6
		);
45
46 6
		return new self( $configuration );
47
	}
48
49
	/**
50
	 * @since 1.0
51
	 *
52
	 * @param string $key
53
	 * @param mixed $value
54
	 */
55 1
	public function set( $key, $value ) {
56 1
		$this->options[$key] = $value;
57 1
	}
58
59
	/**
60
	 * @since 1.0
61
	 *
62
	 * @param string $key
63
	 *
64
	 * @return boolean
65
	 */
66 8
	public function has( $key ) {
67 8
		return isset( $this->options[$key] ) || array_key_exists( $key, $this->options );
68
	}
69
70
	/**
71
	 * @since 1.0
72
	 *
73
	 * @param string $key
74
	 *
75
	 * @return string
76
	 * @throws InvalidArgumentException
77
	 */
78 2
	public function get( $key ) {
79
80 2
		if ( $this->has( $key ) ) {
81 1
			return $this->options[$key];
82
		}
83
84 1
		throw new InvalidArgumentException( "{$key} is an unregistered option" );
85
	}
86
87
}
88