PHPOffice/PHPWord · error · InvalidArgumentException
Cannot set a Comment on a Comment
Error message
Cannot set a Comment on a Comment
What it means
AbstractElement::setCommentRangeStart() throws an InvalidArgumentException when the element receiving the comment range start is itself a Comment, since comments cannot contain comment ranges. This prevents self-referential comment structures that would produce invalid OOXML.
Solutions
- Skip Comment instances before calling setCommentRangeStart (check with instanceof)
- Attach comment ranges only to content elements like Text, TextRun, or Table
- Guard with if (!$element instanceof Comment)
Example fix
// before
$element->setCommentRangeStart($comment); // element may be a Comment
// after
if (!$element instanceof \PhpOffice\PhpWord\Element\Comment) {
$element->setCommentRangeStart($comment);
} Defensive patterns
Strategy: type-guard
Validate before calling
if ($element instanceof \PhpOffice\PhpWord\Element\Comment) {
return; // cannot attach comment ranges to comments
} Type guard
function canCarryCommentRange($element): bool {
return !$element instanceof \PhpOffice\PhpWord\Element\Comment;
} Try / catch
try {
$element->setCommentRangeStart($comment);
} catch (\InvalidArgumentException $e) {
// skip comments; optionally log
} Prevention
- Filter Comment instances out of element collections before attaching ranges
- Use the same instanceof guard for setCommentRangeStart and setCommentRangeEnd
- Centralize comment attachment in one helper that performs the guard
When it happens
Trigger: Calling setCommentRangeStart($comment) on a Comment instance — e.g. chaining comment setup code that applies to all elements indiscriminately, or accidentally assigning the comment's own range markers onto another comment object.
Common situations: Iterating over all document elements and attaching comment ranges to each without excluding Comment instances; nested comment workflows; copy-pasted comment-attachment code applied to the comment object itself.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Invalid value, on of ' . implode(', ', $position) . '…
- Invalid value, on of ' . implode(', ', $restartNumbers) . '…
- Invalid value, dirty or clean possible
- Invalid value, alignments of ' . implode(', '…
- Cannot add in .
AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14).
Data as JSON: /api/errors/13a6eb5613430e38.
Report an issue: GitHub.
Appendix: source
Thrown at src/PhpWord/Element/AbstractElement.php:317
/**
* Get comment start.
*/
public function getCommentRangeStart(): ?Comment
{
if ($this->commentsRangeStart != null) {
return $this->commentsRangeStart->getItem($this->commentsRangeStart->countItems());
}
return null;
}
/**
* Set comment start.
*/
public function setCommentRangeStart(Comment $value): void
{
if ($this instanceof Comment) {
throw new InvalidArgumentException('Cannot set a Comment on a Comment');
}
if ($this->commentsRangeStart == null) {
$this->commentsRangeStart = new Comments();
}
// Set ID early to avoid duplicates.
if ($value->getElementId() == null) {
$value->setElementId();
}
foreach ($this->commentsRangeStart->getItems() as $comment) {
if ($value->getElementId() == $comment->getElementId()) {
return;
}
}
$idxItem = $this->commentsRangeStart->addItem($value);
$this->commentsRangeStart->getItem($idxItem)->setStartElement($this);
}
/**View on GitHub (pinned to aef95c0415)