{"record":{"id":"0d656b7c2256cf0e","repo":"PHPOffice/PHPWord","slug":"no-phpword-assigned","errorCode":null,"errorMessage":"No PhpWord assigned.","messagePattern":"No PhpWord assigned\\.","errorType":"exception","errorClass":"PhpOffice\\PhpWord\\Exception\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Writer/AbstractWriter.php","lineNumber":108,"sourceCode":"    /**\n     * Temporary file name.\n     *\n     * @var string\n     */\n    private $tempFilename;\n\n    /**\n     * Get PhpWord object.\n     *\n     * @return PhpWord\n     */\n    public function getPhpWord()\n    {\n        if (null !== $this->phpWord) {\n            return $this->phpWord;\n        }\n\n        throw new Exception('No PhpWord assigned.');\n    }\n\n    /**\n     * Set PhpWord object.\n     *\n     * @return self\n     */\n    public function setPhpWord(?PhpWord $phpWord = null)\n    {\n        $this->phpWord = $phpWord;\n\n        return $this;\n    }\n\n    /**\n     * Get writer part.\n     *\n     * @param string $partName Writer part name","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Writer/AbstractWriter.php#L90-L126","documentation":"AbstractWriter::getPhpWord() returns the PhpWord document assigned to the writer. Writers require a document to serialize; if none was set (via constructor or setPhpWord()) the writer has nothing to write and throws. It is an internal state guard protecting all write operations.","triggerScenarios":"Instantiating a writer directly (e.g. new ODText()) without passing a PhpWord instance, or clearing the phpWord property before calling getContent()/save()/addNotes()/addComments().","commonSituations":"Custom code creating writers via reflection or DI containers that skip the constructor argument; partial mock/factory setups; calling writer methods before IOFactory::createWriter($phpWord, ...) completed assignment.","solutions":["Pass the PhpWord instance to the writer's constructor.","Call setPhpWord($phpWord) on the writer before using it.","Prefer IOFactory::createWriter($phpWord, 'WriterName') so assignment is never missed.","Check custom factory/DI code to ensure it forwards the document argument."],"exampleFix":"// before\n$writer = new \\PhpOffice\\PhpWord\\Writer\\ODText();\n$writer->save('doc.odt'); // No PhpWord assigned\n// after\n$phpWord = new \\PhpOffice\\PhpWord\\PhpWord();\n$writer = new \\PhpOffice\\PhpWord\\Writer\\ODText($phpWord);\n$writer->save('doc.odt');","handlingStrategy":"type-guard","validationCode":"function writerHasDocument(\\PhpOffice\\PhpWord\\Writer\\WriterInterface $writer): bool {\n    $ref = new ReflectionProperty(get_class($writer), 'phpWord');\n    $ref->setAccessible(true);\n    return $ref->getValue($writer) !== null;\n}","typeGuard":null,"tryCatchPattern":"try {\n    $writer->save($path);\n} catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) {\n    if ($e->getMessage() === 'No PhpWord assigned.') {\n        $writer->setPhpWord($phpWord);\n        $writer->save($path);\n    } else { throw $e; }\n}","preventionTips":["Always construct writers through IOFactory::createWriter($phpWord, $name).","Never instantiate writer classes without passing the PhpWord document.","In DI/reflection-based factories, assert the document argument is forwarded.","Guard writer usage with a getPhpWord() null check in wrappers."],"tags":["phpword","writer","state"],"backgroundTag":"null-argument","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"}