{"record":{"id":"2456efe4a2bc951f","repo":"PHPOffice/PHPWord","slug":"line-height-must-be-a-valid-number","errorCode":null,"errorMessage":"Line height must be a valid number","messagePattern":"Line height must be a valid number","errorType":"validation","errorClass":"InvalidStyleException","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Style/Paragraph.php","lineNumber":599,"sourceCode":"    {\n        return $this->lineHeight;\n    }\n\n    /**\n     * Set the line height.\n     *\n     * @param float|int|string $lineHeight\n     *\n     * @return self\n     */\n    public function setLineHeight($lineHeight)\n    {\n        if (is_string($lineHeight)) {\n            $lineHeight = (float) (preg_replace('/[^0-9\\.\\,]/', '', $lineHeight));\n        }\n\n        if ((!is_int($lineHeight) && !is_float($lineHeight)) || !$lineHeight) {\n            throw new InvalidStyleException('Line height must be a valid number');\n        }\n\n        $this->lineHeight = $lineHeight;\n        $this->setSpacing(($lineHeight - 1) * self::LINE_HEIGHT);\n        $this->setSpacingLineRule(\\PhpOffice\\PhpWord\\SimpleType\\LineSpacingRule::AUTO);\n\n        return $this;\n    }\n\n    /**\n     * Get allow first/last line to display on a separate page setting.\n     *\n     * @return bool\n     */\n    public function hasWidowControl()\n    {\n        return $this->widowControl;\n    }","sourceCodeStart":581,"sourceCodeEnd":617,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Style/Paragraph.php#L581-L617","documentation":"PhpWord\\Style\\Paragraph::setLineHeight accepts only a positive int/float line-height. Strings are stripped of non-numeric characters and cast to float; if the resulting value is not numeric or is falsy (0, 0.0, empty after stripping), InvalidStyleException('Line height must be a valid number') is thrown.","triggerScenarios":"setLineHeight('abc'), setLineHeight(''), setLineHeight(0), or a string containing no digits (e.g. 'px', '1.5x' is OK because 1.5 survives stripping but 'auto' becomes 0 and throws).","commonSituations":"Passing CSS-like values ('1.5em', 'auto') or localized numbers with commas stripped to 0; user-supplied config with a non-numeric line-height; confusion with the CSS line-height property.","solutions":["Pass a plain number, e.g. setLineHeight(1.5) for 150% spacing","Pre-clean the string yourself and verify it is > 0 before calling","For 'auto' spacing, use spacingLineRule AUTO via setSpacingLineRule instead of setLineHeight"],"exampleFix":"// before\n$paragraph->setLineHeight('1.5em'); // may strip to 1.5 OK, but 'auto' throws\n// after\n$paragraph->setLineHeight(1.5);","handlingStrategy":"type-guard","validationCode":"function assertLineHeight($v): float {\n    $f = is_string($v) ? (float)preg_replace('/[^0-9\\.]/', '', $v) : (float)$v;\n    if ($f <= 0) throw new InvalidArgumentException('lineHeight must be a positive number');\n    return $f;\n}\n$paragraph->setLineHeight(assertLineHeight($config['lineHeight']));","typeGuard":"function isNumericLineHeight($v): bool {\n    return (is_int($v) || is_float($v)) && $v > 0;\n}","tryCatchPattern":"try {\n    $paragraph->setLineHeight($value);\n} catch (\\PhpOffice\\PhpWord\\Exception\\InvalidStyleException $e) {\n    $paragraph->setLineHeight(1.0); // single spacing default\n}","preventionTips":["Pass numeric values, not CSS strings like '1.5em' or 'auto'","Cast user config with (float) and validate > 0 before setting","Replace 'auto' behavior with setSpacingLineRule(LineSpacingRule::AUTO)"],"tags":["validation","style","numeric"],"backgroundTag":"invalid-argument-value","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"}