{"record":{"id":"78c64f0edfdb8480","repo":"PHPOffice/PHPWord","slug":"no-phpword-assigned-content","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/EPub3/Part/Content.php","lineNumber":65,"sourceCode":"     *\n     * @return XMLWriter\n     */\n    protected function getXmlWriter()\n    {\n        $xmlWriter = new XMLWriter();\n        $xmlWriter->openMemory();\n        $xmlWriter->startDocument('1.0', 'UTF-8');\n\n        return $xmlWriter;\n    }\n\n    /**\n     * Write part content.\n     */\n    public function write(): string\n    {\n        if ($this->phpWord === null) {\n            throw new Exception('No PhpWord assigned.');\n        }\n\n        $xmlWriter = $this->getXmlWriter();\n        $docInfo = $this->phpWord->getDocInfo();\n\n        // Write package\n        $xmlWriter->startElement('package');\n        $xmlWriter->writeAttribute('xmlns', 'http://www.idpf.org/2007/opf');\n        $xmlWriter->writeAttribute('version', '3.0');\n        $xmlWriter->writeAttribute('unique-identifier', 'book-id');\n        $xmlWriter->writeAttribute('xml:lang', 'en');\n\n        // Write metadata\n        $xmlWriter->startElement('metadata');\n        $xmlWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');\n        $xmlWriter->writeAttribute('xmlns:opf', 'http://www.idpf.org/2007/opf');\n\n        // Required elements","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Writer/EPub3/Part/Content.php#L47-L83","documentation":"EPub3 Part\\Content::write() serializes package metadata that requires the PhpWord document. If the phpWord property is still null at write time (part written standalone or writer constructed without a document), the part refuses to produce content and throws.","triggerScenarios":"Instantiating the Content part directly and calling write() without setting phpWord; using the EPub3 writer without a document; custom pipelines that build parts manually and skip setPhpWord().","commonSituations":"Unit tests or custom writers instantiating Part\\Content in isolation; forgetting setPhpWord() after manual construction; refactored code paths that bypass the standard writer flow.","solutions":["Set the document on the part via setPhpWord($phpWord) before calling write().","Use the normal EPub3 writer flow (IOFactory::createWriter($phpWord, 'EPub3')) which assigns parts automatically.","Ensure the PhpWord instance passed is not null at call time.","In custom pipelines, assert the part is fully configured before writing."],"exampleFix":"// before\n$part = new \\PhpOffice\\PhpWord\\Writer\\EPub3\\Part\\Content();\n$xml = $part->write(); // No PhpWord assigned\n// after\n$part = new \\PhpOffice\\PhpWord\\Writer\\EPub3\\Part\\Content();\n$part->setPhpWord(new \\PhpOffice\\PhpWord\\PhpWord());\n$xml = $part->write();","handlingStrategy":"type-guard","validationCode":"function partIsWritable(\\PhpOffice\\PhpWord\\Writer\\EPub3\\Part\\Content $part): bool {\n    $ref = new ReflectionProperty(get_class($part), 'phpWord');\n    $ref->setAccessible(true);\n    return $ref->getValue($part) !== null;\n}","typeGuard":null,"tryCatchPattern":"try {\n    $xml = $part->write();\n} catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) {\n    if ($e->getMessage() === 'No PhpWord assigned.') {\n        $part->setPhpWord($phpWord);\n        $xml = $part->write();\n    } else { throw $e; }\n}","preventionTips":["Do not instantiate EPub3 parts directly; let the EPub3 writer wire them.","Always call setPhpWord() immediately after manual part construction.","Assert the document is set in custom writer pipelines before write().","Cover part usage in tests that run against a real PhpWord instance."],"tags":["phpword","epub3","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"}