تخطَّ إلى المحتوى

Long method (ARCH7180)

هذا المحتوى غير متوفر بلغتك بعد.

Key php.quality.long-method · Severity info · Category quality

Methods whose body exceeds the threshold usually take on multiple responsibilities, are hard to read, and resist testing. Break the logic into smaller, named methods.

<?php
function processOrder(array $order): void
{
$total = 0;
foreach ($order['items'] as $i) { $total += $i['price'] * $i['qty']; }
if ($order['coupon'] === 'SAVE10') { $total *= 0.9; }
if ($total > 100) { $total -= 5; }
$tax = $total * 0.18;
$total += $tax;
file_put_contents('invoice.txt', "Total: $total");
mail($order['email'], 'Order', "Total: $total");
// ... dozens more lines ...
}
<?php
function processOrder(array $order): void
{
$total = applyDiscounts(subtotal($order['items']), $order['coupon']);
$total += $total * 0.18;
writeInvoice($total);
notifyCustomer($order['email'], $total);
}

Split the method into smaller, named helpers that each carry a single responsibility.