{"record":{"id":"c3365f3f884894b1","repo":"yiisoft/yii2","slug":"invalid-data-type-valuetype-type-is-expected","errorCode":null,"errorMessage":"Invalid data type: $valueType. $type is expected.","messagePattern":"Invalid data type: \\$valueType\\. \\$type is expected\\.","errorType":"exception","errorClass":"InvalidConfigException","httpStatus":null,"severity":"error","filePath":"framework/di/Instance.php","lineNumber":166,"sourceCode":"        } elseif ($type === null || $reference instanceof $type) {\n            return $reference;\n        }\n\n        if ($reference instanceof self) {\n            try {\n                $component = $reference->get($container);\n            } catch (\\ReflectionException $e) {\n                throw new InvalidConfigException('Failed to instantiate component or class \"' . $reference->id . '\".', 0, $e);\n            }\n            if ($type === null || $component instanceof $type) {\n                return $component;\n            }\n\n            throw new InvalidConfigException('\"' . $reference->id . '\" refers to a ' . get_class($component) . \" component. $type is expected.\");\n        }\n\n        $valueType = is_object($reference) ? get_class($reference) : gettype($reference);\n        throw new InvalidConfigException(\"Invalid data type: $valueType. $type is expected.\");\n    }\n\n    /**\n     * Returns the actual object referenced by this Instance object.\n     * @param ServiceLocator|Container|null $container the container used to locate the referenced object.\n     * If null, the method will first try `Yii::$app` then `Yii::$container`.\n     * @return object|null the actual object referenced by this Instance object.\n     */\n    public function get($container = null)\n    {\n        try {\n            if ($container) {\n                return $container->get($this->id);\n            }\n            if (Yii::$app && Yii::$app->has($this->id)) {\n                return Yii::$app->get($this->id);\n            }\n","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/di/Instance.php#L148-L184","documentation":"yii\\di\\Instance::ensure() fell through all accepted reference shapes - the value is not an array config, not a string ID, not an Instance, and not an object of the expected type - so it reports the actual PHP type it received ('Invalid data type: integer. ... is expected.'). It is the terminal guard of ensure(): whatever was passed can never denote a component.","triggerScenarios":"Instance::ensure(5, Cache::class) or Instance::ensure(true, ...) - an int/bool/resource/float where a component reference is required; an untyped config value (e.g. from JSON or a DB field) passed straight into a component-typed setter.","commonSituations":"Env/config values cast to scalars (feature flags parsed as int) funneled into component properties; JSON payloads where an object was expected but a scalar arrived; setter methods that accept mixed but forward to Instance::ensure without validation.","solutions":["Pass a valid reference: a component ID string, a config array with 'class', an Instance, or an object instance.","Validate/normalize external input before it reaches the component setter (is_string/is_array checks).","Cast or map scalar settings to a real configuration, e.g. 5 → ['class' => FileCache::class, 'cachePath' => ...].","Fix the source of the wrong type (config schema, JSON contract)."],"exampleFix":"// before\n$cache = Instance::ensure(5, yii\\caching\\CacheInterface::class);\n\n// after\n$cache = Instance::ensure('cache', yii\\caching\\CacheInterface::class);","handlingStrategy":"type-guard","validationCode":"if (!is_string($value) && !is_array($value) && !is_object($value)) {\n    throw new \\InvalidArgumentException('Component reference must be a string ID, config array, or object; got ' . gettype($value));\n}\n$component = \\yii\\di\\Instance::ensure($value, $type);","typeGuard":"/** Narrows anything a config source can yield to a valid Instance::ensure() reference. */\nfunction toReference($value)\n{\n    if (is_string($value) && $value !== '') {\n        return $value;\n    }\n    if (is_array($value) && isset($value['__class'], $value['class']) === false && isset($value['class'])) {\n        return $value;\n    }\n    if (is_object($value) && !($value instanceof \\Closure)) {\n        return $value;\n    }\n    return null; // caller decides the default\n}","tryCatchPattern":"try {\n    $component = \\yii\\di\\Instance::ensure($raw, $type);\n} catch (\\yii\\base\\InvalidConfigException $e) {\n    if (strpos($e->getMessage(), 'Invalid data type:') === 0) {\n        // normalize: fall back to the default component ID\n        $component = \\yii\\di\\Instance::ensure('cache', $type);\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Validate external/config input (is_string/is_array) before forwarding to component setters.","Type properties and parameters where PHP allows it so scalars never reach ensure().","Map flattened config scalars into full config arrays at load time, not at use time."],"tags":["yii2","dependency-injection","instance","type-mismatch","invalid-config"],"backgroundTag":"di-type-mismatch","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}