cakephp/cakephp · error · LogicException

If config is null, key must be an array.

Error message

If config is null, key must be an array.

What it means

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.

Solutions

  1. Pass a settings array as the second argument: setConfig('default', ['className' => ...]).
  2. Or pass a single array keyed by name: setConfig(['default' => [...]]).
  3. If you meant to read config, use getConfig()/getConfigOrFail() instead of setConfig().
  4. Add a static analysis check so calls with a single string argument are flagged.

Example fix

// before
ConnectionManager::setConfig('default');
// after
ConnectionManager::setConfig('default', ['className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\Mysql']);
Defensive patterns

Strategy: type-guard

Validate before calling

if (is_string($key) && $config === null) {
    throw new InvalidArgumentException('setConfig needs an array when config is null');
}

Type guard

assert(is_array($key) || $config !== null);

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12). Data as JSON: /api/errors/67a558fd37f67827. Report an issue: GitHub.

Appendix: source

Thrown at src/Core/StaticConfigTrait.php:80

     * ```
     *
     * Configure multiple adapters at once:
     *
     * ```
     * Cache::setConfig($arrayOfConfig);
     * ```
     *
     * @param array<string, mixed>|string $key The name of the configuration, or an array of multiple configs.
     * @param mixed $config Configuration value. Generally an array of name => configuration data for adapter.
     * @throws \BadMethodCallException When trying to modify an existing config.
     * @throws \LogicException When trying to store an invalid structured config array.
     * @return void
     */
    public static function setConfig(array|string $key, mixed $config = null): void
    {
        if ($config === null) {
            if (!is_array($key)) {
                throw new LogicException('If config is null, key must be an array.');
            }
            foreach ($key as $name => $settings) {
                static::setConfig((string)$name, $settings);
            }

            return;
        }
        if (!is_string($key)) {
            throw new LogicException('If config is not null, key must be a string.');
        }

        if (isset(static::$_config[$key])) {
            throw new BadMethodCallException(sprintf('Cannot reconfigure existing key `%s`.', $key));
        }

        if (is_object($config)) {
            $config = ['className' => $config];
        }

View on GitHub (pinned to 1128eba9b0)