{"record":{"id":"67a558fd37f67827","repo":"cakephp/cakephp","slug":"if-config-is-null-key-must-be-an-array","errorCode":null,"errorMessage":"If config is null, key must be an array.","messagePattern":"If config is null, key must be an array\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Core/StaticConfigTrait.php","lineNumber":80,"sourceCode":"     * ```\n     *\n     * Configure multiple adapters at once:\n     *\n     * ```\n     * Cache::setConfig($arrayOfConfig);\n     * ```\n     *\n     * @param array<string, mixed>|string $key The name of the configuration, or an array of multiple configs.\n     * @param mixed $config Configuration value. Generally an array of name => configuration data for adapter.\n     * @throws \\BadMethodCallException When trying to modify an existing config.\n     * @throws \\LogicException When trying to store an invalid structured config array.\n     * @return void\n     */\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        }","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/cakephp/cakephp/blob/1128eba9b09f1946df684811350e26f2cef68fac/src/Core/StaticConfigTrait.php#L62-L98","documentation":"StaticConfigTrait::setConfig() accepts either (string $key, mixed $config) or (array $key => settings). When $config is omitted (null), the trait demands $key be an array of configs; passing a bare string with no config is a caller bug and throws this LogicException.","triggerScenarios":"Calling e.g. ConnectionManager::setConfig('default') or Log::setConfig('debug') with only a string key and no second argument, instead of passing an array of settings.","commonSituations":"Refactoring from per-engine static config methods to setConfig, copy-paste dropping the config array, thinking the key alone references existing config (use getConfig for that).","solutions":["Pass a settings array as the second argument: setConfig('default', ['className' => ...]).","Or pass a single array keyed by name: setConfig(['default' => [...]]).","If you meant to read config, use getConfig()/getConfigOrFail() instead of setConfig().","Add a static analysis check so calls with a single string argument are flagged."],"exampleFix":"// before\nConnectionManager::setConfig('default');\n// after\nConnectionManager::setConfig('default', ['className' => 'Cake\\Database\\Connection', 'driver' => 'Cake\\Database\\Driver\\Mysql']);","handlingStrategy":"type-guard","validationCode":"if (is_string($key) && $config === null) {\n    throw new InvalidArgumentException('setConfig needs an array when config is null');\n}","typeGuard":"assert(is_array($key) || $config !== null);","tryCatchPattern":null,"preventionTips":["Remember the two signatures: (array configs) or (string key, config)","Never call setConfig with a single string argument","Use IDE static analysis (phpstan) to catch wrong arity/type calls"],"tags":["php","cakephp","configuration","developer-error"],"backgroundTag":"missing-required-argument","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"}