3v4l.org

run code in 500+ PHP versions simultaneously
<?php class FakeCollection { public function __construct(private array $items) {} public function sum(string $key): float { $total = 0; foreach ($this->items as $item) { $total += $item[$key]; } return $total; } } function collect(array $items): FakeCollection { return new FakeCollection($items); } class FakePayment { public array $credits = []; public function __construct(public float $amount, public float $refunded) {} public function credits() { return new class { public function exists(): bool { return false; } }; } } function checkTotalRefundableAmount($payment, array $request_invoices, array $input): bool { // Calculate total refund amount requested $total_refund_requested = 0; if (count($request_invoices) > 0) { $total_refund_requested = collect($request_invoices)->sum('amount'); } elseif (array_key_exists('amount', $this->input)) { $total_refund_requested = $this->input['amount']; } if ($total_refund_requested <= 0) { return true; // No refund requested } // Calculate maximum refundable from cash portion $max_cash_refund = $payment->amount - $payment->refunded; // Calculate maximum refundable from credits $max_credit_refund = 0; if ($payment->credits()->exists()) { foreach ($payment->credits as $credit) { $max_credit_refund += ($credit->pivot->amount - $credit->pivot->refunded); } } $max_total_refundable = $max_cash_refund + $max_credit_refund; if ($total_refund_requested > $max_total_refundable) { return false; } return true; } $payment = new FakePayment(amount: 30.20, refunded: 0.0); $invoices = [['invoice_id' => 'INV_1', 'amount' => 11.85], ['invoice_id' => 'INV_2', 'amount' => 18.35]]; var_dump(checkTotalRefundableAmount($payment, $invoices, ['id' => 'PAYMENT_ID'])); // bool(false) — should be true
Output for git.master, git.master_jit
bool(false)

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
45.54 ms | 784 KiB | 4 Q