The same match over order status appeared in a controller, two Blade templates, a mailer and an exporter, and adding a case updated four of the five.
enum OrderStatus: string
{
case Pending = 'pending';
case Shipped = 'shipped';
public function isFinal(): bool
{
return match ($this) {
self::Shipped => true,
self::Pending => false,
};
}
public static function open(): array
{
return array_filter(self::cases(), fn (self $c) => ! $c->isFinal());
}
}
Moving the behaviour onto the enum means adding a case produces an UnhandledMatchError at the one place that has to change, which is the whole benefit — a match with no default is an exhaustiveness check the engine performs for you. Static methods on the enum are the natural home for the collection queries that were previously an array constant somewhere else. The temptation to keep going and put the persistence on it too should be resisted.