$30 off During Our Annual Pro Sale. View Details »

型があると嬉しい〜って本当??

 型があると嬉しい〜って本当??

hideki kinjyo
PRO

July 31, 2023
Tweet

More Decks by hideki kinjyo

Other Decks in Programming

Transcript

  1. が
    ͳʹ͔ͷษڧձͱ͔͕͋Δ༁Ͱ͸ͳ͍


    Hideki Kinjyo


    GitHub: o0h / Twitter: @o0h_


    View Slide

  2. ࣗݾ঺հ
    /
    GitHub: @o0h / Twitter: @o0h_
    FW CakePHP

    View Slide

  3. Πϯτϩ

    PHP
    PHP


    View Slide

  4. ( )
    https://fortee.jp/phpcon-2021/proposal/6cd22f6c-9caa-432f-930c-9e62f512d59d

    View Slide


  5. View Slide

  6. View Slide

  7. https://www.php.net/manual/ja/language.types.intro.php

    View Slide

  8. https://www.php.net/manual/ja/language.types.intro.php
    PHP
    ( )
    PHP

    View Slide

  9. https://www.php.net/manual/ja/language.types.intro.php
    10000
    3

    View Slide

  10. https://www.php.net/manual/ja/language.types.intro.php
    =

    View Slide

  11. https://www.php.net/manual/ja/language.types.intro.php
    ( )
    Web

    View Slide

  12. shin1x1͞Μͷൃදʮ੍໿ͷྗʯ

    https://speakerdeck.com/shin1x1/restricting-states

    View Slide

  13. shin1x1͞Μͷൃදʮ੍໿ͷྗʯ

    https://speakerdeck.com/shin1x1/restricting-states

    View Slide

  14. shin1x1͞Μͷൃදʮ੍໿ͷྗʯ

    https://speakerdeck.com/shin1x1/restricting-states

    View Slide

  15. shin1x1͞Μͷൃදʮ੍໿ͷྗʯ

    https://speakerdeck.com/shin1x1/restricting-states

    View Slide

  16. t-wada͞Μͷൃදʮ༧๷ʹউΔ๷ޚͳ͠ʯ

    https://speakerdeck.com/twada/growing-reliable-code-phperkaigi-2022

    View Slide

  17. t-wada͞Μͷൃදʮ༧๷ʹউΔ๷ޚͳ͠ʯ

    https://speakerdeck.com/twada/growing-reliable-code-phperkaigi-2022

    View Slide

  18. ܕΛ͚ͭΔͧʂʂͷϞνϕʔγϣϯ
    ( )

    View Slide

  19. :

    View Slide

  20. PHPͷܕએݴ
    PHP
    php.net
    https://www.php.net/manual/ja/language.types.declarations.php

    View Slide

  21. (
    )

    View Slide

  22. int
    $l, $r int
    int
    NULL
    DB
    (= )

    View Slide

  23. int
    int
    int
    int
    (
    )

    View Slide

  24. PHP7
    ( )

    ࢀߟ

    ݫີͳܕ෇͚

    IUUQTXXXQIQOFUNBOVBMKBMBOHVBHFUZQFTEFDMBSBUJPOTQIQMBOHVBHFUZQFTEFDMBSBUJPOTTUSJDU

    View Slide

  25. خ͍͠Ͱ͔͢ʁ
    int
    t-wada

    View Slide

  26. خ͍͠Ͱ͔͢ʁ
    = (DB HTTP ) etc
    ( )
    =
    int
    =>

    View Slide

  27. int
    /
    $response->getBody() $body-
    >eof()
    string,
    bool, Response, Stream

    ࢀߟ

    IUUQTHJUIVCDPNTMJNQIQ4MJNCMPC4MJN
    3FTQPOTF&NJUUFSQIQ

    View Slide

  28. Interface

    ࢀߟ

    IUUQTHJUIVCDPNTMJNQIQ4MJNCMPC4MJN
    3FTQPOTF&NJUUFSQIQ
    StreamInterface
    bool
    int: 0

    (Content-Length )
    int
    int
    string
    bool

    View Slide

  29. ܕͱͷ෇͖߹͍ํ
    ( )
    Interface
    strict_types=1
    private
    public

    View Slide

  30. :

    View Slide

  31. ΨϯΨϯܕએݴΛ͢Δ͜ͱͷऑ఺ͱ͔
    PHPDoc
    PHP
    PHP

    View Slide

  32. ੩తղੳπʔϧΛ࢖͓͏ / PHPStan
    https://phpstan.org/
    PHPStan scans your whole codebase and looks for both
    obvious & tricky bugs. Even in those rarely executed if
    statements that certainly aren't covered by tests.
    ( )

    View Slide

  33. ੩తղੳπʔϧΛ࢖͓͏ / Psalm
    https://psalm.dev/
    Psalm is a static analysis tool that attempts to dig into
    your program and
    fi
    nd as many type-related bugs as
    possible.
    PHPStan

    View Slide

  34. ੩తղੳπʔϧΛ࢖͓͏ / PhpStorm
    PHP
    PHPStan Psalm
    lint
    ( )

    View Slide

  35. PHPStan

    View Slide

  36. =
    CI

    ࢀߟ

    IUUQTQIQTUBOPSHSGDGBDGCDCEGC

    View Slide

  37. PHPDoc
    @param / @return

    ࢀߟ

    IUUQTQIQTUBOPSHSGFFECEEEDG

    View Slide

  38. :
    `key-of-
    <***>` `value-of-<***>`
    ( )

    ࢀߟ

    IUUQTQIQTUBOPSHSCDGDFCBCCBBEE

    View Slide

  39. :
    int
    `int`

    ࢀߟ

    IUUQTQIQTUBOPSHSEFFGDDCCBEEFE

    View Slide

  40. :
    (key, value
    )
    iterable
    array
    mixed

    ࢀߟ

    IUUQTQIQTUBOPSHSEG
    ff
    EDFBBBFD

    View Slide

  41. :
    DTO

    ࢀߟ

    IUUQTQIQTUBOPSHSDCFGGCGGGFE

    View Slide

  42. ৭ʑͳܕͷදݱ
    : https://phpstan.org/writing-php-
    code/phpdoc-types

    View Slide

  43. PHPStan

    View Slide

  44. Πϯετʔϧ͢Δ

    View Slide

  45. ઃఆϑΝΠϧΛઃஔ͢Δ

    https://phpstan.org/con
    fi
    g-reference

    View Slide

  46. ࣮ߦ͢Δ

    View Slide

  47. ( OK)

    View Slide

  48. ಋೖ࣌ʹ͓֮͑ͯ͘ͱྑͦ͞͏ͳ֓೦
    Error
    Rule/RuleSet
    Rule RuleSet Rule
    Rule level
    PHPStan Ruleset
    0 9(max)
    level

    View Slide

  49. Rule Levelͷྫ

    https://github.com/phpstan/phpstan-src/blob/1.10.25/conf/con
    fi
    g.level6.neon

    View Slide

  50. ಋೖ࣌ʹ͓֮͑ͯ͘ͱྑͦ͞͏ͳ֓೦
    Baseline
    ( )
    Error
    `phpstan analyze generate-baseline`
    Error
    ignoreErrors
    con
    fi
    g 1 (
    )Error
    Baseline ignoreErrors

    View Slide

  51. Baselineͷྫ(CakePHP)

    https://github.com/cakephp/cakephp/blob/4.4.15/phpstan-baseline.neon

    View Slide

  52. PHPStanͷ΍͍͞͠ಋೖʹ޲͚ͯ
    PJ
    Level OK
    Baseline
    PHPStan ignoreErrors( Baseline )
    Error

    (cf: https://phpstan.org/user-guide/ignoring-errors#reporting-unused-
    ignores)

    View Slide

  53. PHPStanͷ΍͍͞͠ಋೖʹ޲͚ͯ
    PJ Level CI
    5 6

    View Slide

  54. PHPStanͷ΍͍͞͠ಋೖʹ޲͚ͯ

    Error
    etc

    View Slide

  55. ΋ͬͱڧྗͳ׆༻ʹ޲͚ͯ
    PHPStan
    https://phpstan.org/writing-php-code/phpdoc-types
    https://phpstan.org/con
    fi
    g-reference
    https://phpstan.org/user-guide/extension-library
    OSS

    View Slide

  56. ͱͯ΋ࢀߟʹͳΔࢿྉͨͪ
    PHPStan #phpcon_okinawa pixivFANBOX

    https://tadsan.fanbox.cc/posts/4348669
    #phpcon_okinawa
    pixivFANBOX

    https://tadsan.fanbox.cc/posts/2558149
    level=0 PHPStan(Larastan) - Shin x Blog

    https://blog.shin1x1.com/entry/getting-stated-with-phpstan
    CI / Great static analysis with CI -
    Speaker Deck

    https://speakerdeck.com/oogfranz/great-static-analysis-with-ci

    View Slide