{"record":{"id":"72309247d2e72493","repo":"cakephp/cakephp","slug":"cannot-reconfigure-existing-key-s","errorCode":null,"errorMessage":"Cannot reconfigure existing key `%s`.","messagePattern":"Cannot reconfigure existing key `(.+?)`\\.","errorType":"exception","errorClass":"BadMethodCallException","httpStatus":null,"severity":"error","filePath":"src/Core/StaticConfigTrait.php","lineNumber":93,"sourceCode":"     */\n    public static function setConfig(array|string $key, mixed $config = null): void\n    {\n        if ($config === null) {\n            if (!is_array($key)) {\n                throw new LogicException('If config is null, key must be an array.');\n            }\n            foreach ($key as $name => $settings) {\n                static::setConfig((string)$name, $settings);\n            }\n\n            return;\n        }\n        if (!is_string($key)) {\n            throw new LogicException('If config is not null, key must be a string.');\n        }\n\n        if (isset(static::$_config[$key])) {\n            throw new BadMethodCallException(sprintf('Cannot reconfigure existing key `%s`.', $key));\n        }\n\n        if (is_object($config)) {\n            $config = ['className' => $config];\n        }\n\n        if (is_array($config) && isset($config['url'])) {\n            $parsed = static::parseDsn($config['url']);\n            unset($config['url']);\n            $config = $parsed + $config;\n        }\n\n        if (isset($config['engine']) && empty($config['className'])) {\n            $config['className'] = $config['engine'];\n            unset($config['engine']);\n        }\n\n        static::$_config[$key] = $config;","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/cakephp/cakephp/blob/1128eba9b09f1946df684811350e26f2cef68fac/src/Core/StaticConfigTrait.php#L75-L111","documentation":"StaticConfigTrait::setConfig() forbids overwriting an existing configuration key and throws BadMethodCallException with this message. Configurations are write-once; to change a config you must drop it first (dropConfig) or use a different key.","triggerScenarios":"Calling setConfig('default', ...) twice on the same alias during a single request/bootstrap — e.g., application config then plugin bootstrap both configure the same key, or tests re-configuring between test cases without cleanup.","commonSituations":"Duplicated datasource/log/email aliases defined in app.php and again in bootstrap; test suites that call setConfig in setUp() without tearDown() calling dropConfig(); two plugins configuring the same cache engine.","solutions":["Call static::dropConfig('key') before re-configuring, or use a unique key name.","Guard with if (!static::getConfig('key', null)) before setting.","In tests, reset config in tearDown(): ClassName::dropConfig('key').","Remove the duplicate setConfig call (search bootstrap/plugins for the alias)."],"exampleFix":"// before\nCache::setConfig('default', $config);\n// later...\nCache::setConfig('default', $otherConfig);\n// after\nCache::dropConfig('default');\nCache::setConfig('default', $otherConfig);","handlingStrategy":"validation","validationCode":"if (Cache::getConfig('default') !== null) {\n    Cache::dropConfig('default');\n}\nCache::setConfig('default', $config);","typeGuard":null,"tryCatchPattern":"try {\n    Cache::setConfig('default', $cfg);\n} catch (\\BadMethodCallException $e) {\n    Cache::dropConfig('default');\n    Cache::setConfig('default', $cfg);\n}","preventionTips":["Grep bootstrap/plugins for duplicate alias definitions","In tests, call dropConfig for every alias you setConfig in setUp()","Use unique alias names per plugin to avoid collisions"],"tags":["php","cakephp","configuration","duplicate-key"],"backgroundTag":"conflicting-config-options","analyzedSha":"1128eba9b09f1946df684811350e26f2cef68fac","analyzedAt":"2026-09-12T12:07:00.388Z","contentChangedAt":"2026-09-12T12:07:00.388Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}