{"record":{"id":"6fff0f2c96b51bc4","repo":"moodle/moodle","slug":"unexpected-array-or-string","errorCode":null,"errorMessage":"Unexpected array or string","messagePattern":"Unexpected array or string","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Calculation\\Exception","httpStatus":null,"severity":"error","filePath":"public/lib/phpspreadsheet/phpspreadsheet/src/PhpSpreadsheet/Calculation/Engineering/BesselK.php","lineNumber":75,"sourceCode":"    private static function calculate(float $x, int $ord): float\n    {\n        return match ($ord) {\n            0 => self::besselK0($x),\n            1 => self::besselK1($x),\n            default => self::besselK2($x, $ord),\n        };\n    }\n\n    /**\n     * Mollify Phpstan.\n     *\n     * @codeCoverageIgnore\n     */\n    private static function callBesselI(float $x, int $ord): float\n    {\n        $rslt = BesselI::BESSELI($x, $ord);\n        if (!is_float($rslt)) {\n            throw new Exception('Unexpected array or string');\n        }\n\n        return $rslt;\n    }\n\n    private static function besselK0(float $x): float\n    {\n        if ($x <= 2) {\n            $fNum2 = $x * 0.5;\n            $y = ($fNum2 * $fNum2);\n\n            return -log($fNum2) * self::callBesselI($x, 0)\n                + (-0.57721566 + $y * (0.42278420 + $y * (0.23069756 + $y * (0.3488590e-1 + $y * (0.262698e-2 + $y\n                                    * (0.10750e-3 + $y * 0.74e-5))))));\n        }\n\n        $y = 2 / $x;\n","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/phpspreadsheet/phpspreadsheet/src/PhpSpreadsheet/Calculation/Engineering/BesselK.php#L57-L93","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n$k = Engineering::BESSELK(0, 1); // throws 'Unexpected array or string'\n// after\n$x = 1e-9; // clamp to a tiny positive value instead of 0\n$k = $x > 0 ? Engineering::BESSELK($x, 1) : ExcelError::NAN();","handlingStrategy":"validation","validationCode":"if (!is_numeric($x) || (float) $x <= 0) {\n    throw new \\InvalidArgumentException('BESSELK requires x > 0, got: ' . var_export($x, true));\n}\nif (!is_int($ord) || $ord < 0) {\n    throw new \\InvalidArgumentException('BESSELK order must be a non-negative integer');\n}","typeGuard":"function isValidBesselInput(mixed $x, mixed $ord): bool {\n    return is_numeric($x) && (float) $x > 0 && is_int($ord) && $ord >= 0;\n}","tryCatchPattern":"try {\n    $result = Engineering::BESSELK($x, $ord);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Calculation\\Exception $e) {\n    $result = ExcelError::NAN(); // or log and rethrow\n}","preventionTips":["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."],"tags":["phpspreadsheet","bessel","math","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}