{"record":{"id":"5ae7ca1f370c93af","repo":"twigphp/Twig","slug":"empty-array-elements-are-only-allowed-in-destructuring","errorCode":null,"errorMessage":"Empty array elements are only allowed in destructuring assignments.","messagePattern":"Empty array elements are only allowed in destructuring assignments\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/Node/Expression/ArrayExpression.php","lineNumber":127,"sourceCode":"\n            $names[] = (string) ($i * 2);\n        }\n\n        return $names;\n    }\n\n    public function compile(Compiler $compiler): void\n    {\n        if ($this->definedTest) {\n            $compiler->repr(true);\n\n            return;\n        }\n\n        // Check for empty expressions which are only allowed in destructuring\n        foreach ($this->getKeyValuePairs() as $pair) {\n            if ($pair['value'] instanceof EmptyExpression) {\n                throw new SyntaxError('Empty array elements are only allowed in destructuring assignments.', $pair['value']->getTemplateLine(), $this->getSourceContext());\n            }\n        }\n\n        $compiler->raw('[');\n        $isSequence = true;\n        foreach ($this->getKeyValuePairs() as $i => $pair) {\n            if (0 !== $i) {\n                $compiler->raw(', ');\n            }\n\n            $key = null;\n            if ($pair['key'] instanceof TempNameExpression) {\n                $key = $pair['key']->getAttribute('name');\n                $pair['key'] = new ConstantExpression($key, $pair['key']->getTemplateLine());\n            } elseif ($pair['key'] instanceof ConstantExpression) {\n                $key = $pair['key']->getAttribute('value');\n            } else {\n                // dynamic key: cast to string so PHP accepts it as an array offset","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Node/Expression/ArrayExpression.php#L109-L145","documentation":"While compiling an array/tuple expression, ArrayExpression checks that no element is an EmptyExpression (the parser's placeholder for holes, like the middle slot of [1, , 3]). Empty slots are only meaningful in destructuring assignment, so an empty element in a plain array expression throws this SyntaxError at compile time.","triggerScenarios":"Writing an array literal with a missing element in a template ({{ [1, , 3] }}), leaving a hole when editing arrays, or generating array-literal nodes containing EmptyExpression outside destructuring context.","commonSituations":"Typos with double commas in templates; dangling commas inside brackets after edits; template-generation code emitting placeholders that are only valid in destructuring ({% set [a, , c] = foo %} is fine, {{ [a, , c] }} is not).","solutions":["Remove the empty slot: write [1, 3], or use null explicitly: [1, null, 3].","Check for accidental double commas or a dangling comma inside the array literal.","Use destructuring assignment syntax ({% set [a, , c] = foo %}) if a skipped slot is intended.","Run twig's lint command (bin/console lint:twig) in CI to catch this before deployment."],"exampleFix":"// before (Twig template)\n{% set arr = [1, , 3] %}\n// after\n{% set arr = [1, null, 3] %}","handlingStrategy":"type-guard","validationCode":"// pre-parse check of template source for holes in array literals\nif (preg_match('/\\[[^\\]]*\\b,\\s*(,|\\])/s', $templateSource)) {\n    throw new InvalidArgumentException('Array literal contains empty element(s).');\n}","typeGuard":"function hasNoEmptyArrayElements(array $pairs): bool {\n    return !in_array(null, $pairs, true); // null slots only legal in destructuring\n}","tryCatchPattern":"try {\n    $twig->parse($twig->tokenize(new \\Twig\\Source($code, 'tpl')));\n} catch (\\Twig\\Error\\SyntaxError $e) {\n    if (str_contains($e->getMessage(), 'Empty array elements')) {\n        // report template line and fix the literal\n    }\n    throw $e;\n}","preventionTips":["Lint templates in CI (twig lint command) to catch array holes before deploy.","Avoid double commas in array literals; use null explicitly for empty values.","Reserve empty slots for destructuring ({% set [a, , c] = ... %}) only.","When generating templates, never emit placeholder nodes into plain array literals."],"tags":["template","syntax","compile-time","array"],"backgroundTag":"invalid-argument-format","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}