制約でコードに秩序を

 制約でコードに秩序を

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

Ca17a082a30f4cbfed1d0a6dacbe3af2?s=128

shin1x1

May 21, 2016
Tweet

Transcript

  1. 11.

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

    function __construct(string $code, int $price)
 {
 $this->code = $code;
 $this->price = $price;
 }
 }
  2. 12.

    $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. 14.

    $BSU class Cart
 {
 private $items;
 
 public function __construct()


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

    $BSU class Cart
 { (snip)
 public function addItem(Item $item)
 {


    if (count($this->items) >= 3) {
 throw new PreconditionException();
 }
 
 $this->items[] = $item;
 } (snip)
 }
  5. 22.

    $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. 23.

    $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. 25.

    *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. 26.

    $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. 29.

    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. 30.

    *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. 31.

    $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. 35.

    "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. 36.

    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. 39.

    *UFN class Item implements Arrayable
 {
 (snip) public function toArray():

    array
 {
 return [
 'code' => $this->code,
 'price' => $this->price->toInt(),
 ];
 }
 }
  15. 40.

    $BSU class Cart implements Arrayable
 {
 (snip) public function toArray():

    array
 {
 return array_map(function (Item $item) {
 return $item->toArray();
 }, $this->items);
 }
 }
  16. 41.

    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