{"record":{"id":"49a1515841c6eaf6","repo":"PHPOffice/PHPWord","slug":"no-parent-writerinterface-assigned","errorCode":null,"errorMessage":"No parent WriterInterface assigned.","messagePattern":"No parent WriterInterface assigned\\.","errorType":"exception","errorClass":"PhpOffice\\PhpWord\\Exception\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Writer/HTML/Part/AbstractPart.php","lineNumber":53,"sourceCode":"     * @return string\n     */\n    abstract public function write();\n\n    public function setParentWriter(?HTML $writer = null): void\n    {\n        $this->parentWriter = $writer;\n    }\n\n    /**\n     * @return HTML\n     */\n    public function getParentWriter()\n    {\n        if ($this->parentWriter !== null) {\n            return $this->parentWriter;\n        }\n\n        throw new Exception('No parent WriterInterface assigned.');\n    }\n}\n","sourceCodeStart":35,"sourceCodeEnd":56,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Writer/HTML/Part/AbstractPart.php#L35-L56","documentation":"HTML writer parts (AbstractPart subclasses) need a reference to their parent writer to access styles, settings, and the document. getParentWriter() throws when the parentWriter property was never injected, meaning the part cannot resolve dependencies during write().","triggerScenarios":"Instantiating an HTML part manually and calling write()/getElementStyle() without setParentWriter(); constructing parts in custom code that bypasses the HTML writer's wiring flow.","commonSituations":"Custom HTML export pipelines assembling parts manually; unit tests instantiating parts directly; overridden writer subclasses forgetting to propagate the parent writer to parts.","solutions":["Call setParentWriter($htmlWriter) on the part before writing.","Let the standard HTML writer construct and wire its parts instead of instantiating them manually.","In subclasses, ensure the parent writer is passed through when parts are created.","Guard custom factories with a null check on parentWriter before invoking write."],"exampleFix":"// before\n$part = new \\PhpOffice\\PhpWord\\Writer\\HTML\\Part\\Body();\n$html = $part->write(); // No parent WriterInterface assigned\n// after\n$writer = new \\PhpOffice\\PhpWord\\Writer\\HTML(new \\PhpOffice\\PhpWord\\PhpWord());\n$part = new \\PhpOffice\\PhpWord\\Writer\\HTML\\Part\\Body();\n$part->setParentWriter($writer);\n$html = $part->write();","handlingStrategy":"type-guard","validationCode":"function partHasParent(\\PhpOffice\\PhpWord\\Writer\\HTML\\Part\\AbstractPart $part): bool {\n    $ref = new ReflectionProperty(get_class($part), 'parentWriter');\n    $ref->setAccessible(true);\n    return $ref->getValue($part) !== null;\n}","typeGuard":null,"tryCatchPattern":"try {\n    $html = $part->write();\n} catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) {\n    if ($e->getMessage() === 'No parent WriterInterface assigned.') {\n        $part->setParentWriter($htmlWriter);\n        $html = $part->write();\n    } else { throw $e; }\n}","preventionTips":["Use the HTML writer's own part-wiring flow instead of manual part construction.","Call setParentWriter() right after instantiating any HTML part manually.","In writer subclasses, propagate the parent writer to every part created.","Add a dev-time assertion calling getParentWriter() before heavy write work."],"tags":["phpword","html","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"}