moodle/moodle · error · PhpOffice\PhpSpreadsheet\Calculation\Exception
Unexpected array or string
Error message
Unexpected array or string
What it means
Thrown by BesselK's private callBesselI() helper when BesselI::BESSELI() returns something other than a float. BESSELI can legitimately return a string error value (e.g. '#NUM!' for x <= 0 or negative order) or an array in some error paths, and the wrapper demands a strict float to continue the Bessel-K computation. It is an internal type-contract violation surfacing an underlying Bessel-I evaluation failure, most commonly a non-positive x passed to BESSELI/K.
Solutions
- Ensure the x argument is a positive number (> 0) before calling BESSELK; Bessel-K is only defined for x > 0 in this implementation.
- Validate the order argument is a non-negative integer within the supported range.
- Wrap the Engineering::BESSELK call in try/catch for Calculation\Exception and substitute an Excel error or default.
- If x comes from a cell reference, coerce/validate the cell value numerically before evaluating the formula.
Example fix
// before $k = Engineering::BESSELK(0, 1); // throws 'Unexpected array or string' // after $x = 1e-9; // clamp to a tiny positive value instead of 0 $k = $x > 0 ? Engineering::BESSELK($x, 1) : ExcelError::NAN();
Defensive patterns
Strategy: validation
Validate before calling
if (!is_numeric($x) || (float) $x <= 0) {
throw new \InvalidArgumentException('BESSELK requires x > 0, got: ' . var_export($x, true));
}
if (!is_int($ord) || $ord < 0) {
throw new \InvalidArgumentException('BESSELK order must be a non-negative integer');
} Type guard
function isValidBesselInput(mixed $x, mixed $ord): bool {
return is_numeric($x) && (float) $x > 0 && is_int($ord) && $ord >= 0;
} Try / catch
try {
$result = Engineering::BESSELK($x, $ord);
} catch (\PhpOffice\PhpSpreadsheet\Calculation\Exception $e) {
$result = ExcelError::NAN(); // or log and rethrow
} Prevention
- Always guard x > 0 before Bessel functions (BESSELI/K are undefined at x <= 0 here).
- Validate the order is a non-negative integer accepted by the implementation.
- Treat cell-derived values as suspect: coerce and range-check before formula evaluation.
- Catch PhpSpreadsheet Calculation\Exception at formula-evaluation boundaries and map to Excel error values.
When it happens
Trigger: Calling Engineering::BESSELK() with a numeric x <= 0 (BesselI returns the error string '#NUM!') or with a negative/invalid order ord; also any path where BESSELI returns an array error matrix.
Common situations: Spreadsheet formulas like =BESSELK(0, 1) or =BESSELK(-2, 1) referencing zero or negative x values imported from user data; formulas computed against cell ranges where a boundary value dropped to 0 or negative.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Unexpected array or string
- Column is not within the autofilter range.
- Conditional is not a Cell Value CF Rule conditional
- Conditional is not a Date Value CF Rule conditional
- Conditional is not a Duplicates CF Rule conditional
AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14).
Data as JSON: /api/errors/6fff0f2c96b51bc4.
Report an issue: GitHub.
Appendix: source
Thrown at public/lib/phpspreadsheet/phpspreadsheet/src/PhpSpreadsheet/Calculation/Engineering/BesselK.php:75
private static function calculate(float $x, int $ord): float
{
return match ($ord) {
0 => self::besselK0($x),
1 => self::besselK1($x),
default => self::besselK2($x, $ord),
};
}
/**
* Mollify Phpstan.
*
* @codeCoverageIgnore
*/
private static function callBesselI(float $x, int $ord): float
{
$rslt = BesselI::BESSELI($x, $ord);
if (!is_float($rslt)) {
throw new Exception('Unexpected array or string');
}
return $rslt;
}
private static function besselK0(float $x): float
{
if ($x <= 2) {
$fNum2 = $x * 0.5;
$y = ($fNum2 * $fNum2);
return -log($fNum2) * self::callBesselI($x, 0)
+ (-0.57721566 + $y * (0.42278420 + $y * (0.23069756 + $y * (0.3488590e-1 + $y * (0.262698e-2 + $y
* (0.10750e-3 + $y * 0.74e-5))))));
}
$y = 2 / $x;
View on GitHub (pinned to 48ea8c3322)