Skip to content

Commit e3bf470

Browse files
authored
fix: skip env overrides for anonymous BaseConfig subclasses on PHP 8.6 (#10804)
1 parent 0531321 commit e3bf470

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎docs/changelogs/v4.8.0.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,6 @@ Release Date: Unreleased
393393

394394
## Bugs Fixed
395395

396+
- **Config:** Fixed a bug where instantiating an anonymous `BaseConfig` subclass threw a `ValueError` from `getenv()` on PHP 8.6.
397+
396398
See the repo's [CHANGELOG.md](https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md) for a complete list of bugs fixed.

‎system/Config/BaseConfig.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ public function __construct()
175175

176176
$this->registerProperties();
177177

178+
if (str_contains(static::class, "\0")) {
179+
return; // PHP 8.6 getenv() throws for names with null bytes, as in anonymous class names
180+
}
181+
178182
$values = get_object_vars($this);
179183
$properties = array_keys($values);
180184
$prefix = static::class;

‎tests/system/Config/BaseConfigTest.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use Tests\Support\Config\MergeRegistrarA;
4141
use Tests\Support\Config\MergeRegistrarB;
4242
use Tests\Support\Config\TestRegistrar;
43+
use ValueError;
4344

4445
/**
4546
* @internal
@@ -137,6 +138,27 @@ public function testBasicValues(): void
137138
$this->assertSame(18, $config->golf);
138139
}
139140

141+
public function testAnonymousSubclassSkipsEnvironmentOverrides(): void
142+
{
143+
$dotenv = new DotEnv($this->fixturesFolder, '.env');
144+
$dotenv->load();
145+
146+
$named = new SimpleConfig();
147+
$alpha = null;
148+
149+
$exception = null;
150+
151+
try {
152+
$alpha = (new class () extends SimpleConfig {})->alpha;
153+
} catch (ValueError $exception) {
154+
// ...
155+
}
156+
157+
$this->assertNull($exception, 'BaseConfig should skip environment overrides for anonymous subclasses.');
158+
$this->assertSame('pow', $named->alpha);
159+
$this->assertSame('one', $alpha);
160+
}
161+
140162
public function testUseDefaultValueTypeIntAndFloatValues(): void
141163
{
142164
$dotenv = new DotEnv($this->fixturesFolder, '.env');

0 commit comments

Comments
 (0)