{"record":{"id":"c2abb4b9ed1c95e7","repo":"moodle/moodle","slug":"multiplication-arguments-must-be-matrix-or-array","errorCode":null,"errorMessage":"Multiplication arguments must be Matrix or array","messagePattern":"Multiplication arguments must be Matrix or array","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"public/lib/phpspreadsheet/markbaker/matrix/classes/src/Operations.php","lineNumber":122,"sourceCode":"            $result->execute($matrix);\n        }\n\n        return $result->result();\n    }\n\n    public static function multiply(...$matrixValues): Matrix\n    {\n        if (count($matrixValues) < 2) {\n            throw new Exception('Multiplication operation requires at least 2 arguments');\n        }\n\n        $matrix = array_shift($matrixValues);\n\n        if (is_array($matrix)) {\n            $matrix = new Matrix($matrix);\n        }\n        if (!$matrix instanceof Matrix) {\n            throw new Exception('Multiplication arguments must be Matrix or array');\n        }\n\n        $result = new Multiplication($matrix);\n\n        foreach ($matrixValues as $matrix) {\n            $result->execute($matrix);\n        }\n\n        return $result->result();\n    }\n\n    public static function subtract(...$matrixValues): Matrix\n    {\n        if (count($matrixValues) < 2) {\n            throw new Exception('Subtraction operation requires at least 2 arguments');\n        }\n\n        $matrix = array_shift($matrixValues);","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/phpspreadsheet/markbaker/matrix/classes/src/Operations.php#L104-L140","documentation":"Matrix\\Operations::multiply() requires every operand to be a Matrix object or a 2-D PHP array that can be wrapped in a Matrix. The FIRST argument is validated after an optional array-to-Matrix conversion; if it is neither (e.g. a scalar, string, null, or object of another class), the library throws this Exception instead of attempting an invalid multiplication. Only the first operand hits this check — later operands are validated inside Multiplication::execute().","triggerScenarios":"Calling Operations::multiply() (or the multiply() helper function) where the first argument is not a Matrix and not an array: e.g. multiply(5, $m), multiply('3', $a, $b), multiply(null, $m), multiply($someOtherObject, $m), or a first argument that is a 1-D/non-rectangular structure accepted as an array but note arrays are auto-wrapped — the failure is specifically non-array, non-Matrix values.","commonSituations":"Developers pass a scalar because they think multiply() does element-wise scalar multiplication (it only does matrix multiplication); a spreadsheet cell value read as string/null is fed in as the first operand; a function returns false/null on error and its result is passed straight through; mixing up argument order so a number lands first.","solutions":["Ensure the first argument is a Matrix instance or a rectangular 2-D array, e.g. Operations::multiply(new Matrix($a), $b) or Operations::multiply($aArray, $bArray)","If you intend to scale a matrix by a scalar, use Matrix::multiply($scalar) or directMatrix/scale operations instead of Operations::multiply","Validate/convert cell values from spreadsheets before passing: cast numeric strings with (float) and reject null/false","Inspect the actual runtime type with gettype()/var_dump if unsure what the first operand is"],"exampleFix":"// before\n$result = \\Matrix\\Operations::multiply(2, $matrixA); // throws\n// after\n$result = $matrixA->multiply(2);          // scalar scaling on the Matrix object\n// or, for matrix products:\n$result = \\Matrix\\Operations::multiply($arrayA, $arrayB);","handlingStrategy":"type-guard","validationCode":"// PHP\nfunction assertMatrixOperand($v): \\Matrix\\Matrix {\n    if ($v instanceof \\Matrix\\Matrix) return $v;\n    if (is_array($v)) return new \\Matrix\\Matrix($v);\n    throw new \\InvalidArgumentException('Expected Matrix or 2-D array, got ' . gettype($v));\n}\nassertMatrixOperand($first);","typeGuard":"function isMatrixOperand($v): bool {\n    return $v instanceof \\Matrix\\Matrix || (is_array($v) && count($v) > 0 && is_array($v[0]));\n}","tryCatchPattern":"try {\n    $result = \\Matrix\\Operations::multiply($a, $b);\n} catch (\\Matrix\\Exception $e) {\n    log_error('Bad multiply operand: ' . $e->getMessage());\n    $result = null; // or rethrow after adding operand context\n}","preventionTips":["Always pass Matrix objects or rectangular 2-D arrays as the first operand","Use Matrix::multiply() for scalar scaling instead of Operations::multiply","Sanitize spreadsheet/JSON values (cast numeric strings, reject null/false) before matrix calls","Unit-test operand edge cases (scalars, null, wrong class)"],"tags":["php","matrix","type-error","argument-validation"],"backgroundTag":"invalid-argument-value","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"}