Skip to main content
2 of 4
edited tags

Violation of Liskov Substitution Principle?

I've defined a data transformer interface

interface TransformerInterface
{
    /**
     * @return mixed
     */
    public function transform($origin);
}

as you could see, $origin and the return type could be any type of data (I use PHP), however, the class who implements it actually cannot support all kinds of data, (I think it should be OK if it returns certain type of data, it doesn't violate LSP):

class TagTransformer implements TransformerInterface
{
    public function transform($origin)
    {
        if (!is_string($origin)) throw new InvalidArgumentException();
        ...
    }
}

why I've designed the interface like this because I want a ChainTransformer to combine the transformers, so all the code in any transformer class could have a chance to reuse:

$transformer = new ChainTransformer([new String2ObjA(), new ObjA2ObjB(), ...]);

So is this code violate LSP (I think it does)? and how to re-design it?