{"record":{"id":"15653447e2991675","repo":"phalcon/cphalcon","slug":"value-must-be-of-type-type-actual-given","errorCode":null,"errorMessage":"Value must be of type '{type}', '{actual}' given","messagePattern":"Value must be of type '(.+?)', '(.+?)' given","errorType":"exception","errorClass":"Phalcon\\Support\\Collection\\Exceptions\\InvalidValueType","httpStatus":null,"severity":"error","filePath":"phalcon/Support/Collection.zep","lineNumber":765,"sourceCode":"                break;\n            case \"bool\":\n                let ok = is_bool(value);\n                break;\n            case \"float\":\n                let ok = is_float(value);\n                break;\n            case \"array\":\n                let ok = is_array(value);\n                break;\n            case \"object\":\n                let ok = is_object(value);\n                break;\n            default:\n                let ok = (value instanceof this->type);\n        }\n\n        if (!ok) {\n            throw new InvalidValueType(this->type, value);\n        }\n    }\n\n    /**\n     * @param mixed $value\n     */\n    private function checkSerializable(var value) -> mixed\n    {\n        if (\n            typeof value === \"object\" &&\n            true === method_exists(value, \"jsonSerialize\")\n        ) {\n            return value->jsonSerialize();\n        }\n\n        return value;\n    }\n}","sourceCodeStart":747,"sourceCodeEnd":783,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Support/Collection.zep#L747-L783","documentation":"Phalcon\\Support\\Collection accepts a $type as its 4th constructor argument (also restored on unserialization). When a type is set, every value stored through init() or set() passes validateType(): the exact tokens 'int', 'string', 'bool', 'float', 'array', 'object' map to is_* checks, and any other string is treated as a class/interface name checked with instanceof. A value that fails the check throws Phalcon\\Support\\Collection\\Exceptions\\InvalidValueType (extends InvalidArgumentException) with \"Value must be of type '{type}', '{actual}' given\", where {actual} is gettype() of the rejected value.","triggerScenarios":"new Collection(['age' => '30'], true, false, 'int') (numeric string vs int); new Collection(['u' => new stdClass()], true, false, DateTimeInterface::class)->set('u', 'nope'); using a non-token spelling like 'integer' or 'boolean', which falls through to instanceof and always fails; passing null, which fails every type check.","commonSituations":"Query params, JSON bodies, and DB/CSV drivers return numbers as strings, so a collection typed 'int'/'float' throws on the first insert. Rehydrating a typed collection with user input of shifted types. Declaring type 'integer'/'double' instead of the recognized tokens.","solutions":["Cast values to the declared type before insert, e.g. (int) $age, or normalize the whole input array first","Use only the exact scalar tokens 'int', 'string', 'bool', 'float', 'array', 'object' — 'integer'/'boolean'/'double' are treated as class names and never match","If the payload is genuinely mixed, omit the type argument (null) and validate at the business layer","For class/interface types, ensure every value passes instanceof before set()/init()"],"exampleFix":"// before\n$collection = new Collection(['age' => '30'], true, false, 'int'); // throws: '30' is string\n\n// after\n$collection = new Collection(['age' => (int) '30'], true, false, 'int');","handlingStrategy":"type-guard","validationCode":"$type = 'int';\n$data = ['age' => '30', 'posts' => 7];\n\nforeach ($data as $key => $value) {\n    if ('int' === $type && !is_int($value)) {\n        $data[$key] = (int) $value; // or reject with your own error\n    }\n}\n\n$collection = new Collection($data, true, false, $type);","typeGuard":"function acceptsCollectionType($value, string $type): bool\n{\n    switch ($type) {\n        case 'int':    return is_int($value);\n        case 'string': return is_string($value);\n        case 'bool':   return is_bool($value);\n        case 'float':  return is_float($value);\n        case 'array':  return is_array($value);\n        case 'object': return is_object($value);\n        default:       return $value instanceof $type;\n    }\n}","tryCatchPattern":"use Phalcon\\Support\\Collection\\Exceptions\\InvalidValueType;\n\ntry {\n    $collection->set('age', $age);\n} catch (InvalidValueType $e) {\n    // \"Value must be of type 'int', 'string' given\"\n    $logger->warning($e->getMessage());\n    $collection->set('age', (int) $age);\n}","preventionTips":["Normalize scalar types at the system boundary — cast query/JSON/DB input once, before it reaches a typed collection","Use only the six recognized scalar tokens; anything else must be a real class/interface name","Wrap typed collections in factory methods that validate the whole payload before constructing","Unit-test every typed collection with edge values: null, numeric strings, empty arrays"],"tags":["phalcon","collection","type-validation","php"],"backgroundTag":"type-validation-failed","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}