From this example, is CoffeeWithCream's getBrand() method improper or problematic in any way? My reason for doing it this way is to avoid writing $coffeeWithCream->$coffee->getBrand() everywhere it's called.
Particularly, one area of concern that's surfaced is unit testing. I'm not comfortable enough yet with Unit Testing to know whether this strategy complicates testing.
Also, I know that getBrand() is just a simple accessor method. What if the method performed a more complex task, would the answer change?
Class Coffee {
public $brand;
public $ingredients = array('coffee');
public function getBrand() {
return $this->brand;
}
public function getIngredients() {
return $this->ingredients;
}
}
Class CoffeeWithCream {
public $coffee;
public __construct(Coffee $coffee) {
$this->coffee = $coffee;
}
public function getIngredients() {
$ingredients = $this->coffee->getIngredients();
$ingredients[] = 'cream';
return $ingredients;
}
public function getBrand() {
$this->coffee->getBrand();
}
}