From 296a00225c0c38b9b6c84ce29db0e2342b4ac0e2 Mon Sep 17 00:00:00 2001 From: Naxvog Date: Tue, 10 Nov 2026 23:09:59 +0100 Subject: [PATCH] Skip env resolution in Config::isEnabled() when no override is set 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 --- src/Internal/Util/Config.php | 36 ++++++++++++++++++++---------- tests/Internal/Util/ConfigTest.php | 16 +++++++++++++ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/Internal/Util/Config.php b/src/Internal/Util/Config.php index a80dd9f1..6a9cae56 100644 --- a/src/Internal/Util/Config.php +++ b/src/Internal/Util/Config.php @@ -131,22 +131,34 @@ private static function getEnvValue(string $key): ?string public static function isEnabled(): bool { - $envDisable = self::getEnvValue('TYPEPHP_DISABLE'); - if ($envDisable !== null && trim($envDisable) !== '') { - if (filter_var($envDisable, FILTER_VALIDATE_BOOLEAN)) { - return false; + // Hot path: called by every runtime check. The overrides are re-probed on each call + // (Dotenv may populate them after boot), but only resolved when one is actually present. + if ( + isset($_ENV['TYPEPHP_DISABLE']) + || isset($_SERVER['TYPEPHP_DISABLE']) + || isset($_ENV['TYPEPHP_ENABLED']) + || isset($_SERVER['TYPEPHP_ENABLED']) + || getenv('TYPEPHP_DISABLE') !== false + || getenv('TYPEPHP_ENABLED') !== false + || \defined('TYPEPHP_DISABLE') + ) { + $envDisable = self::getEnvValue('TYPEPHP_DISABLE'); + if ($envDisable !== null && trim($envDisable) !== '') { + if (filter_var($envDisable, FILTER_VALIDATE_BOOLEAN)) { + return false; + } } - } - $envEnabled = self::getEnvValue('TYPEPHP_ENABLED'); - if ($envEnabled !== null && trim($envEnabled) !== '') { - if (! filter_var($envEnabled, FILTER_VALIDATE_BOOLEAN)) { - return false; + $envEnabled = self::getEnvValue('TYPEPHP_ENABLED'); + if ($envEnabled !== null && trim($envEnabled) !== '') { + if (! filter_var($envEnabled, FILTER_VALIDATE_BOOLEAN)) { + return false; + } } - } - if (\defined('TYPEPHP_DISABLE') && (bool) TYPEPHP_DISABLE) { - return false; + if (\defined('TYPEPHP_DISABLE') && (bool) TYPEPHP_DISABLE) { + return false; + } } if (self::$cachedConfig === null) { diff --git a/tests/Internal/Util/ConfigTest.php b/tests/Internal/Util/ConfigTest.php index 5241ad04..eda0d5bb 100644 --- a/tests/Internal/Util/ConfigTest.php +++ b/tests/Internal/Util/ConfigTest.php @@ -313,6 +313,22 @@ function cleanTypePhpEnvVariables(): void unset($_SERVER['TYPEPHP_ENABLED']); }); + test('isEnabled dynamically reflects TYPEPHP_DISABLE and TYPEPHP_ENABLED set through putenv()', function () { + expect(Config::isEnabled())->toBeTrue(); + + putenv('TYPEPHP_DISABLE=1'); + expect(Config::isEnabled())->toBeFalse(); + + putenv('TYPEPHP_DISABLE'); + expect(Config::isEnabled())->toBeTrue(); + + putenv('TYPEPHP_ENABLED=false'); + expect(Config::isEnabled())->toBeFalse(); + + putenv('TYPEPHP_ENABLED'); + expect(Config::isEnabled())->toBeTrue(); + }); + test('isAutoBootEnabled resolves values from $_ENV and $_SERVER', function () { $_ENV['TYPEPHP_AUTO_BOOT'] = 'false'; expect(Config::isAutoBootEnabled())->toBeFalse();