Liskov Substitution: fix a subtype that breaks callers
Make every subtype work wherever its base type already works.
Watch the narrated video
Every step
Problem
Steps 1–5Refactor
Steps 6–9Payoff
Steps 10–11Recap
Steps 12–13The code: before and after
Before
declare(strict_types=1);
abstract class PaymentMethod
{
abstract public function charge(int $amount): void;
abstract public function refund(int $amount): void;
}
final class Card extends PaymentMethod
{
public function charge(int $amount): void
{
echo "card charged $amount\n";
}
public function refund(int $amount): void
{
echo "card refunded $amount\n";
}
}
final class Checkout
{
public function pay(PaymentMethod $method, int $amount): void
{
$method->charge($amount);
}
}
final class Returns
{
public function giveBack(PaymentMethod $method, int $amount): void
{
$method->refund($amount);
}
}
$card = new Card();
(new Checkout())->pay($card, 40);
(new Returns())->giveBack($card, 40);After
declare(strict_types=1);
interface Refundable
{
public function refund(int $amount): void;
}
abstract class PaymentMethod
{
abstract public function charge(int $amount): void;
}
final class Card extends PaymentMethod implements Refundable
{
public function charge(int $amount): void
{
echo "card charged $amount\n";
}
public function refund(int $amount): void
{
echo "card refunded $amount\n";
}
}
final class GiftCard extends PaymentMethod
{
public function charge(int $amount): void
{
echo "gift card charged $amount\n";
}
}
final class Checkout
{
public function pay(PaymentMethod $method, int $amount): void
{
$method->charge($amount);
}
}
final class Returns
{
public function giveBack(Refundable $method, int $amount): void
{
$method->refund($amount);
}
}
$card = new Card();
(new Checkout())->pay($card, 40);
(new Returns())->giveBack($card, 40);
$gift = new GiftCard();
(new Checkout())->pay($gift, 25);
// (new Returns())->giveBack($gift, 25); // TypeErrorTranscript, step by step
Problem
Step 1: A shop that takes cards Before
Payment Method is an abstract class, and every way to pay extends it, so it's the base type. It promises its callers a charge method and a refund method, and that promise is its contract. Card is the one way the shop has to pay, and it can charge and refund. Checkout and Returns are the callers, and they only know the base type. The script pays forty dollars with the card and gives it back, so it prints card charged and card refunded. Every payment so far could be charged and refunded, so putting both methods in the base type made sense.
Step 2: Finance adds gift cards Code smell
Finance adds gift cards, and a gift card can pay, but its money is never refunded. The quick way adds Gift Card as a subtype of Payment Method, and its charge prints gift card charged. The base type asks for a refund too, so this one throws a Logic Exception. The script pays twenty five with a gift card, then gives it back. PHP loads the file without a complaint, so what happens when this last line runs? PHP stops with an uncaught Logic Exception, because give back calls refund here, and refund throws here.
Step 3: Returns checks the type Code smell
To stop the crash, you'd put a type check at the top of give back. If the method is a Gift Card, it prints gift card no refund and returns early. So the script runs to the end again, with two gift card lines in its output.
Step 4: A subtype breaks its contract Code smell
Gift Card is a subtype that can't keep its contract, so a caller that trusts the base type crashed. The contract includes refund here, the gift card breaks it here, and Returns now needs a type check here. The next payment that can't refund needs a second check here, and every other caller of refund needs its own. And the base type still says refund is safe to call on any payment, which is no longer true.
Step 5: Liskov Substitution Principle
So code that works with a base type must keep working with any subtype, and that rule is the Liskov Substitution Principle. Gift Card breaks it, and the type check in Returns is the symptom.
Refactor
Step 6: Refactor first Back it out
So let's take the gift card and the type check out again. We'll refactor first, so the contract fits every payment, and then add gift cards back.
Step 7: Name the refund Extract Interface
First we give refund a type of its own, an interface called Refundable with that one method. Card implements it as well, and its refund method already matches, so nothing inside Card changes. Payment Method still declares refund, and the next two moves fix that.
Step 8: Returns asks for Refundable Change Function Declaration
Next, give back takes a Refundable instead of a Payment Method, because refund is the only thing it calls. The script still passes the card, and a card is a Refundable, so it prints the same two lines.
Step 9: Refund leaves the base type Push Down Method
Now no caller needs refund from Payment Method, so it moves down out of the base type. The contract of Payment Method is charge only, and refund lives in Refundable, which Card implements.
Payoff
Step 10: Gift cards, again Payoff
So now gift cards can come back in. Gift Card extends Payment Method with charge only, so there's no refund method and nothing to throw. The script pays twenty five with it, and the program prints gift card charged twenty five. The give back line from before would now throw a Type Error at the call, because Gift Card isn't a Refundable. So the error names the line that passed the gift card, not code deep inside refund. A static checker like PHPStan reports that line before you ever run the script.
Step 11: Every subtype keeps the contract Payoff
Card, Checkout and Returns did not change to add gift cards. The contract has no refund here, the gift card has nothing to break here, and Returns has no type check here. Gift Card can stand in for Payment Method everywhere, because the contract holds only what every payment can do. But the split has a cost, because code that holds only a Payment Method can't call refund on it. To refund one, it has to check for Refundable first, so a type check comes back in that caller.
Recap
Step 12: Where we started, where we ended Before / after
On the left, Payment Method's contract included refund, so the first payment that couldn't refund had to throw. On the right, its contract is charge only, and refund has its own interface. Gift Card keeps the whole contract it inherits, and Returns asks only for something it can refund.
Step 13: When to reach for it Takeaways
Reach for it when a subtype overrides a method only to throw, or when its callers need a special case for it. Skip it when the subtype can keep the contract, like gift cards that pay back in store credit. You'll spot it by a caller with a type check, or an override that throws for good. So a function that only formats a date takes Date Time Interface, and both date classes fit.