These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Copyright 2014 |
||
4 | * |
||
5 | * This program is free software; you can redistribute it and/or modify |
||
6 | * it under the terms of the GNU General Public License as published by |
||
7 | * the Free Software Foundation; either version 2 of the License, or |
||
8 | * (at your option) any later version. |
||
9 | * |
||
10 | * This program is distributed in the hope that it will be useful, |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
13 | * GNU General Public License for more details. |
||
14 | * |
||
15 | * You should have received a copy of the GNU General Public License along |
||
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||
18 | * http://www.gnu.org/copyleft/gpl.html |
||
19 | * |
||
20 | * @file |
||
21 | */ |
||
22 | |||
23 | /** |
||
24 | * Accesses configuration settings from $GLOBALS |
||
25 | * |
||
26 | * @since 1.23 |
||
27 | */ |
||
28 | class GlobalVarConfig implements Config { |
||
29 | |||
30 | /** |
||
31 | * Prefix to use for configuration variables |
||
32 | * @var string |
||
33 | */ |
||
34 | private $prefix; |
||
35 | |||
36 | /** |
||
37 | * Default builder function |
||
38 | * @return GlobalVarConfig |
||
39 | */ |
||
40 | public static function newInstance() { |
||
41 | return new GlobalVarConfig(); |
||
42 | } |
||
43 | |||
44 | public function __construct( $prefix = 'wg' ) { |
||
45 | $this->prefix = $prefix; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @see Config::get |
||
50 | */ |
||
51 | public function get( $name ) { |
||
52 | if ( !$this->has( $name ) ) { |
||
53 | throw new ConfigException( __METHOD__ . ": undefined option: '$name'" ); |
||
54 | } |
||
55 | return $this->getWithPrefix( $this->prefix, $name ); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @see Config::has |
||
60 | */ |
||
61 | public function has( $name ) { |
||
62 | return $this->hasWithPrefix( $this->prefix, $name ); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Get a variable with a given prefix, if not the defaults. |
||
67 | * |
||
68 | * @param string $prefix Prefix to use on the variable, if one. |
||
69 | * @param string $name Variable name without prefix |
||
70 | * @return mixed |
||
71 | */ |
||
72 | protected function getWithPrefix( $prefix, $name ) { |
||
0 ignored issues
–
show
|
|||
73 | return $GLOBALS[$prefix . $name]; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Check if a variable with a given prefix is set |
||
78 | * |
||
79 | * @param string $prefix Prefix to use on the variable |
||
80 | * @param string $name Variable name without prefix |
||
81 | * @return bool |
||
82 | */ |
||
83 | protected function hasWithPrefix( $prefix, $name ) { |
||
0 ignored issues
–
show
hasWithPrefix 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);
}
}
![]() |
|||
84 | $var = $prefix . $name; |
||
85 | return array_key_exists( $var, $GLOBALS ); |
||
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: