{"record":{"id":"04583983373f3917","repo":"PHPOffice/PhpSpreadsheet","slug":"unknown-trend-type-trendtype","errorCode":null,"errorMessage":"Unknown trend type {$trendType}","messagePattern":"Unknown trend type (.+?)","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Shared/Trend/Trend.php","lineNumber":129,"sourceCode":"                }\n                if ($trendType !== self::TREND_BEST_FIT_NO_POLY) {\n                    foreach (self::$trendTypePolynomialOrders as $trendMethod) {\n                        $order = (int) substr($trendMethod, -1);\n                        $bestFit[$trendMethod] = new PolynomialBestFit($order, $yValues, $xValues);\n                        if ($bestFit[$trendMethod]->getError()) {\n                            unset($bestFit[$trendMethod]);\n                        } else {\n                            $bestFitValue[$trendMethod] = $bestFit[$trendMethod]->getGoodnessOfFit();\n                        }\n                    }\n                }\n                //    Determine which of our Trend lines is the best fit, and then we return the instance of that Trend class\n                arsort($bestFitValue);\n                $bestFitType = key($bestFitValue);\n\n                return $bestFit[$bestFitType];\n            default:\n                throw new SpreadsheetException(\"Unknown trend type $trendType\");\n        }\n    }\n}\n","sourceCodeStart":111,"sourceCodeEnd":133,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Shared/Trend/Trend.php#L111-L133","documentation":"Thrown by Trend::trend() when $trendType matches none of the known constants — the switch's default branch. Valid values are the class constants TREND_LINEAR, TREND_LOGARITHMIC, TREND_EXPONENTIAL, TREND_POWER, TREND_POLYNOMIAL (and the BEST_FIT variants routed through the same switch); anything else, including near-miss strings, is rejected.","triggerScenarios":"Trend::trend('log', ...) or 'exponential ' with a trailing space, 'TREND_LINEAR' passed as the literal constant name, or a type read from unvalidated user/DB/config input. Because the comparison is exact and case-sensitive, casing or whitespace deviations land in the default branch and throw.","commonSituations":"Exposing trend-type selection in an API and forwarding the raw string; copy-paste from documentation that paraphrases the constant names; storing type strings in a column where values drift ('linear' vs 'LINEAR'); refactors that renamed local constants but not stored values.","solutions":["Reference Trend::TREND_* constants instead of string literals: Trend::trend(Trend::TREND_POWER, $y, $x).","Whitelist untrusted input at the boundary against the constant list and reject unknowns with the allowed values in the message.","Trim/normalize incoming strings and re-map friendly names ('linear', 'log') to the constants before calling.","Note TREND_POLYNOMIAL is a valid type that throws its own 'not yet implemented' exception — pick a supported fit type from the start."],"exampleFix":"// before\n$fit = Trend::trend('log', $yValues, $xValues); // Unknown trend type log\n\n// after\nuse PhpOffice\\PhpSpreadsheet\\Shared\\Trend\\Trend;\n$map = ['linear' => Trend::TREND_LINEAR, 'log' => Trend::TREND_LOGARITHMIC,\n        'exp' => Trend::TREND_EXPONENTIAL, 'power' => Trend::TREND_POWER];\n$type = $map[$request->string('trend')] ?? throw new InvalidArgumentException('Unsupported trend');\n$fit = Trend::trend($type, $yValues, $xValues);","handlingStrategy":"validation","validationCode":"use PhpOffice\\PhpSpreadsheet\\Shared\\Trend\\Trend;\n$allowed = [Trend::TREND_LINEAR, Trend::TREND_LOGARITHMIC, Trend::TREND_EXPONENTIAL, Trend::TREND_POWER];\nif (!in_array($trendType, $allowed, true)) {\n    throw new InvalidArgumentException('Unknown trend type: ' . $trendType . '. Allowed: ' . implode(', ', $allowed));\n}\n$fit = Trend::trend($trendType, $yValues, $xValues);","typeGuard":"function trendTypeOrThrow(string $type): string\n{\n    return match ($type) {\n        'linear' => Trend::TREND_LINEAR,\n        'logarithmic' => Trend::TREND_LOGARITHMIC,\n        'exponential' => Trend::TREND_EXPONENTIAL,\n        'power' => Trend::TREND_POWER,\n        default => throw new InvalidArgumentException(\"Unknown trend type $type\"),\n    };\n}","tryCatchPattern":"try { $fit = Trend::trend($trendType, $yValues, $xValues); }\ncatch (\\PhpOffice\\PhpSpreadsheet\\Exception $e) {\n    if (str_contains($e->getMessage(), 'Unknown trend type')) {\n        $fit = Trend::trend(Trend::TREND_LINEAR, $yValues, $xValues); // or rethrow with allowed list\n    } else { throw $e; }\n}","preventionTips":["Pass Trend::TREND_* constants, never free-form strings from users, config, or DB.","Whitelist and remap friendly names at the API boundary; include the allowed list in error messages.","Watch casing/whitespace: the switch match is exact and case-sensitive."],"tags":["trend-analysis","invalid-enum","validation","phpspreadsheet"],"backgroundTag":"invalid-enum-value","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}