This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the dumb-mistakes-and-gotchas category.
Last Updated: 2024-11-21
Never bind a variable only in if
branches.
<?php
if ($cart->isFree()) {
$appointment = $cart->redeemFreebieIfAvailable();
}
if ($unusedTicket) {
$appointment = $cart->useTicketToBookAppointment($unusedTicket);
}
// FIXME: In the double-payment edge case here, there is no appointment
// model available, therefore this will fail.
if ($cart->fresh()->paid) {
if ($appointment) {
This gave an error appointment undefined
sometimes - i.e. when neither condition was true.
I should have set the appointment
to null
first
<?php
$appoinment = null;