Upgrade to Pro — share decks privately, control downloads, hide ads and more …

制約でコードに秩序を

 制約でコードに秩序を

2016/05/21 PHP カンファレンス福岡 2016

shin1x1

May 21, 2016
Tweet

More Decks by shin1x1

Other Decks in Programming

Transcript

  1. *UFN class Item
 {
 public $code;
 public $price;
 
 public

    function __construct(string $code, int $price)
 {
 $this->code = $code;
 $this->price = $price;
 }
 }
  2. $BSU class Cart
 {
 public $items;
 
 public function __construct()


    {
 $this->items = [];
 }
 
 public function calculateTotalPrice(): int
 {
 $totalPrice = 0;
 
 foreach ($this->items as $item) {
 $totalPrice = $totalPrice + $item->price;
 }
 
 return $totalPrice;
 }
 }
  3. $BSU class Cart
 {
 private $items;
 
 public function __construct()


    {
 $this->items = [];
 }
 
 public function addItem(Item $item)
 {
 $this->items[] = $item;
 }
 (snip) } QSJWBUF λΠϓώϯςΟϯά
  4. $BSU class Cart
 { (snip)
 public function addItem(Item $item)
 {


    if (count($this->items) >= 3) {
 throw new PreconditionException();
 }
 
 $this->items[] = $item;
 } (snip)
 }
  5. $BSU $cart = new Cart();
 
 $item1 = new Item('code1',

    100);
 $cart->addItem($item1);
 
 $item2 = new Item('code2', 200);
 $cart->addItem($item2);
 
 $this->assertEquals(324, $cart->calculateTotalPrice());
 $this->assertEquals(324, $cart->calculateTotalPrice()); ʹͳΔʁʁʁ
  6. $BSU class Cart
 { (snip)
 public function calculateTotalPrice(): int
 {


    $totalPrice = 0;
 
 foreach ($this->items as $item) {
 $item->price = (int)($item->price * 1.08);
 $totalPrice = $totalPrice + $item->price;
 }
 
 return $totalPrice;
 } (snip)
 } *UFNQSJDF͕ มߋ͞ΕΔ ճݺͿͱ ͞ΒʹഒʹͳΔ
  7. *UFN class Item
 {
 private $code;
 private $price;
 
 public

    function __construct(string $code, int $price)
 {
 $this->code = $code;
 $this->price = $price;
 }
 
 public function price(): int
 {
 return $this->price;
 }
 } QSJWBUF
  8. $BSU class Cart
 { (snip)
 public function calculateTotalPrice(): int
 {


    $totalPrice = 0;
 
 foreach ($this->items as $item) {
 $price = (int)($item->price() * 1.08);
 $totalPrice = $totalPrice + $price;
 }
 
 return $totalPrice;
 } (snip)
 } *UFNQSJDF͸ มߋͰ͖ͳ͍
  9. 1SJDF class Price
 {
 const TAX_RATE = 1.08;
 private $price;


    
 public function __construct(int $price)
 {
 $this->price = $price;
 }
 
 public function add(self $price): self
 {
 return new self($this->price + $price->price);
 }
 
 public function withTax(): self
 {
 return new self((int)($this->price * static::TAX_RATE));
 }
 
 public function toInt(): int
 {
 return $this->price;
 }
 }
  10. *UFN class Item
 {
 private $code;
 private $price;
 
 public

    function __construct(string $code, int $price)
 {
 $this->code = $code;
 $this->price = new Price($price);
 }
 
 public function price(): Price
 {
 return $this->price;
 }
 }
  11. $BSU class Cart
 { (snip)
 public function calculateTotalPrice(): Price
 {


    $totalPrice = new Price(0);
 
 foreach ($this->items as $item) {
 $price = $item->price()->withTax();
 $totalPrice = $totalPrice->add($price);
 }
 
 return $totalPrice;
 } (snip)
 } Ձ֨͸1SJDFͰදݱ ੫ࠐՁ֨ࢉग़ Ձ֨ͷ଍͠ࢉ
  12. "QJ$POUSPMMFS class ApiController
 {
 public function item()
 {
 $item =

    new Item('code1', 100);
 $this->json($item);
 }
 
 private function json(Item $entity)
 {
 header('Content-Type: application/json');
 echo json_encode($entity->toArray());
 }
 }
  13. class ApiController
 {
 public function item()
 {
 $item = new

    Item('code1', 100);
 $this->json($item);
 }
 public function cart()
 {
 $cart = new Cart();
 $cart->addItem(new Item('code1', 100));
 $cart->addItem(new Item('code2', 200));
 
 $this->json($cart);
 } private function json(Item $entity)
 {
 header('Content-Type: application/json');
 echo json_encode($entity->toArray());
 }
 } DBSU͸
 *UFNΫϥεͰ͸ͳ͍
  14. *UFN class Item implements Arrayable
 {
 (snip) public function toArray():

    array
 {
 return [
 'code' => $this->code,
 'price' => $this->price->toInt(),
 ];
 }
 }
  15. $BSU class Cart implements Arrayable
 {
 (snip) public function toArray():

    array
 {
 return array_map(function (Item $item) {
 return $item->toArray();
 }, $this->items);
 }
 }
  16. class ApiController
 {
 public function item()
 {
 $item = new

    Item('code1', 100);
 $this->json($item);
 }
 public function cart()
 {
 $cart = new Cart();
 $cart->addItem(new Item('code1', 100));
 $cart->addItem(new Item('code2', 200));
 
 $this->json($cart);
 } private function json(Arryable $entity)
 {
 header('Content-Type: application/json');
 echo json_encode($entity->toArray());
 }
 } "SSBZBCMF