Skip to content

Commit bdeb54d

Browse files
authored
Skip env resolution in Config::isEnabled() when no override is set (#131)
Every runtime check calls Config::isEnabled(), which resolved TYPEPHP_DISABLE and TYPEPHP_ENABLED through getEnvValue() and trimmed and filtered them on each call, even when neither is set. Probe for the overrides first and only resolve them when one is present. The probes still run on every call, so values that Dotenv or putenv() set after boot keep taking effect. PHP 8.4, 2M iterations, best of 5: Config::isEnabled() 192 ns -> 139 ns call with @param/@return contract 3155 ns -> 2986 ns
1 parent d2bf388 commit bdeb54d

2 files changed

Lines changed: 40 additions & 12 deletions

File tree

‎src/Internal/Util/Config.php‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,34 @@ private static function getEnvValue(string $key): ?string
131131

132132
public static function isEnabled(): bool
133133
{
134-
$envDisable = self::getEnvValue('TYPEPHP_DISABLE');
135-
if ($envDisable !== null && trim($envDisable) !== '') {
136-
if (filter_var($envDisable, FILTER_VALIDATE_BOOLEAN)) {
137-
return false;
134+
// Hot path: called by every runtime check. The overrides are re-probed on each call
135+
// (Dotenv may populate them after boot), but only resolved when one is actually present.
136+
if (
137+
isset($_ENV['TYPEPHP_DISABLE'])
138+
|| isset($_SERVER['TYPEPHP_DISABLE'])
139+
|| isset($_ENV['TYPEPHP_ENABLED'])
140+
|| isset($_SERVER['TYPEPHP_ENABLED'])
141+
|| getenv('TYPEPHP_DISABLE') !== false
142+
|| getenv('TYPEPHP_ENABLED') !== false
143+
|| \defined('TYPEPHP_DISABLE')
144+
) {
145+
$envDisable = self::getEnvValue('TYPEPHP_DISABLE');
146+
if ($envDisable !== null && trim($envDisable) !== '') {
147+
if (filter_var($envDisable, FILTER_VALIDATE_BOOLEAN)) {
148+
return false;
149+
}
138150
}
139-
}
140151

141-
$envEnabled = self::getEnvValue('TYPEPHP_ENABLED');
142-
if ($envEnabled !== null && trim($envEnabled) !== '') {
143-
if (! filter_var($envEnabled, FILTER_VALIDATE_BOOLEAN)) {
144-
return false;
152+
$envEnabled = self::getEnvValue('TYPEPHP_ENABLED');
153+
if ($envEnabled !== null && trim($envEnabled) !== '') {
154+
if (! filter_var($envEnabled, FILTER_VALIDATE_BOOLEAN)) {
155+
return false;
156+
}
145157
}
146-
}
147158

148-
if (\defined('TYPEPHP_DISABLE') && (bool) TYPEPHP_DISABLE) {
149-
return false;
159+
if (\defined('TYPEPHP_DISABLE') && (bool) TYPEPHP_DISABLE) {
160+
return false;
161+
}
150162
}
151163

152164
if (self::$cachedConfig === null) {

‎tests/Internal/Util/ConfigTest.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,22 @@ function cleanTypePhpEnvVariables(): void
313313
unset($_SERVER['TYPEPHP_ENABLED']);
314314
});
315315

316+
test('isEnabled dynamically reflects TYPEPHP_DISABLE and TYPEPHP_ENABLED set through putenv()', function () {
317+
expect(Config::isEnabled())->toBeTrue();
318+
319+
putenv('TYPEPHP_DISABLE=1');
320+
expect(Config::isEnabled())->toBeFalse();
321+
322+
putenv('TYPEPHP_DISABLE');
323+
expect(Config::isEnabled())->toBeTrue();
324+
325+
putenv('TYPEPHP_ENABLED=false');
326+
expect(Config::isEnabled())->toBeFalse();
327+
328+
putenv('TYPEPHP_ENABLED');
329+
expect(Config::isEnabled())->toBeTrue();
330+
});
331+
316332
test('isAutoBootEnabled resolves values from $_ENV and $_SERVER', function () {
317333
$_ENV['TYPEPHP_AUTO_BOOT'] = 'false';
318334
expect(Config::isAutoBootEnabled())->toBeFalse();

0 commit comments

Comments
 (0)