• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 13946675487

19 Mar 2025 12:36PM UTC coverage: 84.559% (+0.004%) from 84.555%
13946675487

Pull #9476

github

web-flow
Merge cc64430aa into 1cfe68cc1
Pull Request #9476: fix: Inconsistent directives value between Default and OPCache groups

2 of 2 new or added lines in 1 file covered. (100.0%)

57 existing lines in 10 files now uncovered.

20843 of 24649 relevant lines covered (84.56%)

191.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.82
/system/Config/BaseConfig.php
1
<?php
2

3
/**
4
 * This file is part of CodeIgniter 4 framework.
5
 *
6
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11

12
namespace CodeIgniter\Config;
13

14
use CodeIgniter\Autoloader\FileLocatorInterface;
15
use CodeIgniter\Exceptions\ConfigException;
16
use CodeIgniter\Exceptions\RuntimeException;
17
use Config\Encryption;
18
use Config\Modules;
19
use ReflectionClass;
20
use ReflectionException;
21

22
/**
23
 * Class BaseConfig
24
 *
25
 * Not intended to be used on its own, this class will attempt to
26
 * automatically populate the child class' properties with values
27
 * from the environment.
28
 *
29
 * These can be set within the .env file.
30
 *
31
 * @phpstan-consistent-constructor
32
 * @see \CodeIgniter\Config\BaseConfigTest
33
 */
34
class BaseConfig
35
{
36
    /**
37
     * An optional array of classes that will act as Registrars
38
     * for rapidly setting config class properties.
39
     *
40
     * @var array
41
     */
42
    public static $registrars = [];
43

44
    /**
45
     * Whether to override properties by Env vars and Registrars.
46
     */
47
    public static bool $override = true;
48

49
    /**
50
     * Has module discovery completed?
51
     *
52
     * @var bool
53
     */
54
    protected static $didDiscovery = false;
55

56
    /**
57
     * Is module discovery running or not?
58
     */
59
    protected static bool $discovering = false;
60

61
    /**
62
     * The processing Registrar file for error message.
63
     */
64
    protected static string $registrarFile = '';
65

66
    /**
67
     * The modules configuration.
68
     *
69
     * @var Modules|null
70
     */
71
    protected static $moduleConfig;
72

73
    public static function __set_state(array $array)
74
    {
75
        static::$override = false;
2✔
76
        $obj              = new static();
2✔
77
        static::$override = true;
2✔
78

79
        $properties = array_keys(get_object_vars($obj));
2✔
80

81
        foreach ($properties as $property) {
2✔
82
            $obj->{$property} = $array[$property];
2✔
83
        }
84

85
        return $obj;
2✔
86
    }
87

88
    /**
89
     * @internal For testing purposes only.
90
     * @testTag
91
     */
92
    public static function setModules(Modules $modules): void
93
    {
94
        static::$moduleConfig = $modules;
1✔
95
    }
96

97
    /**
98
     * @internal For testing purposes only.
99
     * @testTag
100
     */
101
    public static function reset(): void
102
    {
103
        static::$registrars   = [];
18✔
104
        static::$override     = true;
18✔
105
        static::$didDiscovery = false;
18✔
106
        static::$moduleConfig = null;
18✔
107
    }
108

109
    /**
110
     * Will attempt to get environment variables with names
111
     * that match the properties of the child class.
112
     *
113
     * The "shortPrefix" is the lowercase-only config class name.
114
     */
115
    public function __construct()
116
    {
117
        static::$moduleConfig ??= new Modules();
6,639✔
118

119
        if (! static::$override) {
6,639✔
120
            return;
6✔
121
        }
122

123
        $this->registerProperties();
6,635✔
124

125
        $properties  = array_keys(get_object_vars($this));
6,635✔
126
        $prefix      = static::class;
6,635✔
127
        $slashAt     = strrpos($prefix, '\\');
6,635✔
128
        $shortPrefix = strtolower(substr($prefix, $slashAt === false ? 0 : $slashAt + 1));
6,635✔
129

130
        foreach ($properties as $property) {
6,635✔
131
            $this->initEnvValue($this->{$property}, $property, $prefix, $shortPrefix);
6,635✔
132

133
            if ($this instanceof Encryption && $property === 'key') {
6,635✔
134
                if (str_starts_with($this->{$property}, 'hex2bin:')) {
34✔
135
                    // Handle hex2bin prefix
136
                    $this->{$property} = hex2bin(substr($this->{$property}, 8));
2✔
137
                } elseif (str_starts_with($this->{$property}, 'base64:')) {
32✔
138
                    // Handle base64 prefix
139
                    $this->{$property} = base64_decode(substr($this->{$property}, 7), true);
2✔
140
                }
141
            }
142
        }
143
    }
144

145
    /**
146
     * Initialization an environment-specific configuration setting
147
     *
148
     * @param array|bool|float|int|string|null $property
149
     *
150
     * @return void
151
     */
152
    protected function initEnvValue(&$property, string $name, string $prefix, string $shortPrefix)
153
    {
154
        if (is_array($property)) {
6,635✔
155
            foreach (array_keys($property) as $key) {
6,628✔
156
                $this->initEnvValue($property[$key], "{$name}.{$key}", $prefix, $shortPrefix);
6,628✔
157
            }
158
        } elseif (($value = $this->getEnvValue($name, $prefix, $shortPrefix)) !== false && $value !== null) {
6,635✔
159
            if ($value === 'false') {
6,583✔
160
                $value = false;
10✔
161
            } elseif ($value === 'true') {
6,583✔
162
                $value = true;
10✔
163
            }
164
            if (is_bool($value)) {
6,583✔
165
                $property = $value;
10✔
166

167
                return;
10✔
168
            }
169

170
            $value = trim($value, '\'"');
6,583✔
171

172
            if (is_int($property)) {
6,583✔
173
                $value = (int) $value;
10✔
174
            } elseif (is_float($property)) {
6,583✔
175
                $value = (float) $value;
10✔
176
            }
177

178
            // If the default value of the property is `null` and the type is not
179
            // `string`, TypeError will happen.
180
            // So cannot set `declare(strict_types=1)` in this file.
181
            $property = $value;
6,583✔
182
        }
183
    }
184

185
    /**
186
     * Retrieve an environment-specific configuration setting
187
     *
188
     * @return string|null
189
     */
190
    protected function getEnvValue(string $property, string $prefix, string $shortPrefix)
191
    {
192
        $shortPrefix        = ltrim($shortPrefix, '\\');
6,635✔
193
        $underscoreProperty = str_replace('.', '_', $property);
6,635✔
194

195
        switch (true) {
196
            case array_key_exists("{$shortPrefix}.{$property}", $_ENV):
6,635✔
197
                return $_ENV["{$shortPrefix}.{$property}"];
12✔
198

199
            case array_key_exists("{$shortPrefix}_{$underscoreProperty}", $_ENV):
6,635✔
200
                return $_ENV["{$shortPrefix}_{$underscoreProperty}"];
10✔
201

202
            case array_key_exists("{$shortPrefix}.{$property}", $_SERVER):
6,635✔
203
                return $_SERVER["{$shortPrefix}.{$property}"];
6,583✔
204

205
            case array_key_exists("{$shortPrefix}_{$underscoreProperty}", $_SERVER):
6,635✔
UNCOV
206
                return $_SERVER["{$shortPrefix}_{$underscoreProperty}"];
×
207

208
            case array_key_exists("{$prefix}.{$property}", $_ENV):
6,635✔
209
                return $_ENV["{$prefix}.{$property}"];
10✔
210

211
            case array_key_exists("{$prefix}_{$underscoreProperty}", $_ENV):
6,635✔
212
                return $_ENV["{$prefix}_{$underscoreProperty}"];
10✔
213

214
            case array_key_exists("{$prefix}.{$property}", $_SERVER):
6,635✔
215
                return $_SERVER["{$prefix}.{$property}"];
1✔
216

217
            case array_key_exists("{$prefix}_{$underscoreProperty}", $_SERVER):
6,635✔
UNCOV
218
                return $_SERVER["{$prefix}_{$underscoreProperty}"];
×
219

220
            default:
221
                $value = getenv("{$shortPrefix}.{$property}");
6,635✔
222
                $value = $value === false ? getenv("{$shortPrefix}_{$underscoreProperty}") : $value;
6,635✔
223
                $value = $value === false ? getenv("{$prefix}.{$property}") : $value;
6,635✔
224
                $value = $value === false ? getenv("{$prefix}_{$underscoreProperty}") : $value;
6,635✔
225

226
                return $value === false ? null : $value;
6,635✔
227
        }
228
    }
229

230
    /**
231
     * Provides external libraries a simple way to register one or more
232
     * options into a config file.
233
     *
234
     * @return void
235
     *
236
     * @throws ReflectionException
237
     */
238
    protected function registerProperties()
239
    {
240
        if (! static::$moduleConfig->shouldDiscover('registrars')) {
6,635✔
241
            return;
1✔
242
        }
243

244
        if (! static::$didDiscovery) {
6,635✔
245
            // Discovery must be completed before the first instantiation of any Config class.
246
            if (static::$discovering) {
19✔
247
                throw new ConfigException(
×
248
                    'During Auto-Discovery of Registrars,'
×
249
                    . ' "' . static::class . '" executes Auto-Discovery again.'
×
250
                    . ' "' . clean_path(static::$registrarFile) . '" seems to have bad code.',
×
UNCOV
251
                );
×
252
            }
253

254
            static::$discovering = true;
19✔
255

256
            /** @var FileLocatorInterface */
257
            $locator         = service('locator');
19✔
258
            $registrarsFiles = $locator->search('Config/Registrar.php');
19✔
259

260
            foreach ($registrarsFiles as $file) {
19✔
261
                // Saves the file for error message.
262
                static::$registrarFile = $file;
19✔
263

264
                $className = $locator->findQualifiedNameFromPath($file);
19✔
265

266
                if ($className === false) {
19✔
UNCOV
267
                    continue;
×
268
                }
269

270
                static::$registrars[] = new $className();
19✔
271
            }
272

273
            static::$didDiscovery = true;
19✔
274
            static::$discovering  = false;
19✔
275
        }
276

277
        $shortName = (new ReflectionClass($this))->getShortName();
6,635✔
278

279
        // Check the registrar class for a method named after this class' shortName
280
        foreach (static::$registrars as $callable) {
6,635✔
281
            // ignore non-applicable registrars
282
            if (! method_exists($callable, $shortName)) {
6,635✔
283
                continue; // @codeCoverageIgnore
6,635✔
284
            }
285

286
            $properties = $callable::$shortName();
801✔
287

288
            if (! is_array($properties)) {
801✔
289
                throw new RuntimeException('Registrars must return an array of properties and their values.');
1✔
290
            }
291

292
            foreach ($properties as $property => $value) {
800✔
293
                if (isset($this->{$property}) && is_array($this->{$property}) && is_array($value)) {
721✔
294
                    $this->{$property} = array_merge($this->{$property}, $value);
721✔
295
                } else {
UNCOV
296
                    $this->{$property} = $value;
×
297
                }
298
            }
299
        }
300
    }
301
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc