{"record":{"id":"04f6d279e104d501","repo":"PHPOffice/PHPWord","slug":"invalid-header-footer-type","errorCode":null,"errorMessage":"Invalid header/footer type.","messagePattern":"Invalid header/footer type\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Element/Section.php","lineNumber":214,"sourceCode":"    private function addHeaderFooter($type = Header::AUTO, $header = true)\n    {\n        $containerClass = substr(static::class, 0, strrpos(static::class, '\\\\') ?: 0) . '\\\\' .\n            ($header ? 'Header' : 'Footer');\n        $collectionArray = $header ? 'headers' : 'footers';\n        $collection = &$this->$collectionArray;\n\n        if (in_array($type, [Header::AUTO, Header::FIRST, Header::EVEN])) {\n            $index = count($collection);\n            /** @var AbstractContainer $container Type hint */\n            $container = new $containerClass($this->sectionId, ++$index, $type);\n            $container->setPhpWord($this->phpWord);\n\n            $collection[$index] = $container;\n\n            return $container;\n        }\n\n        throw new Exception('Invalid header/footer type.');\n    }\n}\n","sourceCodeStart":196,"sourceCodeEnd":217,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Element/Section.php#L196-L217","documentation":"Section::addHeaderFooter() builds a Header/Footer container for the given type, but only Header::AUTO, Header::FIRST and Header::EVEN (and the footer equivalents) are recognized; anything else falls through to a generic PhpOffice\\PhpWord\\Exception\\Exception 'Invalid header/footer type.'. It is an internal whitelist check on the container type constant.","triggerScenarios":"Calling $section->addHeader('FOO') or addFooter with a string other than 'AUTO'/'FIRST'/'EVEN' (the Header/Footer constants), often from a typo or dynamic value.","commonSituations":"Passing 'DEFAULT' instead of 'AUTO', mixing up header/footer type names, or mapping document settings to header types without validating the mapping.","solutions":["Use the class constants: Section::addHeader(Header::AUTO) (or FIRST / EVEN).","Fix typos — valid values are exactly 'AUTO', 'FIRST', 'EVEN'.","Validate/whitelist a config value against these constants before passing it in.","Catch PhpWord Exception around addHeader/addFooter for user-driven configuration."],"exampleFix":"// before\n$section->addHeader('DEFAULT');\n// after\n$section->addHeader(Header::AUTO);","handlingStrategy":"type-guard","validationCode":"$allowed = [\\PhpOffice\\PhpWord\\Element\\Header::AUTO, \\PhpOffice\\PhpWord\\Element\\Header::FIRST, \\PhpOffice\\PhpWord\\Element\\Header::EVEN];\nif (!in_array($type, $allowed, true)) { throw new DomainException('Bad header type'); }","typeGuard":"function isValidHeaderFooterType(string $t): bool { return in_array($t, ['AUTO', 'FIRST', 'EVEN'], true); }","tryCatchPattern":"try { $hdr = $section->addHeader($type); } catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) { $hdr = $section->addHeader(Header::AUTO); }","preventionTips":["Always use the Header/Footer class constants, not raw strings","Remember the valid values are exactly AUTO, FIRST, EVEN","Map app-level settings to these constants in one place"],"tags":["phpword","header","footer","invalid-argument"],"backgroundTag":"invalid-enum-value","analyzedSha":"aef95c04151b5633cc505f672ddd6a71da900ee1","analyzedAt":"2026-09-14T10:53:58.933Z","contentChangedAt":"2026-09-14T10:53:58.933Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}