Good Code
The good version declares strict typing, names the unit of money, returns a specific type, and rejects invalid values before computing.
Lesson 01
Make PHP function contracts explicit with strict scalar types and return types so reviewers can catch coercion bugs at boundaries.
<?php declare(strict_types=1);
function invoiceTotalCents(int $subtotalCents, int $taxCents): int
{
if ($subtotalCents < 0 || $taxCents < 0) {
throw new InvalidArgumentException('Invoice amounts must be positive.');
}
return $subtotalCents + $taxCents;
}<?php
function invoiceTotal($subtotal, $tax)
{
return $subtotal + $tax;
}
echo invoiceTotal($_POST['subtotal'], $_POST['tax']);The good version declares strict typing, names the unit of money, returns a specific type, and rejects invalid values before computing.
The bad version relies on PHP coercion and untyped request values. A reviewer has to infer whether strings, floats, negative numbers, or missing fields are valid.