{"record":{"id":"5eca838c1af0e8bb","repo":"moodle/moodle","slug":"the-value-supplied-was-of-type-type-a-numeric-value-was","errorCode":null,"errorMessage":"The value supplied was of type '{$type}'. A numeric value was expected.","messagePattern":"The value supplied was of type '(.+?)'\\. A numeric value was expected\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"public/lib/table/classes/local/filter/numeric_comparison_filter.php","lineNumber":116,"sourceCode":"        }\n\n        if (!property_exists($data, 'direction')) {\n            throw new InvalidArgumentException(\"A 'direction' must be provided.\");\n        }\n        $direction = $this->get_direction($data->direction);\n\n        if (!property_exists($data, 'value')) {\n            throw new InvalidArgumentException(\"A 'value' must be provided.\");\n        }\n        $value = $data->value;\n\n        if (!is_numeric($value)) {\n            $type = gettype($value);\n            if ($type === 'object') {\n                $type = get_class($value);\n            }\n\n            throw new TypeError(\"The value supplied was of type '{$type}'. A numeric value was expected.\");\n        }\n\n        $fullvalue = (object) [\n            'direction' => $direction,\n            'value' => $value,\n        ];\n\n        if (array_search($fullvalue, $this->filtervalues) !== false) {\n            // Remove duplicates.\n            return $this;\n        }\n\n        $this->filtervalues[] = $fullvalue;\n\n        return $this;\n    }\n}\n","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/table/classes/local/filter/numeric_comparison_filter.php#L98-L134","documentation":"thrown by the numeric comparison table filter when the supplied filter value is not numeric. PHP's TypeError is raised after gettype() (or get_class() for objects) reports the actual type so the developer can see what was passed instead of a number.","triggerScenarios":"calling add_filter()/setValues() (or similar) on a numeric_comparison_filter with a value that fails is_numeric(): arrays, null, booleans, non-numeric strings, or objects.","commonSituations":"form input not cast before being passed to the filter; database fields returning null; passing a stdClass value object instead of a scalar; copy-paste from a string filter configuration.","solutions":["Cast or validate the value with is_numeric() before passing it to the filter","Convert numeric strings with (float)$value or (int)$value prior to setValues()","Check the source of the value (form data, DB row) for null/empty defaults and substitute a numeric default","If an object is passed, extract its numeric property instead of passing the object"],"exampleFix":"// before\n$filter->setValues([$row->score]);\n// after\n$score = is_numeric($row->score ?? null) ? (float)$row->score : 0;\n$filter->setValues([$score]);","handlingStrategy":"type-guard","validationCode":"$values = array_map(function ($v) { return is_numeric($v) ? $v + 0 : null; }, $values); if (in_array(null, $values, true)) { throw new InvalidArgumentException('non-numeric filter value'); }","typeGuard":"function assertNumeric($v): float { if (!is_numeric($v)) { throw new InvalidArgumentException('Expected numeric, got ' . (is_object($v) ? get_class($v) : gettype($v))); } return (float)$v; }","tryCatchPattern":"try { $filter->setValues([$value]); } catch (TypeError $e) { // log and skip this filter, or cast and retry\n    $filter->setValues([(float)$value]); }","preventionTips":["Always is_numeric()-check values coming from forms before filtering","Coerce DB scalars explicitly ((float)/(int)) at the data-access boundary","Add unit tests for each filter with wrong-type inputs"],"tags":["php","type-error","table-filter","numeric"],"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"}