O — Open/Closed

  1. SSingle Responsibility
  2. OOpen/Closed
  3. LLiskov Substitution
  4. IInterface Segregation
  5. DDependency Inversion

PaymentProcessor

Before

class PaymentProcessor
{
    public function charge(string $method, Money $amount): Receipt
    {
        if ($method === 'stripe') {
            return $this->chargeStripe($amount);
        } elseif ($method === 'paypal') {
            return $this->chargePaypal($amount);
        }

        throw new InvalidArgumentException('Unknown payment method');
    }
}

After

interface PaymentGateway
{
    public function charge(Money $amount): Receipt;
}

class PaymentProcessor
{
    public function __construct(private PaymentGateway $gateway) {}

    public function charge(Money $amount): Receipt
    {
        return $this->gateway->charge($amount);
    }
}
// Adding Klarna later: implement PaymentGateway, no changes here
{CC}