{"record":{"id":"6591db61b9e237b0","repo":"PHPOffice/PHPWord","slug":"invalid-text","errorCode":null,"errorMessage":"Invalid text","messagePattern":"Invalid text","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Element/Field.php","lineNumber":284,"sourceCode":"    public function getOptions()\n    {\n        return $this->options;\n    }\n\n    /**\n     * Set Field text.\n     *\n     * @param null|mixed|string|TextRun $text\n     *\n     * @return null|string|TextRun\n     */\n    public function setText($text = null)\n    {\n        if (null !== $text) {\n            if (is_string($text) || $text instanceof TextRun) {\n                $this->text = $text;\n            } else {\n                throw new InvalidArgumentException('Invalid text');\n            }\n        }\n\n        return $this->text;\n    }\n\n    /**\n     * Get Field text.\n     *\n     * @return string|TextRun\n     */\n    public function getText()\n    {\n        return $this->text;\n    }\n}\n","sourceCodeStart":266,"sourceCodeEnd":301,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Element/Field.php#L266-L301","documentation":"Field::setText() accepts only a string or a TextRun instance as the field's display text. Any other scalar/object (int, array, null-adjacent values, plain Text) throws InvalidArgumentException. The value is only assigned when non-null.","triggerScenarios":"Calling $field->setText(123), setText(['a']), or setText(new Text('x')) — anything not is_string() and not instanceof TextRun.","commonSituations":"Passing numeric values from computed data without casting to string, or confusing PhpWord\\Element\\Text with TextRun.","solutions":["Cast the value to a string before calling setText(), e.g. setText((string) $value).","Wrap multiple inline elements in a TextRun and pass that instead.","Pass null (or omit) if you intend no text; null is accepted and skipped.","Add an is_string||TextRun check before the call for dynamic values."],"exampleFix":"// before\n$field->setText($count);\n// after\n$field->setText((string) $count);","handlingStrategy":"type-guard","validationCode":"if (null !== $text && !is_string($text) && !$text instanceof \\PhpOffice\\PhpWord\\Element\\TextRun) { $text = (string) $text; }","typeGuard":"function isValidFieldText($text): bool { return $text === null || is_string($text) || $text instanceof \\PhpOffice\\PhpWord\\Element\\TextRun; }","tryCatchPattern":"try { $field->setText($value); } catch (\\InvalidArgumentException $e) { $field->setText((string) $value); }","preventionTips":["Cast scalars to string before setText","Use TextRun, not Text, for rich field text","Pass null instead of other falsy values when clearing"],"tags":["phpword","type-mismatch","field","text"],"backgroundTag":"invalid-argument-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"}