Skip to main content
added 884 characters in body
Source Link

I've have an animal class

class Animal
{
    public function eat(Food $food);
}

the subclass who inherit it actually cannot support all kinds of Food (Cat can only eat meat):

class Cat extends Animal
{
    public function eat(Food $food)
    {
        if (!$food instanceof Meat) throw new InvalidArgumentException();
    }
}

of course, Meat is a subclass of Food

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

====================================

PS. The description above is an edited version. the original version is like below:

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();
        ...
    }
}

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

I've have an animal class

class Animal
{
    public function eat(Food $food);
}

the subclass who inherit it actually cannot support all kinds of Food (Cat can only eat meat):

class Cat extends Animal
{
    public function eat(Food $food)
    {
        if (!$food instanceof Meat) throw new InvalidArgumentException();
    }
}

of course, Meat is a subclass of Food

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

I've have an animal class

class Animal
{
    public function eat(Food $food);
}

the subclass who inherit it actually cannot support all kinds of Food (Cat can only eat meat):

class Cat extends Animal
{
    public function eat(Food $food)
    {
        if (!$food instanceof Meat) throw new InvalidArgumentException();
    }
}

of course, Meat is a subclass of Food

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

====================================

PS. The description above is an edited version. the original version is like below:

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();
        ...
    }
}

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

deleted 515 characters in body
Source Link

I've defined a data transformer interfacehave an animal class

interfaceclass TransformerInterfaceAnimal
{
    /**
     * @return mixed
     */
    public function transformeat($originFood $food);
}

as you could see, $origin and the return type could be any type of data (I use PHP), however, the classsubclass who implementsinherit it actually cannot support all kinds of data,Food (I think it should be OK if it returns certain type of data, it doesn't violate LSPCat can only eat meat):

class TagTransformerCat implementsextends TransformerInterfaceAnimal
{
    public function transformeat($originFood $food)
    {
        if (!is_string($origin)$food instanceof Meat) throw new InvalidArgumentException();
        ...
    }
}

why I've designed the interface like this because I want aof course, ChainTransformerMeat to combine the transformers, so all the code in any transformer class could haveis a chance to reuse:subclass of Food

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

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

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?

I've have an animal class

class Animal
{
    public function eat(Food $food);
}

the subclass who inherit it actually cannot support all kinds of Food (Cat can only eat meat):

class Cat extends Animal
{
    public function eat(Food $food)
    {
        if (!$food instanceof Meat) throw new InvalidArgumentException();
    }
}

of course, Meat is a subclass of Food

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

edited tags
Source Link

I've defined a DataTransformerdata transformer interface

interface transformerInterfaceTransformerInterface
{
    /**
     * @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?

I've defined a DataTransformer 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?

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?

Source Link
Loading