View on GitHub

RefactoringPHP

A PHP version of the refactors from Refactoring: Improving the Design of Existing Code (Second Edition) by Martin Fowler

Extract Function (106)

Inverse of Inline Function (115)

Old Code

<?php
function printOwing($invoice)
{
    printBanner();
    $outstanding = calculateOutstanding();
    
    // print details
    echo "name: {$invoice->customer}\n";
    echo "amount: {$outstanding}\n";
}

New Code

<?php
function printOwing($invoice)
{
    printBanner();
    $outstanding = calculateOutstanding();
    printDetails($invoice,$outstanding);
}

function printDetails($invoice,$outstanding)
{    
    // print details
    echo "name: {$invoice->customer}\n";
    echo "amount: {$outstanding}\n";
}