{"record":{"id":"1499296431a1bf36","repo":"moodle/moodle","slug":"header-must-either-be-a-string-or-an-array-containing","errorCode":null,"errorMessage":"$header must either be a string or an array containing strings.","messagePattern":"\\$header must either be a string or an array containing strings\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"public/lib/guzzlehttp/psr7/src/Header.php","lineNumber":86,"sourceCode":"     * Example headers include 'accept', 'cache-control' and 'if-none-match'.\n     *\n     * This method must not be used to parse headers that are not defined as\n     * a list, such as 'user-agent' or 'set-cookie'.\n     *\n     * @param string|string[] $values Header value as returned by MessageInterface::getHeader()\n     *\n     * @return string[]\n     */\n    public static function splitList($values): array\n    {\n        if (!\\is_array($values)) {\n            $values = [$values];\n        }\n\n        $result = [];\n        foreach ($values as $value) {\n            if (!\\is_string($value)) {\n                throw new \\TypeError('$header must either be a string or an array containing strings.');\n            }\n\n            $v = '';\n            $isQuoted = false;\n            $isEscaped = false;\n            for ($i = 0, $max = \\strlen($value); $i < $max; ++$i) {\n                if ($isEscaped) {\n                    $v .= $value[$i];\n                    $isEscaped = false;\n\n                    continue;\n                }\n\n                if (!$isQuoted && $value[$i] === ',') {\n                    $v = \\trim($v);\n                    if ($v !== '') {\n                        $result[] = $v;\n                    }","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/guzzlehttp/psr7/src/Header.php#L68-L104","documentation":"Header::splitList() splits a comma-separated header value into its list elements. It throws a TypeError when it encounters an element that is not a string, since it can only parse string header values.","triggerScenarios":"Calling Header::splitList() with an int, float, bool, null, or an array containing non-string elements — e.g. $headers['Accept-Language'] holding integers or nested arrays.","commonSituations":"Passing loosely-typed header arrays from config files, PSR-15 middleware with mixed header value types, or getallheaders()-style arrays mutated by other code.","solutions":["Cast or filter the input to strings before calling: array_map('strval', ...).","Validate that $header is a string or string[] at your boundary.","If the value may be null, default it to an empty string or skip the call."],"exampleFix":"// before\nHeader::splitList([1, 2]); // TypeError\n// after\n$values = array_map('strval', [1, 2]);\nHeader::splitList($values);","handlingStrategy":"type-guard","validationCode":"if (!is_string($header) && !(is_array($header) && array_values($header) === $header && count($header) === count(array_filter($header, 'is_string')))) { throw new \\TypeError('header must be string or list of strings'); }","typeGuard":"function isHeaderList($v): bool { return is_string($v) || (is_array($v) && array_reduce($v, fn($c, $i) => $c && is_string($i), true)); }","tryCatchPattern":"try { $parts = \\GuzzleHttp\\Psr7\\Header::splitList($header); } catch (\\TypeError $e) { $parts = []; /* or strval-cast and retry */ }","preventionTips":["Cast header values with (string) or strval() before parsing","Keep header arrays strictly string-typed","Sanitize values from config files or external input"],"tags":["php","psr7","http-headers","type-error"],"backgroundTag":"type-mismatch","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}