weew /
config
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
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 Weew\Config; |
||
| 4 | |||
| 5 | use Weew\Config\Exceptions\InvalidConfigValueException; |
||
| 6 | use Weew\Config\Exceptions\MissingConfigException; |
||
| 7 | |||
| 8 | class Config implements IConfig { |
||
| 9 | /** |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | protected $config; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var IConfigParser |
||
| 16 | */ |
||
| 17 | protected $parser; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @param array $config |
||
| 21 | * @param IConfigParser $parser |
||
| 22 | */ |
||
| 23 | public function __construct(array $config = [], IConfigParser $parser = null) { |
||
| 24 | if ( ! $parser instanceof IConfigParser) { |
||
| 25 | $parser = $this->createConfigParser(); |
||
| 26 | } |
||
| 27 | |||
| 28 | $this->setConfigParser($parser); |
||
| 29 | $this->setConfig($config); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @return array |
||
| 34 | */ |
||
| 35 | public function getConfig() { |
||
| 36 | return $this->config; |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param array $config |
||
| 41 | */ |
||
| 42 | public function setConfig(array $config) { |
||
| 43 | $this->config = $config; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param $key |
||
| 48 | * @param null $default |
||
| 49 | * |
||
| 50 | * @return mixed |
||
| 51 | */ |
||
| 52 | public function get($key, $default = null) { |
||
| 53 | $key = $this->getAbsoluteConfigKey($key); |
||
| 54 | |||
| 55 | return $this->getConfigParser() |
||
| 56 | ->parse($this, array_get($this->config, $key, $default)); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param $key |
||
| 61 | * @param null $default |
||
| 62 | * |
||
| 63 | * @return mixed |
||
| 64 | */ |
||
| 65 | public function getRaw($key, $default = null) { |
||
| 66 | return array_get($this->config, $key, $default); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param $key |
||
| 71 | * @param $value |
||
| 72 | * |
||
| 73 | * @return IConfig |
||
| 74 | */ |
||
| 75 | public function set($key, $value) { |
||
| 76 | $key = $this->getAbsoluteConfigKey($key); |
||
| 77 | |||
| 78 | array_set($this->config, $key, $value); |
||
| 79 | |||
| 80 | return $this; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param $key |
||
| 85 | * |
||
| 86 | * @return bool |
||
| 87 | */ |
||
| 88 | public function has($key) { |
||
| 89 | $key = $this->getAbsoluteConfigKey($key); |
||
| 90 | |||
| 91 | return array_has($this->config, $key); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param $key |
||
| 96 | * |
||
| 97 | * @return IConfig |
||
| 98 | */ |
||
| 99 | public function remove($key) { |
||
| 100 | $key = $this->getAbsoluteConfigKey($key); |
||
| 101 | |||
| 102 | array_remove($this->config, $key); |
||
| 103 | |||
| 104 | return $this; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param array $config |
||
| 109 | * |
||
| 110 | * @return IConfig |
||
| 111 | */ |
||
| 112 | public function merge(array $config) { |
||
| 113 | $this->setConfig( |
||
| 114 | array_extend_distinct($this->getConfig(), $config) |
||
| 115 | ); |
||
| 116 | |||
| 117 | return $this; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param IConfig $config |
||
| 122 | * |
||
| 123 | * @return IConfig |
||
| 124 | */ |
||
| 125 | public function extend(IConfig $config) { |
||
| 126 | $this->merge($config->getConfig()); |
||
| 127 | |||
| 128 | return $this; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param $key |
||
| 133 | * @param null $errorMessage |
||
| 134 | * @param null $scalarType |
||
| 135 | * |
||
| 136 | * @return IConfig |
||
| 137 | * @throws InvalidConfigValueException |
||
| 138 | * @throws MissingConfigException |
||
| 139 | */ |
||
| 140 | public function ensure($key, $errorMessage = null, $scalarType = null) { |
||
| 141 | if ( ! $this->has($key)) { |
||
| 142 | if ($errorMessage === null) { |
||
| 143 | $errorMessage = sprintf('Missing config at key "%s".', $key); |
||
| 144 | } else { |
||
| 145 | $errorMessage = s('%s: %s', $key, $errorMessage); |
||
| 146 | } |
||
| 147 | |||
| 148 | throw new MissingConfigException($errorMessage); |
||
| 149 | } |
||
| 150 | |||
| 151 | if ($scalarType !== null) { |
||
| 152 | if ( ! str_starts_with(gettype($this->get($key)), $scalarType)) { |
||
| 153 | $errorMessage = sprintf('%s: Config value at key "%s" should be of type "%s".', $key, $key, $scalarType); |
||
| 154 | |||
| 155 | throw new InvalidConfigValueException($errorMessage); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | return $this; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | public function toArray() { |
||
| 166 | return $this->getConfigParser() |
||
| 167 | ->parse($this, $this->getConfig()); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return ConfigParser |
||
| 172 | */ |
||
| 173 | protected function createConfigParser() { |
||
| 174 | return new ConfigParser(); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return IConfigParser |
||
| 179 | */ |
||
| 180 | public function getConfigParser() { |
||
| 181 | return $this->parser; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param IConfigParser $parser |
||
| 186 | */ |
||
| 187 | public function setConfigParser(IConfigParser $parser) { |
||
| 188 | $this->parser = $parser; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param $key |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function getAbsoluteConfigKey($key) { |
||
| 197 | $prefix = null; |
||
|
0 ignored issues
–
show
|
|||
| 198 | $parser = $this->getConfigParser(); |
||
| 199 | $steps = explode('.', $key); |
||
| 200 | $config = $this->config; |
||
| 201 | |||
| 202 | foreach ($steps as $step) { |
||
| 203 | if ( ! is_array($config)) { |
||
| 204 | break; |
||
| 205 | } |
||
| 206 | |||
| 207 | array_shift($steps); |
||
| 208 | $config = array_get($config, $step); |
||
| 209 | |||
| 210 | if ($parser->isReference($config)) { |
||
| 211 | array_unshift($steps, $parser->parseReferencePath($config)); |
||
| 212 | $config = implode('.', $steps); |
||
| 213 | |||
| 214 | return $this->getAbsoluteConfigKey($config); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | return $key; |
||
| 219 | } |
||
| 220 | } |
||
| 221 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.