{"record":{"id":"7327f08959e8a8e3","repo":"Intervention/image","slug":"result-must-be-instance-of-colorinterface-class","errorCode":null,"errorMessage":"Result must be instance of {ColorInterface::class}","messagePattern":"Result must be instance of (.+?)","errorType":"exception","errorClass":"ColorDecoderException","httpStatus":null,"severity":"error","filePath":"src/Drivers/AbstractDriver.php","lineNumber":100,"sourceCode":"     * @throws ColorDecoderException\n     * @throws DriverException\n     */\n    public function decodeColor(mixed $input, ?array $decoders = null): ColorInterface\n    {\n        $decoders = $decoders === null ? InputHandler::COLOR_DECODERS : $decoders;\n\n        if (count($decoders) === 0) {\n            throw new InvalidArgumentException('No decoders in array');\n        }\n\n        try {\n            $result = InputHandler::usingDecoders($decoders, $this)->handle($input);\n        } catch (NotSupportedException) {\n            throw new ColorDecoderException('Unknown color format');\n        }\n\n        if (!$result instanceof ColorInterface) {\n            throw new ColorDecoderException('Result must be instance of ' . ColorInterface::class);\n        }\n\n        return $result;\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see DriverInterface::specializeModifier()\n     *\n     * @throws NotSupportedException\n     */\n    public function specializeModifier(ModifierInterface $modifier): ModifierInterface\n    {\n        return $this->specialize($modifier);\n    }\n\n    /**","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/AbstractDriver.php#L82-L118","documentation":"The color decoder chain finished but its result is not a ColorInterface instance. The driver validates the handler's result before returning it. All built-in color decoders return colors, so this indicates a custom color decoder in the chain violating the decoder contract.","triggerScenarios":"Registering a custom color decoder whose decode() returns a string, array, or DTO, then calling any color-accepting API that routes through $driver->decodeColor().","commonSituations":"Custom color decoders added to support app-internal color types (design tokens, serialized color structs) that return the raw struct instead of a ColorInterface.","solutions":["Return a ColorInterface from the custom decoder - e.g. build and return an Intervention\\Image\\Colors\\Rgb\\Color","Convert the custom format to Rgb\\Color inside the decoder before returning"],"exampleFix":"// before\npublic function decode(mixed $input): mixed\n{\n    return $input; // raw token struct\n}\n\n// after\npublic function decode(mixed $input): ColorInterface\n{\n    return new RgbColor($input->r, $input->g, $input->b);\n}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isValidColorDecoder(mixed $decoder): bool\n{\n    return $decoder instanceof \\Intervention\\Image\\Interfaces\\DecoderInterface;\n}","tryCatchPattern":"try {\n    $color = $driver->decodeColor($input);\n} catch (ColorDecoderException $e) {\n    // a registered custom color decoder returned a non-color - fix the decoder\n}","preventionTips":["Type custom color decoders' return as ColorInterface","Unit-test custom decoders through decodeColor(), not only in isolation"],"tags":["color","decoder","contract","custom-extension"],"backgroundTag":"custom-decoder-invalid-return","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}