{"record":{"id":"8f227471bad21110","repo":"PHPOffice/PHPWord","slug":"invalid-type-type","errorCode":null,"errorMessage":"Invalid type '$type'","messagePattern":"Invalid type '\\$type'","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Element/Field.php","lineNumber":200,"sourceCode":"        $this->setOptions($options);\n        $this->setText($text);\n        $this->setFontStyle($fontStyle);\n    }\n\n    /**\n     * Set Field type.\n     *\n     * @param string $type\n     *\n     * @return string\n     */\n    public function setType($type = null)\n    {\n        if (isset($type)) {\n            if (isset($this->fieldsArray[$type])) {\n                $this->type = $type;\n            } else {\n                throw new InvalidArgumentException(\"Invalid type '$type'\");\n            }\n        }\n\n        return $this->type;\n    }\n\n    /**\n     * Get Field type.\n     *\n     * @return string\n     */\n    public function getType()\n    {\n        return $this->type;\n    }\n\n    /**\n     * Set Field properties.","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Element/Field.php#L182-L218","documentation":"Field::setType() validates the requested Word field type (e.g. PAGE, DATE, NUMPAGES) against the hardcoded $fieldsArray whitelist in Field.php. If the type string is not a key of that array, an InvalidArgumentException is thrown and the field type is left unchanged. The library only supports a fixed set of field codes.","triggerScenarios":"Calling new Field($section, 'NOTAFIELD') or $field->setType('Foo') with any string not present as a top-level key of Field::$fieldsArray (case-sensitive).","commonSituations":"Typo'd field codes (e.g. 'Page' instead of 'PAGE'), attempting a Word field code the library hasn't whitelisted, or dynamically building field names from user/config input.","solutions":["Use one of the supported field codes exactly as defined in Field::$fieldsArray (e.g. 'PAGE', 'DATE', 'NUMPAGES', 'TIME', 'FILESIZE').","Check src/PhpWord/Element/Field.php for the $fieldsArray keys to see all valid types.","Wrap setType/new Field in a try-catch for InvalidArgumentException if the type comes from user input.","If a needed field is missing, add it to $fieldsArray in a subclass or upstream PR."],"exampleFix":"// before\n$field = $section->addField('PageNum');\n// after\n$field = $section->addField('PAGE');","handlingStrategy":"validation","validationCode":"$valid = ['PAGE','DATE','NUMPAGES','TIME','FILESIZE']; // mirror Field::$fieldsArray keys\nif (!in_array($type, array_keys(Field::class ? [] : []), true) && !isset($validMap[$type])) { /* reject */ }","typeGuard":"function isValidFieldType(?string $type): bool { $r = new \\ReflectionClass(\\PhpOffice\\PhpWord\\Element\\Field::class); $p = $r->getProperty('fieldsArray'); $p->setAccessible(true); return $type !== null && array_key_exists($type, $p->getValue()); }","tryCatchPattern":"try { $field = $section->addField($type); } catch (\\InvalidArgumentException $e) { $field = $section->addField('PAGE'); }","preventionTips":["Always use exact uppercase field codes from Field::$fieldsArray","Validate user/config-supplied field names against the whitelist before calling","Keep a constant list of supported fields in your app code"],"tags":["phpword","invalid-argument","field","whitelist"],"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"}