{"record":{"id":"285086281a07a3cb","repo":"PHPOffice/PHPWord","slug":"comment-with-id-s-isn-t-referenced-in-document","errorCode":null,"errorMessage":"Comment with id %s isn't referenced in document","messagePattern":"Comment with id (.+?) isn't referenced in document","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Reader/Word2007/AbstractPart.php","lineNumber":181,"sourceCode":"            $this->commentRefs[$id] = [\n                'start' => null,\n                'end' => null,\n            ];\n        }\n        $this->commentRefs[$id][$type] = $element;\n\n        return $this;\n    }\n\n    /**\n     * Get comment reference.\n     *\n     * @return array<string, null|AbstractElement>\n     */\n    protected function getCommentReference(string $id): array\n    {\n        if (!array_key_exists($id, $this->commentRefs)) {\n            throw new InvalidArgumentException(sprintf('Comment with id %s isn\\'t referenced in document', $id));\n        }\n\n        return $this->commentRefs[$id];\n    }\n\n    /**\n     * Read w:p.\n     *\n     * @param AbstractContainer $parent\n     * @param string $docPart\n     *\n     * @todo Get font style for preserve text\n     */\n    protected function readParagraph(XMLReader $xmlReader, DOMElement $domNode, $parent, $docPart = 'document'): void\n    {\n        // Paragraph style\n        $paragraphStyle = $xmlReader->elementExists('w:pPr', $domNode) ? $this->readParagraphStyle($xmlReader, $domNode) : null;\n","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Reader/Word2007/AbstractPart.php#L163-L199","documentation":"AbstractPart::getCommentReference looks up a comment id in the collected commentRefs map. This exception means the document references a comment (commentReference id) whose range start/end was never recorded, so the reference cannot be resolved.","triggerScenarios":"A document.xml contains <w:commentReference w:id=\"X\"/> (or getCommentReference is called during read) but no commentRangeStart/End with id X was parsed — e.g. a dangling comment reference after comment ranges were stripped or the comment part is missing.","commonSituations":"Documents edited by tools that delete comment ranges but leave references; manually trimmed XML; docx produced by generators with inconsistent comment markup.","solutions":["Open the document in Word, resolve/delete the broken comments, and re-save","Inspect word/document.xml for commentReference ids lacking matching commentRangeStart/End","Strip comment markup from the XML before processing if comments are not needed","Catch the InvalidArgumentException and skip comment handling"],"exampleFix":"// before\n$commentRefs = $part->getCommentReference($id); // throws if unresolved\n// after\ntry {\n    $commentRefs = $part->getCommentReference($id);\n} catch (\\InvalidArgumentException $e) {\n    $commentRefs = ['start' => null, 'end' => null]; // tolerate dangling refs\n}","handlingStrategy":"try-catch","validationCode":"$xml = $zip->getFromName('word/document.xml'); preg_match_all('/commentReference w:id=\"(\\d+)\"/', $xml, $refs); preg_match_all('/commentRangeStart w:id=\"(\\d+)\"/', $xml, $starts); $dangling = array_diff($refs[1], $starts[1]); // non-empty means broken comment refs","typeGuard":null,"tryCatchPattern":"try { $phpWord = \\PhpOffice\\PhpWord\\IOFactory::load($docFile); } catch (\\InvalidArgumentException $e) { if (str_contains($e->getMessage(), 'isn\\'t referenced')) { // resave document or strip comments } throw $e; }","preventionTips":["Clean up comments in Word before pipeline processing","Detect dangling comment references during document intake","If comments are irrelevant, remove comment markup before parsing"],"tags":["phpword","word2007","comments","reference-resolution"],"backgroundTag":"entity-not-found","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"}