PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

Unknown trend type {$trendType}

Error message

Unknown trend type {$trendType}

What it means

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.

Source

Thrown at src/PhpSpreadsheet/Shared/Trend/Trend.php:129

                }
                if ($trendType !== self::TREND_BEST_FIT_NO_POLY) {
                    foreach (self::$trendTypePolynomialOrders as $trendMethod) {
                        $order = (int) substr($trendMethod, -1);
                        $bestFit[$trendMethod] = new PolynomialBestFit($order, $yValues, $xValues);
                        if ($bestFit[$trendMethod]->getError()) {
                            unset($bestFit[$trendMethod]);
                        } else {
                            $bestFitValue[$trendMethod] = $bestFit[$trendMethod]->getGoodnessOfFit();
                        }
                    }
                }
                //    Determine which of our Trend lines is the best fit, and then we return the instance of that Trend class
                arsort($bestFitValue);
                $bestFitType = key($bestFitValue);

                return $bestFit[$bestFitType];
            default:
                throw new SpreadsheetException("Unknown trend type $trendType");
        }
    }
}

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Reference Trend::TREND_* constants instead of string literals: Trend::trend(Trend::TREND_POWER, $y, $x).
  2. Whitelist untrusted input at the boundary against the constant list and reject unknowns with the allowed values in the message.
  3. Trim/normalize incoming strings and re-map friendly names ('linear', 'log') to the constants before calling.
  4. Note TREND_POLYNOMIAL is a valid type that throws its own 'not yet implemented' exception — pick a supported fit type from the start.

Example fix

// before
$fit = Trend::trend('log', $yValues, $xValues); // Unknown trend type log

// after
use PhpOffice\PhpSpreadsheet\Shared\Trend\Trend;
$map = ['linear' => Trend::TREND_LINEAR, 'log' => Trend::TREND_LOGARITHMIC,
        'exp' => Trend::TREND_EXPONENTIAL, 'power' => Trend::TREND_POWER];
$type = $map[$request->string('trend')] ?? throw new InvalidArgumentException('Unsupported trend');
$fit = Trend::trend($type, $yValues, $xValues);
Defensive patterns

Strategy: validation

Validate before calling

use PhpOffice\PhpSpreadsheet\Shared\Trend\Trend;
$allowed = [Trend::TREND_LINEAR, Trend::TREND_LOGARITHMIC, Trend::TREND_EXPONENTIAL, Trend::TREND_POWER];
if (!in_array($trendType, $allowed, true)) {
    throw new InvalidArgumentException('Unknown trend type: ' . $trendType . '. Allowed: ' . implode(', ', $allowed));
}
$fit = Trend::trend($trendType, $yValues, $xValues);

Type guard

function trendTypeOrThrow(string $type): string
{
    return match ($type) {
        'linear' => Trend::TREND_LINEAR,
        'logarithmic' => Trend::TREND_LOGARITHMIC,
        'exponential' => Trend::TREND_EXPONENTIAL,
        'power' => Trend::TREND_POWER,
        default => throw new InvalidArgumentException("Unknown trend type $type"),
    };
}

Try / catch

try { $fit = Trend::trend($trendType, $yValues, $xValues); }
catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
    if (str_contains($e->getMessage(), 'Unknown trend type')) {
        $fit = Trend::trend(Trend::TREND_LINEAR, $yValues, $xValues); // or rethrow with allowed list
    } else { throw $e; }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17). Data as JSON: /api/errors/04583983373f3917. Report an issue: GitHub.