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

#phpcon 良いコードを書けるようになるコツは「エラーを気にする」 〜プログラマにとってエラーとは何なのか〜

#phpcon 良いコードを書けるようになるコツは「エラーを気にする」 〜プログラマにとってエラーとは何なのか〜

PHP Conference Japan 2023での発表資料です。
https://fortee.jp/phpcon-2023/proposal/b2568517-f8ae-4ae7-85f4-89adc624fcda

hideki kinjyo

October 08, 2023
Tweet

More Decks by hideki kinjyo

Other Decks in Programming

Transcript


  1. PHP Conference 2023


    Hideki Kinjyo


    GitHub: o0h / Twitter: @o0h_


    [ൃද൛]

    View full-size slide

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


    View full-size slide

  3. ⏳estimate: 0 1 /25

    View full-size slide

  4. ⏳estimate: 0 1 /25

    View full-size slide


  5. ⏳estimate: 0 1 /25

    View full-size slide

  6. ҧ͏ʂʂ
    Τϥʔ͸ఢ͡Όͳ͍ɺ
    ༑ୡͳΜͩʂʂ
    ⏳estimate: 1 /25

    View full-size slide

  7. ͜ΜͳࣄΛ࿩͍͖ͯ͠·͢
    PHP

    ⏳estimate: 1 2 /25
    ˒খ͞ͳએ఻ίʔφʔ˒

    View full-size slide

  8. ͜Μͳ΋ͷΛ࣋ͬͯؼͬͯ΋Β͍͍ͨ
    PHP

    ⏳estimate: 1 2 /25

    View full-size slide

  9. ͓͠ͳ͕͖
    1.
    2. PHP
    3.
    4. PRO
    5. PHP

    ⏳estimate: 1 2 /25

    View full-size slide

  10. ϓϩάϥϛϯάݴޠ͸ͳͥʮΤϥʔʯΛݴͬͯ͘Δͷʁ
    =

    ⏳estimate: 1 2 /25

    View full-size slide

  11. PHP Software Developers



    ⏳estimate: 1 2 /25

    View full-size slide

  12. PHP Software Developers



    ૝͍
    ⏳estimate: 2 /25

    View full-size slide

  13. ⏳estimate: 2 /25

    View full-size slide

  14. ⏳estimate: 2 /25

    View full-size slide

  15. Τϥʔͷ͋Δੈքɺͳ͍ੈքɾɾɾͲ͕ͬͪྑ͍Ͱ͔͢ʁ
    :
    31
    :
    :
    :

    ⏳estimate: 2~3 /25

    View full-size slide

  16. Τϥʔͷ͋Δੈքɺͳ͍ੈքɾɾɾͲ͕ͬͪྑ͍Ͱ͔͢ʁ
    (= )
    =>
    =>
    =>

    ⏳estimate: 2~3 /25

    View full-size slide

  17. Τϥʔͷ͋Δੈքͷํ͕ɺ༏͍͠ੈք
    2

    ⏳estimate: 2~3 /25

    View full-size slide

  18. PHP 👀
    ⏳estimate: 2~3 /25

    View full-size slide

  19. ྫ͑͹sprintf()ͷྫ
    (%
    )
    switch

    src:
    IUUQTHJUIVCDPNQIQQIQTSDCMPCQIQFYUTUBOEBSEGPSNBUUFE@QSJOUD--

    View full-size slide

  20. ྫ͑͹sprintf()ͷྫ
    case (default )

    ⏳estimate: 3 /25

    View full-size slide

  21. ྫ͑͹sprintf()ͷྫ

    src:
    IUUQTHJUIVCDPNQIQQIQTSDCMPCQIQFYUTUBOEBSEGPSNBUUFE@QSJOUD--
    (% )

    View full-size slide


  22. ⏳estimate: 3 /25

    View full-size slide

  23. Τϥʔɺ͑Βʙ͍👏
    :
    :

    ⏳estimate: 3~4 /25

    View full-size slide

  24. §2 PHP
    .1
    .2 PHP

    View full-size slide

  25. Τϥʔʹ͸ʮఔ౓ʯ͕͋Δͧ
    ( )


    ⏳estimate: 4 /25

    View full-size slide

  26. [ࣸਅ]


    (্) ৅ҹ ενʔϜࣜՃ࣪ث EE-RQ35ܕ/EE-RQ50ܕ औѻઆ໌ॻ


    (Լ) ΞΠϦεΦʔϠϚ ਅۭอଘϑʔυγʔϥʔ VPF-385T औѻઆ໌ॻ


    ࢀߟ: Ոి඼Λ҆શʹ࢖͏ͨΊͷϚʔΫʛҰൠࡒஂ๏ਓՈి੡඼ڠձ https://aeha.or.jp/safety/mark/index.html
    ৘ใͷ෼ྨͷྫ: ՈిͷϚχϡΞϧ
    :
    :

    View full-size slide

  27. ৘ใͷ෼ྨͷྫ: ՈిͷϚχϡΞϧ

    [ࣸਅ]


    ৅ҹ ενʔϜࣜՃ࣪ث EE-RQ35ܕ/EE-RQ50ܕ औѻઆ໌ॻ

    View full-size slide

  28. ৘ใ͕෼ྨ͞Ε͍ͯΔͱศརͰ͢Ͷʂ

    ⏳estimate: 5 /25

    View full-size slide

  29. ͲΜͳʮఔ౓ʯ͕͋Δʁ
    :
    :
    ( ):
    :

    ⏳estimate: 5~6 /25

    View full-size slide

  30. §2 PHP
    .1
    .2 PHP
    ⏳estimate: 5~6 /25

    View full-size slide

  31. PHPʹ͓͚ΔΤϥʔఆ਺(ΤϥʔϨϕϧ)

    IUUQTXXXQIQOFUNBOVBMKBFSSPSGVODDPOTUBOUTQIQ

    View full-size slide

  32. PHPͷΤϥʔܥ౷

    View full-size slide

  33. PHPͷΤϥʔܥ౷

    ͦΕͧΕɺ఻͑Α͏ͱ͍ͯ͠Δҙਤ͕ҧͬͨΓ

    ΋ͨΒ݁͢Ռ͕ҧ͏Μͩͳʙ ۠ผ͕͋ΔΜͩͳʙ

    ⏳estimate: 6 /25

    View full-size slide

  34. PHPͷΤϥʔͨͪʙ͜Ε͚ͩ͸֮͑Α͏ʙ
    E_ERROR => [ ] PHP
    E_WARNING => [ ] PHP
    E_NOTICE => [ ] PHP

    ⏳estimate: 6 /25

    View full-size slide

  35. E_ERROR / ॏେ
    (Throwable)

    ⏳estimate: 6~7 /25

    View full-size slide

  36. E_WARNING / ܯࠂ
    ( )
    ( ) URL

    View full-size slide

  37. E_NOTICE / ஫ҙ

    View full-size slide

  38. §3
    .1
    .2
    ⏳estimate: 7 /25

    View full-size slide

  39. ྑ͘ͳͦ͞͏ͳίʔυͱ͍͏ͷ͕͋Δ

    ⏳estimate: 7 /25

    View full-size slide

  40. ᐆດ͞ʹ͍ͭͯ: shin1x1͞Μʮ੍໿ͷྗʯ

    https://speakerdeck.com/shin1x1/restricting-states
    ⏳estimate: 7~8 /25

    View full-size slide

  41. ᐆດ͞ʹ͍ͭͯ: shin1x1͞Μʮ੍໿ͷྗʯ

    https://speakerdeck.com/shin1x1/restricting-states
    ⏳estimate: 7~8 /25

    View full-size slide

  42. ᐆດ͞ʹ͍ͭͯ: shin1x1͞Μʮ੍໿ͷྗʯ

    https://speakerdeck.com/shin1x1/restricting-states
    ⏳estimate: 7~8 /25

    View full-size slide

  43. ᐆດ͞ʹ͍ͭͯ: shin1x1͞Μʮ੍໿ͷྗʯ

    https://speakerdeck.com/shin1x1/restricting-states
    ⏳estimate: 8 /25

    View full-size slide

  44. ᐆດ͞ʹ͍ͭͯ: t-wada͞Μͷʮ༧๷ʹউΔ๷ޚͳ͠ʯ

    https://speakerdeck.com/twada/growing-reliable-code-phperkaigi-2022
    ⏳estimate: 8 /25

    View full-size slide

  45. ᐆດ͞ʹ͍ͭͯ: t-wada͞Μͷʮ༧๷ʹউΔ๷ޚͳ͠ʯ

    https://speakerdeck.com/twada/growing-reliable-code-phperkaigi-2022
    ⏳estimate: 8 /25

    View full-size slide

  46. ྑ͘ͳ͍ίʔυ͕͜͏ͯ͠ੜ·ΕΔͧ
    ( )

    ⏳estimate: 8~9 /25

    View full-size slide

  47. ΤϥʔͱՄೳੑ




    ⏳estimate: 8~9 /25

    View full-size slide

  48. ͜ͷίʔυ͸ո͍͕͠ɾɾɾ

    ⏳estimate: 9 /25

    View full-size slide

  49. ͜ͷίʔυ͸ո͍͕͠ɾɾɾ
    PHP

    ⏳estimate: 9~10 /25

    View full-size slide

  50. §3
    .1
    .2
    ⏳estimate: 9~10 /25

    View full-size slide

  51. Α͘ͳͦ͞͏ͳίʔυΛආ͚ΔͨΊʹ

    ⏳estimate: 10 /25

    View full-size slide

  52. Τϥʔɺcase-by-case
    ( ) ( )

    ⏳estimate: 10 /25

    View full-size slide

  53. ࣮ྫʂϓϩάϥϛϯά
    ( )


    ⏳estimate: 10~11 /25

    View full-size slide

  54. ࣮ྫʂPHP
    ( )

    View full-size slide

  55. ࣮ྫʂPHP
    :

    include E_WARNING
    require E_ERROR

    https://www.php.net/manual/ja/function.include.php

    ⏳estimate: 10~11 /25

    View full-size slide

  56. ΤϥʔϨϕϧ͸ओ؍తʹܾ·Δ(ଆ໘΋͋Δ)

    ⏳estimate: 11 /25

    View full-size slide

  57. ίϨ͸͋ͳͨͷ௚؍ʹ߹͏ʁ
    E_ERROR

    View full-size slide

  58. ΤϥʔʹࣖΛ܏͚͍ͯ͜͏ʙ

    ⏳estimate: 11 /25

    View full-size slide

  59. ΤϥʔʹࣖΛ܏͚͍ͯ͜͏ʙ
    
 



    ⏳estimate: 11~12 /25

    View full-size slide

  60. Τϥʔɺා͍Ͱ͔͢ʁ
    ( )

    ⏳estimate: 12 /25

    View full-size slide

  61. ΤϥʔͷPRO͕ɺ஌͍ͬͯΔ͜ͱ
    
 

    ( )



    ⏳estimate: 12~13 /25

    View full-size slide

  62. Τϥʔ͕࣍ͷʮ૝ఆ֎ʯʮᐆດʯΛݺͿ
    (
    )

    [ग़య]

    ᴷAndreas Zeller, தా लج, ࠓా ণ޺, େؠ ঘ޺, ஛ా ߳බ, ٶݪ ٱඒࢠ, फܗ ࣿ৫

    σόοάͷཧ࿦ͱ࣮ફ ―ͳͥϓϩάϥϜ͸͏·͘ಈ͔ͳ͍ͷ͔ P3 - P4

    View full-size slide

  63. όά͸ʮൟ৩ʯ͢Δ
    1 1

    [ग़య]

    ᴷ Shore, James, Warden, Shane, ฏು ݈ࣇ, ໦Լ ࢙඙, ࡫Ҫ ਸ࢘

    ΞʔτɾΦϒɾΞδϟΠϧσϕϩοϓϝϯτ : ૊৫Λ੒ޭʹಋ͘ΤΫετϦʔϜϓϩάϥϛϯά P169

    View full-size slide

  64. Τϥʔ͕ڭ͑ͯ͘ΕΔ͜ͱɾ͘Εͳ͍͜ͱ


    ⏳estimate: 13~14 /25

    View full-size slide

  65. ͖ͬ͞ͷίʔυ: Կ͕ਖ਼͍͠ʁ

    $taxRate
    1.8
    fl
    oat
    0.8
    fl
    oat
    80(%) int
    ⏳estimate: 13~14 /25

    View full-size slide

  66. ผͷྫ: ߏจΤϥʔ
    ( )
    7 if{}
    if {} 🤮

    ⏳estimate: 13~14 /25

    View full-size slide

  67. ผͷྫ: ߏจΤϥʔɺ͜ΕͩͬͨΒͲ͏ݟ͑Δʁ
    class{}

    View full-size slide

  68. ผͷྫ: ߏจΤϥʔɺݟͨ໨ͰมΘΒͳ͍҆৺ײ

    ⏳estimate: 14 /25

    View full-size slide

  69. Τϥʔͱ஥ྑ͘ͳΔʹ͸
    PRO

    ⏳estimate: 14~15 /25

    View full-size slide

  70. Τϥʔ͸ఢ͡Όͳ͍ɺ༑ୡͳΜͩʂʂ →ͦͷ৺͸ʁ

    ⏳estimate: 15~16 /25

    View full-size slide

  71. Τϥʔͱ஥ྑ͘ͳΔͨΊͷίπ΋௫Έ·͠ΐ͏ʂ

    ( )
    =
    ( )

    ⏳estimate: 16 /25

    View full-size slide

  72. §5 PHP
    .1
    .2
    ⏳estimate: 16~17 /25

    View full-size slide

  73. ϓϩάϥϛϯάʹͱͬͯΤϥʔ͸ॏཁ

    ⏳estimate: 16~17 /25

    View full-size slide

  74. PHP͸Ͳ͏͖ͯͨ͠ʁˠΑ͘࿩୊ʹͳΔ΍ͭ
    PHP5 -> PHP7
    Throwable/Error
    E_STRICT : E_NOTICE, E_WARNING, E_DEPRECATED
    PHP7 -> PHP8
    warning -> Error
    PHP8 -> PHP9
    Error (8.2 Deprecated)
    Error

    View full-size slide

  75. ᐆດ͞ͱͷઓ͍
    PHP

    ⏳estimate: 17~18 /25

    View full-size slide

  76. ᐆດ͞ͱͷઓ͍
    E_WARNING/E_NOTICE Error throw
    E_NOTICE (7.4 )
    E_NOTICE php.ini these are warnings
    which often result from a bug in your code, but it's possible
    that it was intentional

    ⏳estimate: 17~18 /25

    View full-size slide

  77. php-src grep
    7.x NOTICE
    WARNING
    8.x
    WARNING
    E_NOTICE/

    E_WARNINGͷग़ݱ਺
    git --no-pager grep E_NOTICE -- 'ext/**/*.c' | wc -l

    View full-size slide

  78. RFCͳͲ
    PHP: rfc:engine_warnings

    https://wiki.php.net/rfc/engine_warnings
    PHP: rfc:consistent_type_errors

    https://wiki.php.net/rfc/consistent_type_errors
    Pull requests · php/php-src

    https://github.com/php/php-src/pulls?q=label%3A%22Warning+promotion%22
    PHP: rfc:unde
    fi
    ned_property_error_promotion

    https://wiki.php.net/rfc/unde
    fi
    ned_property_error_promotion
    PHP: rfc:unde
    fi
    ned_variable_error_promotion

    https://wiki.php.net/rfc/unde
    fi
    ned_variable_error_promotion

    View full-size slide

  79. ͜Ε͔ΒͲ͏ͳΓͦ͏ʁ
    NOTICE

    PHP
    Error WARNING
    (DEPRECATED )
    TypeError/ValueError

    View full-size slide

  80. ͜Ε͔ΒͲ͏͍ͯ͘͠ʁ
    PHP7.0

    ⏳estimate: 20 /25

    View full-size slide

  81. ͜Ε͔ΒͲ͏͍ͯ͘͠ʁ
    E_NOTICE/E_WARNING
    (`===` strict )
    PHPStan Psalm
    PHP

    ⏳estimate: 21 /25

    View full-size slide

  82. Τϥʔͱ্ख͘෇͖߹͏ͱɺ೉͕͠͞ݮΔ
    PHP


    ⏳estimate: 21~22 /25

    View full-size slide

  83. ͓͠·͍ʂ
    ͓෇͖߹͍͍͖ͨͩ

    ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide