View on GitHub

RefactoringPHP

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

Replace Derived Variable with Query (248)

Old Code

<?php
public function getDiscountedTotal()
{
    return $this->discountedTotal;
}
public function setDiscount($aNumber)
{
    $old = $this->discount;
    $this->discount = $aNumber;
    $this->discountedTotal += $old - $aNumber;
}

New Code

<?php
public function getDiscountedTotal()
{
    return $this->baseTotal - $this->discountedTotal;
}
public function setDiscount($aNumber)
{
    $this->discount = $aNumber;
}