These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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 |
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: