PHPOffice/PHPWord · error · InvalidArgumentException

Type must be "start" or "end"

Error message

Type must be "start" or "end"

What it means

AbstractPart::setCommentReference validates that a comment reference type is either 'start' or 'end'. Any other value found in commentRangeStart/commentRangeEnd parsing is rejected with this InvalidArgumentException.

Solutions

  1. Update PhpWord in case newer versions handle the element
  2. Inspect the document's commentRangeStart/End markup for anomalies and fix the source document
  3. Re-save the document in Word to normalize the XML
  4. Wrap load() in try-catch if comments are optional for your workflow

Example fix

// before
$type = $element->nodeName; // e.g. 'commentRangeFoo'
$this->setCommentReference($type, $id, $run);
// after
if (in_array($type, ['commentRangeStart', 'commentRangeEnd'], true)) {
    $this->setCommentReference($type === 'commentRangeStart' ? 'start' : 'end', $id, $run);
}
Defensive patterns

Strategy: try-catch

Type guard

function isCommentRangeType(string $t): bool { return $t === 'start' || $t === 'end'; }

Try / catch

try { $phpWord = \PhpOffice\PhpWord\IOFactory::load($docFile); } catch (\InvalidArgumentException $e) { if (str_contains($e->getMessage(), 'Type must be')) { // treat as malformed comments, continue without them } throw $e; }

Prevention

When it happens

Trigger: Parsing a document.xml containing a comment range element whose local name is neither commentRangeStart nor commentRangeEnd, so $type is unexpected when readRun dispatches to setCommentReference.

Common situations: Malformed or non-standard comment markup from third-party docx generators; corrupted document.xml; future OOXML elements mis-detected as comment ranges.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14). Data as JSON: /api/errors/6159dc11a5de88f8. Report an issue: GitHub.

Appendix: source

Thrown at src/PhpWord/Reader/Word2007/AbstractPart.php:159

    /**
     * Set comment references.
     *
     * @param array<string, array<string, null|AbstractElement>> $commentRefs
     */
    public function setCommentReferences(array $commentRefs): self
    {
        $this->commentRefs = $commentRefs;

        return $this;
    }

    /**
     * Set comment reference.
     */
    private function setCommentReference(string $type, string $id, AbstractElement $element): self
    {
        if (!in_array($type, ['start', 'end'])) {
            throw new InvalidArgumentException('Type must be "start" or "end"');
        }

        if (!array_key_exists($id, $this->commentRefs)) {
            $this->commentRefs[$id] = [
                'start' => null,
                'end' => null,
            ];
        }
        $this->commentRefs[$id][$type] = $element;

        return $this;
    }

    /**
     * Get comment reference.
     *
     * @return array<string, null|AbstractElement>
     */

View on GitHub (pinned to aef95c0415)