moodle/moodle · error · TypeError
$header must either be a string or an array containing…
Error message
$header must either be a string or an array containing strings.
What it means
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.
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.
Example fix
// before
Header::splitList([1, 2]); // TypeError
// after
$values = array_map('strval', [1, 2]);
Header::splitList($values); Defensive patterns
Strategy: type-guard
Validate before calling
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'); } Type guard
function isHeaderList($v): bool { return is_string($v) || (is_array($v) && array_reduce($v, fn($c, $i) => $c && is_string($i), true)); } Try / catch
try { $parts = \GuzzleHttp\Psr7\Header::splitList($header); } catch (\TypeError $e) { $parts = []; /* or strval-cast and retry */ } Prevention
- Cast header values with (string) or strval() before parsing
- Keep header arrays strictly string-typed
- Sanitize values from config files or external input
When it happens
Trigger: 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.
Common situations: Passing loosely-typed header arrays from config files, PSR-15 middleware with mixed header value types, or getallheaders()-style arrays mutated by other code.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- A ' ' key is required
- Addition arguments must be Matrix or array
- All cacheable requests must have creation dates.
- An array is expected
- Argument 1 passed to
AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14).
Data as JSON: /api/errors/1499296431a1bf36.
Report an issue: GitHub.
Appendix: source
Thrown at public/lib/guzzlehttp/psr7/src/Header.php:86
* Example headers include 'accept', 'cache-control' and 'if-none-match'.
*
* This method must not be used to parse headers that are not defined as
* a list, such as 'user-agent' or 'set-cookie'.
*
* @param string|string[] $values Header value as returned by MessageInterface::getHeader()
*
* @return string[]
*/
public static function splitList($values): array
{
if (!\is_array($values)) {
$values = [$values];
}
$result = [];
foreach ($values as $value) {
if (!\is_string($value)) {
throw new \TypeError('$header must either be a string or an array containing strings.');
}
$v = '';
$isQuoted = false;
$isEscaped = false;
for ($i = 0, $max = \strlen($value); $i < $max; ++$i) {
if ($isEscaped) {
$v .= $value[$i];
$isEscaped = false;
continue;
}
if (!$isQuoted && $value[$i] === ',') {
$v = \trim($v);
if ($v !== '') {
$result[] = $v;
}View on GitHub (pinned to 48ea8c3322)