Skip to content

Instantly share code, notes, and snippets.

@prodeveloper
Last active August 29, 2015 14:06
Show Gist options
  • Save prodeveloper/cd923c2a50822a5e5d60 to your computer and use it in GitHub Desktop.
Save prodeveloper/cd923c2a50822a5e5d60 to your computer and use it in GitHub Desktop.
Introduce Parameter Object Refactor | After Refactor
<?php
use DateRange;
/**
* Easy to read
* Easy to refactor
* Easy to mock and test
*/
class Customer {
function amountInvoicedIn(DateRange $d_range) {
}
function amountReceicedIn(DateRange $d_range) {
}
function amountOverdueIn(DateRange $d_range) {
}
}
<?php
class DateRange {
private $end_date;
private $start_date;
function __construct($start_date, $end_date) {
$this->start_date = $start_date;
$this->end_date = $end_date;
}
function getStartDate() {
return $this->start_date;
}
function getEndDate() {
return $this->end_date;
}
}
<?php
/**
* Someone (probably your future self) mercilessly murdering your design in the name of
* pretty time
*
* @author jacob
*/
class DateRangeChild extends DateRange{
function getStartDate() {
$this->start_date;
return $this->makePretty($date);
}
function makePretty($date){
return "Today is {$date}";
}
}
<?php
/**
* A parameter object safe from murder
*
* @author jacob
*/
final class DateRangeProtected {
protected $end_date;
protected $start_date;
function __construct($start_date, $end_date) {
$this->start_date = $start_date;
$this->end_date = $end_date;
}
function getStartDate() {
return $this->start_date;
}
function getEndDate() {
return $this->end_date;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment