{"record":{"id":"9f13b7370095d61b","repo":"PHPOffice/PhpSpreadsheet","slug":"unitofmeasure-is-not-a-vaid-unit-of-measure","errorCode":null,"errorMessage":"{$unitOfMeasure} is not a vaid unit of measure","messagePattern":"(.+?) is not a vaid unit of measure","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Helper/Dimension.php","lineNumber":95,"sourceCode":"    public function width(): float\n    {\n        return (float) ($this->unit === null)\n            ? $this->size\n            : round(Drawing::pixelsToCellDimension((int) $this->size, new Font(false)), 4);\n    }\n\n    public function height(): float\n    {\n        return (float) ($this->unit === null)\n            ? $this->size\n            : $this->toUnit(self::UOM_POINTS);\n    }\n\n    public function toUnit(string $unitOfMeasure): float\n    {\n        $unitOfMeasure = strtolower($unitOfMeasure);\n        if (!array_key_exists($unitOfMeasure, self::ABSOLUTE_UNITS)) {\n            throw new Exception(\"{$unitOfMeasure} is not a vaid unit of measure\");\n        }\n\n        $size = $this->size;\n        if ($this->unit === null) {\n            $size = Drawing::cellDimensionToPixels($size, new Font(false));\n        }\n\n        return $size / self::ABSOLUTE_UNITS[$unitOfMeasure];\n    }\n}\n","sourceCodeStart":77,"sourceCodeEnd":106,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Helper/Dimension.php#L77-L106","documentation":"PhpSpreadsheet's Dimension helper converts drawing/cell dimensions between units via toUnit(). The target unit is lowercased and looked up in the fixed ABSOLUTE_UNITS map (cm, mm, in, px, pt, pc — all relative to 96 dpi); any string not in that map throws this exception. Relative/CSS units like '%', 'em', or synonyms like 'inch'/'points' are not accepted (the message even contains an upstream typo, 'vaid').","triggerScenarios":"Calling $dimension->toUnit('inch'), toUnit('%'), toUnit('em'), or piping a raw user-supplied/CSS unit string into toUnit()/width()/height() instead of one of the Dimension::UOM_* constants (UOM_CENTIMETERS, UOM_MILLIMETERS, UOM_INCHES, UOM_PIXELS, UOM_POINTS, UOM_PICA).","commonSituations":"Building image dimensions from HTML attributes or UI dropdowns where units arrive as 'inches', 'pts', or '%'; assuming the helper accepts the full CSS unit vocabulary; case is handled (strtolower) but spelling is not.","solutions":["Pass a Dimension::UOM_* constant instead of a raw string","Normalize external input before calling: lowercase it and map synonyms (inch→in, points→pt) onto the six accepted keys","If you accept arbitrary units, validate against array keys of Dimension::ABSOLUTE_UNITS and reject early with your own error message"],"exampleFix":"// before\n$d = new Dimension('1.5in');\n$px = $d->toUnit('inch'); // throws: 'inch' is not in ABSOLUTE_UNITS\n\n// after\n$d = new Dimension('1.5in');\n$px = $d->toUnit(Dimension::UOM_PIXELS); // 144.0","handlingStrategy":"validation","validationCode":"$unit = strtolower(trim($inputUnit));\nif (!array_key_exists($unit, Dimension::ABSOLUTE_UNITS)) {\n    throw new InvalidArgumentException(\"Unsupported unit '$inputUnit'; expected one of: \" . implode(', ', array_keys(Dimension::ABSOLUTE_UNITS)));\n}\nreturn $dimension->toUnit($unit);","typeGuard":"function isSupportedDimensionUnit(string $unit): bool\n{\n    return array_key_exists(strtolower(trim($unit)), Dimension::ABSOLUTE_UNITS);\n}","tryCatchPattern":null,"preventionTips":["Whitelist units at your API boundary; never pass raw user strings to toUnit()","Map common synonyms once (inch→in, points→pt, pixels→px) before calling","Add a unit-vocabulary regression test alongside the Dimension constants so new spellings fail loudly"],"tags":["dimension","unit-conversion","drawing","invalid-argument"],"backgroundTag":"invalid-unit-of-measure","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}