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. ɹ!shin1x1
    2016/05/21 1)1ΧϯϑΝϨϯε෱Ԭ
    ੍໿ͰίʔυʹடংΛ

    View full-size slide

  2. ੍ݶ΍৚݅Λ͚ͭͯ
    ࣗ༝ʹ׆ಈͤ͞ͳ͍͜ͱ
    https://kotobank.jp/word/%E5%88%B6%E7%B4%84-546519

    View full-size slide

  3. ੍ݶ΍৚݅Λ͚ͭͯ
    ࣗ༝ʹ׆ಈͤ͞ͳ͍͜ͱ
    https://kotobank.jp/word/%E5%88%B6%E7%B4%84-546519

    View full-size slide

  4. ࠓ೔ͷ୊ࡐ

    View full-size slide

  5. &$αΠτ
    γϣοϐϯάΧʔτ

    View full-size slide

  6. Χʔτʹ௥Ճ

    View full-size slide

  7. ΧʔτΛදࣔ

    View full-size slide


  8. Χʔτʹ঎඼Λ௥Ճ

    View full-size slide

  9. D
    .BTBTIJ4IJOCBSB!TIJOY
    Χʔτʹ঎඼Λ௥Ճ
    wΧʔτ$BSUΫϥε
    w঎඼*UFNΫϥε
    w$BSUJUFNTʹ঎඼Λ௥Ճ

    View full-size slide

  10. *UFN
    class Item

    {

    public $code;

    public $price;


    public function __construct(string $code, int $price)

    {

    $this->code = $code;

    $this->price = $price;

    }

    }

    View full-size slide

  11. $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;

    }

    }

    View full-size slide

  12. D
    .BTBTIJ4IJOCBSB!TIJOY
    ঎඼௥ՃΛ*UFNΫϥεʹݶఆ
    w$BSUJUFNTΛQSJWBUFͰӅ͢
    wBEE*UFNϝιουΛ௥Ճ
    wλΠϓώϯςΟϯάͰ*UFNΫϥεʹݶఆ

    View full-size slide

  13. $BSU
    class Cart

    {

    private $items;


    public function __construct()

    {

    $this->items = [];

    }


    public function addItem(Item $item)

    {

    $this->items[] = $item;

    }

    (snip)
    }
    QSJWBUF
    λΠϓώϯςΟϯά

    View full-size slide

  14. D
    .BTBTIJ4IJOCBSB!TIJOY
    ΞΫηεम০ࢠλΠϓώϯςΟϯά
    wΫϥεࣗ਎͕ϓϩύςΟΛίϯτϩʔϧ
    wΫϥε֎ʹίϯτϩʔϧΛ౉͞ͳ͍
    wૢ࡞ํ๏Λݶఆ͍ͯ͠Δ͔Βͦ͜

    ίϯτϩʔϧͰ͖Δ

    ʢλΠϓώϯςΟϯάͳͲʣ

    View full-size slide


  15. Χʔτͷ঎඼఺਺͸
    ఺·Ͱ

    View full-size slide

  16. D
    .BTBTIJ4IJOCBSB!TIJOY
    Χʔτͷ঎඼఺਺͸఺Ҏ಺
    w఺Ҏ্ͷ঎඼Λ֨ೲ͠Α͏ͱͨ͠Βྫ֎ൃੜ
    w঎඼௥ՃͷࡍʹνΣοΫΛߦ͏

    View full-size slide

  17. D
    .BTBTIJ4IJOCBSB!TIJOY
    BEE*UFNϝιουͰ੍໿
    wBEE*UFNϝιουͰ঎඼఺਺ΛνΣοΫ
    w͜ͷϝιουͰ͔͠$BSUJUFNTΛ

    มߋͰ͖ͳ͍ͷͰ༗ޮ
    wϓϩύςΟͷίϯτϩʔϧΛΫϥεࣗ਎͕ߦ͏

    QVCMJDͩͱ֎෦ʹίϯτϩʔϧ͞ΕΔ

    View full-size slide

  18. $BSU
    class Cart

    {
    (snip)

    public function addItem(Item $item)

    {

    if (count($this->items) >= 3) {

    throw new PreconditionException();

    }


    $this->items[] = $item;

    }
    (snip)

    }

    View full-size slide


  19. ঎඼খܭʹ
    ফඅ੫ΛՃࢉ

    View full-size slide

  20. D
    .BTBTIJ4IJOCBSB!TIJOY
    ੫ࠐՁ֨ࢉग़
    wΧʔτͷ঎඼ͦΕͧΕʹফඅ੫ΛՃࢉ
    w੫ࠐΈՁ֨ͷ߹ܭΛখܭՁ֨ͱ͢Δ

    View full-size slide

  21. $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());
    ʹͳΔʁʁʁ

    View full-size slide

  22. $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͕
    มߋ͞ΕΔ
    ճݺͿͱ
    ͞ΒʹഒʹͳΔ

    View full-size slide

  23. D
    .BTBTIJ4IJOCBSB!TIJOY
    *UFNΫϥεΛෆมʹ͢Δ
    w׬શίϯετϥΫλ

    ίϯετϥΫλͰશͯͷ஋Λ༩͑Δ
    wෆมʢΠϛϡʔλϒϧʣΫϥε

    QSJDF
    ͸ৗʹಉ͡஋Λฦ͢
    wඞཁͳHFUUFSͷΈ࣮૷

    View full-size slide

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

    View full-size slide

  25. $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͸
    มߋͰ͖ͳ͍

    View full-size slide


  26. Ձ֨΁ͷૢ࡞Λݶఆ

    View full-size slide

  27. D
    .BTBTIJ4IJOCBSB!TIJOY
    Ձ֨ͷԋࢉΛ੍ݶ
    wJOUͩͱҙຯ͕޿͗͢ΔʢՄೳͳԋࢉ΋ଟ͍ʣ
    w଍͠ࢉͱ੫ࠐՁ֨ܭࢉʢֻ͚ࢉͰ͸ͳ͍ʣͷΈ
    wՁ֨Λ1SJDFΫϥεͰදݱ

    View full-size slide

  28. 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;

    }

    }

    View full-size slide

  29. *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;

    }

    }

    View full-size slide

  30. $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Ͱදݱ
    ੫ࠐՁ֨ࢉग़
    Ձ֨ͷ଍͠ࢉ

    View full-size slide

  31. D
    .BTBTIJ4IJOCBSB!TIJOY
    7BMVF0CKFDU
    w஋ΛΫϥεͰදݱ
    wෆมΫϥε

    ԋࢉ݁Ռ͸৽͍͠ΠϯελϯεͰฦ͢
    wඞཁͳԋࢉͷΈΛ࣮૷
    w಺෦ͷ஋ΛӅณ

    View full-size slide


  32. ঎඼ɺΧʔτ৘ใΛ
    +40/Ͱग़ྗ

    View full-size slide

  33. D
    .BTBTIJ4IJOCBSB!TIJOY
    ঎඼ɺΧʔτ৘ใΛ+40/Ͱग़ྗ
    w+BWB4DSJQUΫϥΠΞϯτ΍

    εϚʔτϑΥϯΞϓϦ࿈ܞ
    w*UFNUP"SSBZ
    Ͱ഑ྻͱͯ͠ग़ྗ
    wKTPO@FODPEF
    Ͱ+40/ʹ͢Δ

    View full-size slide

  34. "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());

    }

    }

    View full-size slide

  35. 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ΫϥεͰ͸ͳ͍

    View full-size slide

  36. D
    .BTBTIJ4IJOCBSB!TIJOY
    "SSBZBCMFΠϯλʔϑΣΠε
    w"QJ$POUSPMMFSKTPO
    ͕

    ඞཁͳґଘΛΠϯλʔϑΣΠεͰ໌ࣔ
    wΠϯλʔϑΣΠε࣮͑͞૷͍ͯ͠Ε͹

    ͲΜͳΫϥεͰ΋ྑ͍
    w࢓༷ͱ࣮૷ͷ෼཭

    View full-size slide

  37. "SSBZBCMF
    interface Arrayable

    {

    public function toArray(): array;

    }

    View full-size slide

  38. *UFN
    class Item implements Arrayable

    {

    (snip)
    public function toArray(): array

    {

    return [

    'code' => $this->code,

    'price' => $this->price->toInt(),

    ];

    }

    }

    View full-size slide

  39. $BSU
    class Cart implements Arrayable

    {

    (snip)
    public function toArray(): array

    {

    return array_map(function (Item $item) {

    return $item->toArray();

    }, $this->items);

    }

    }

    View full-size slide

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

    View full-size slide

  41. D
    .BTBTIJ4IJOCBSB!TIJOY
    ˞ิ଍
    1)1ʹ͸ɺ+TPO4FSJBMJ[BCMFͱ͍͏

    ΠϯλʔϑΣΠε͕͋Γ·͢ɻ

    ΦϒδΣΫτΛ+40/Խ͢Δࡍ͸

    ࢀߟʹ͍ͯͩ͘͠͞ɻ
    http://php.net/manual/ja/class.jsonserializable.php

    View full-size slide

  42. ࣗ༝Λୣ͏
    ໘౗ͳ΋ͷͰ͸ͳ͘

    View full-size slide

  43. டংΛकΓ
    ҙਤΛ໌֬ʹ͢Δ΋ͷ
    ੍໿
    ΘΜͺ͘ͳίʔυ
    https://flic.kr/p/4e5ggf
    ίʔυͷடংΛ
    कΔ΋ͷ

    View full-size slide

  44. ੍໿Ͱடং͋Δ։ൃΛʂ

    View full-size slide

  45. D
    .BTBTIJ4IJOCBSB!TIJOY
    !TIJOY
    !TIJOY
    D
    .BTBTIJ4IJOCBSB!TIJOY

    View full-size slide

  46. D
    .BTBTIJ4IJOCBSB!TIJOY
    QSPUFDUFEPSQSJWBUF
    w·ͣ͸ɺQSJWBUFඞཁͳΒQSPUFDUFE
    w͍͔ͭݺ͹ΕΔ͔΋͠Εͳ͍͔ΒQSPUFDUFE

    :"(/*ʢ:PVBJOUHPOOBOFFEJUʣ
    wଞਓ͕࡞ͬͨγεςϜΛϝϯςφϯε͢Δͱ

    QSJWBUFສࡀʹͳΔ
    wUSBJU͕͋ΔͷͰɺػೳͷͨΊʹܧঝ͢Δඞཁ͕ແ͍

    View full-size slide