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?