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

防衛的 PHP: 多様性を生き抜くための PHP 入門 / Defensive PHP

防衛的 PHP: 多様性を生き抜くための PHP 入門 / Defensive PHP

Naoki Ikeguchi

March 24, 2023
Tweet

More Decks by Naoki Ikeguchi

Other Decks in Programming

Transcript

  1. ๷Ӵత 1)1
    ଟ༷ੑΛੜ͖ൈͨ͘Ίͷ 1)1ೖ໳
    !TO@KQ
    QIQFSLBJHJ B

    View full-size slide

  2. XIPBNJ
    • αʔόαΠυΤϯδχΞ !ΏΊΈ
    • ࡾ౓ͷ൧ΑΓ੩తܕ෇͚͕޷͖
    • 1)1 3VTU (P $ FUD
    • -BSBWFMΑΓ 4ZNGPOZ೿
    • +FU#SBJOT৴ऀ
    • 3VTU Ͱ 1)1ͷ 4"1*Λ࣮૷͢Δ
    ͷ͕झຯ
    2
    @s6n_jp

    View full-size slide

  3. ੩తղੳͬͯԿ
    3

    View full-size slide

  4. 'PSNBUUFS
    ౳Ձͳίʔυͷ··εϖʔγϯά΍ΠϯσϯτɼෆཁͳτʔΫϯ
    ͳͲΛ੔͑Δ
    4
    $foo = (function ($a, $b, $c) {})();
    $bar = array(“a”, “b”,);
    $foo= ( function($a,$b, $c ){ }, )();
    $bar = [
    ‘a’,
    ‘b’,
    ];

    View full-size slide

  5. -JOUFS -JOU

    • ৑௕ͳॻ͖ํɼݹ͍ॻ͖ํ΍ɼҰൠʹΑ͘ͳ͍ͱ͞ΕΔॻ͖ํ
    ͳͲΛݕ஌͢Δɾผͷॻ͖ํʹม͑Δ
    • ഁյతͳมߋΛߦ͏ɾఏҊ͢Δ৔߹΋͋Δ
    • ͨͱ͑͹ 1)1Ͱ͸ declare(strict_types=1); Λ௥Ճ͢Δ
    ϧʔϧ͕͜Εʹ͋ͨΔ
    • ࣗಈతʹमਖ਼Ͱ͖ͳ͍΋ͷ΋͋Δ
    5

    View full-size slide

  6. 5ZQF$IFDLFS
    • ܕΛݕࠪ͢Δ ͦͷ··

    6

    View full-size slide

  7. ଞͷݴޠʹ͓͚Δ੩తղੳ
    7

    View full-size slide

  8. 1)1ͷ੩తղੳπʔϧ
    • 1)1@$PEF4OJGGFS
    • 1)1$4'JYFS
    • 1)1.FTT%FUFDUPS
    • 1)14UBO
    • 1TBMN
    • 3FDUPS
    FUDʜ
    8

    View full-size slide

  9. 9
    ଟ͗͢Δ
    ͷͰͱΓ͋͑ͣೖΕ͓ͯ͘΂͖ ͭΛ঺հ͠·͢

    View full-size slide

  10. 8IZ੩తղੳ
    10

    View full-size slide

  11. ܕͷऑ͔ͬͨݴޠ͕ܕΛٻΊ͍ͯΔ
    11

    View full-size slide

  12. ਓྨ͸ܕΛٻΊΔ
    • ਓ͸खଓ͖ܕݴޠʹ;ΕΔͱߴจ຺ͳίʔυΛੜ࢈͢Δ
    • ܕ͸ߟ͑Δ͜ͱΛ࠷খʹ͢ΔखஈͰ͋Δ
    12

    View full-size slide

  13. ϏϧυλΠϜͰ஌ΕΔʹӽͨ͜͠ͱ͸ͳ͍
    ͋͞օ͞Μ͝Ұॹʹɺ
    • ίϯύΠϧΤϥʔ͸ී௨
    • ίϯύΠϧΤϥʔ͕ग़ͨΒ͋Γ͕ͱ͏
    • ίϯύΠϧΤϥʔ͕ग़ͨΒେتͼ
    13
    l
    ߐఴ྄ɽzߐఴ྄ͷ$ೖ໳zɽɽIUUQTF[PFSZPVHJUIVCJPDQQJOUSP

    View full-size slide

  14. ๷Ӵతͳίʔυϕʔε
    14

    View full-size slide

  15. ͭͷ๷Ӵ
    • ࣗ෼ͷϛε͔Βͷ๷Ӵ
    • ࣗ෼Ͱॻ͍ͨίʔυͷจ຺Λ׬શʹ͍֮͑ͯΒΕΔΘ͚Ͱ͸ͳ͍
    • ແବͳࢥߟ͔Βͷ๷Ӵ
    • ίʔυͷॻ͖ํ͸ຊ࣭Ͱ͸ͳ͍
    • ॻ͖ํΛࣗಈͰ౷Ұ͢Δ͜ͱͰίʔυ্ͷຊ࣭ʹूதͰ͖Δ
    • ଞਓͷॻ͘ίʔυ͔Βͷ๷Ӵ
    • ίʔυϕʔεதͷίʔυͷ࣭͸ҰఆʹอͨΕΔ΂͖Ͱ͋Δ
    • ҉໧ͷ
    ίʔσΟϯάن໿͸कΒΕͳ͍
    15

    View full-size slide

  16. ॻ͘ਓͷ਺͚ͩҧ͏ίʔυ͕ੜ·ΕΔ
    • Πϯσϯτ͸εϖʔε λϒ
    • ԋࢉࢠͷपΓʹεϖʔεΛೖΕΔ ೖΕͳ͍
    • BSSBZ@NBQBSSBZ@GJMUFSΛ࢖͏ GPSFBDIΛ࢖͏
    • ͜ͷॻ͖ํ͸ݹ͍ ৽͍͠
    ˠίʔυͷຊ࣭͸ͦ͜Ͱ͸ͳ͍
    ˠ੩తղੳʹΑͬͯຊ࣭ʹूதͰ͖ΔΑ͏ʹ͢Δ
    17

    View full-size slide

  17. 143ͱ͸
    • 1)1'*( 'SBNFXPSL*OUFSPQFSBCJMJUZ(SPVQ
    ʹΑͬͯ
    ࡦఆ͞ΕͨϑϨʔϜϫʔΫؒͷ૬ޓӡ༻ੑΛ֬อ͢ΔͨΊͷ
    ඪ४΍ "CTUSBDUJPO
    • ͨͱ͑͹ʜʜ
    • 143143ΦʔτϩʔσΟϯά
    • 143143143ίʔσΟϯάελΠϧ
    • 143)551ϝοηʔδΠϯλʔϑΣʔε
    • 143$MPDLˡ/&8
    19

    View full-size slide

  18. 143
    • 143Λݩʹͨ͠ίʔσΟϯάελΠϧඪ४
    • ͨͱ͑͹ʜʜ
    • 1)1ϑΝΠϧ͸ -'ͷΈΛ࢖͏ .645

    • FYUFOET΍ JNQMFNFOUT͸Ϋϥε໊ͱಉ͡ߦʹॻ͘ .645

    • 5SBJUͷ VTFจ͸ DMBTTͷ։͔࢝ͬ͜ͷ͙͢ޙ .645

    • ͢΂ͯͷϝιουʹ͸ 7JTJCJMJUZΛॻ͘ .645

    • BCTUSBDU GJOBM TUBUJD͸ 7JTJCJMJUZͷલʹॻ͘ .645

    • FMTFJGͰ͸ͳ͘ FMTFJGΛ࢖͏ 4)06-%

    FUDʜ
    20

    View full-size slide

  19. 1)1ͷ੩తղੳೖ໳
    21

    View full-size slide

  20. QIQM
    • ҙ֎ͱ஌ΒΕ͍ͯͳ͍ ࢲ΋஌Βͳ͔ͬͨ
    ੩తղੳπʔϧ
    • ࣮ߦ࣌ͷ 4ZOUBY&SSPSΛ๷͛Δ
    22

    View full-size slide

  21. 1)1$4'JYFS
    24
    • ϧʔϧηοτ
    • !4ZNGPOZ !4ZNGPOZSJTLZ
    ͜Ε͸ !143Λܧঝ͢Δ

    • !1IQ$T'JYFS !1IQ$T'JYFSSJTLZ

    • !1)1.JHSBUJPO !1)1.JHSBUJPOSJTLZ

    • !1)16OJU.JHSBUJPOSJTLZ

    View full-size slide

  22. &$4 ίϯςφ͕ಈ͔ͳ͍΄͏

    26
    • 0VUPG#PYͰ࢖͑Δ
    • 1)1$4'JYFSͷࡉʑͱͨ͠ϧʔϧ͸࢖͑ͳ͍
    • &$41)1$4'JYFS1)1$44ZNQMJGZ

    View full-size slide

  23. 1)14UBOฤ
    • ϨϕϧΛ d͔Β͑ΒͿ ͕͓͢͢Ί

    • 4ZNGPOZͰ࢖͏৔߹͸ &YUFOTJPOΛೖΕΔ
    • ΄΅ 0VUPG#PYͰ࢖͑Δ
    • ࠷ॳ͸ෆཁͳϧʔϧΛҰ୴ແޮԽ͍͍ͯ͠ͱࢥ͏
    28

    View full-size slide

  24. 3FDUPSฤ
    • $0%&@26"-*5: %&"%@$0%& 1)1@d1)1@
    5:1&@%&$-"3"5*0/͋ͨΓͷ 4FU-JTUΛ࢖͏
    • JNQPSU/BNFTΛઃఆ͠ͳ͍ͱ '2$/͕ॻ͔ΕΔͷͰ஫ҙ
    30

    View full-size slide

  25. ੩తղੳͱ 1IQ4UPSN
    33

    View full-size slide

  26. ͋ͳͨͷ 1IQ4UPSN ͸࠷దͳઃఆ
    34

    View full-size slide

  27. ͋ͳͨͷ 1IQ4UPSN ͸࠷దͳઃఆ
    35

    View full-size slide

  28. ͏Ε͍͠χϡʔε
    38

    View full-size slide

  29. 1IQ4UPSN͸ $* Ͱಈ͖·͢
    39

    View full-size slide

  30. 1IQ4UPSN͸ $* Ͱಈ͖·͢
    brew install jetbrains/utils/qodana
    qodana scan –-show-report
    • +BWB ,PUMJO 1)1 1ZUIPO +BWB4DSJQU 5ZQF4DSJQU (P
    $ ' 7#/&5 BOENPSF
    • 4"3*'ܗࣜͰͷग़ྗ΋Մೳ
    • ࠓͳΒແྉͰ࢖͑·͢
    40

    View full-size slide

  31. 0OF.PSF5IJOH
    41

    View full-size slide

  32. ࠓ೔঺հͨ͠πʔϧΛ·ͱΊͯಋೖͰ͖·͢
    composer require --dev \
    quartetcom/static-analysis-kit:~8.1
    • 1)1$4'JYFS 1)14UBO 3FDUPSΛҰ౓ʹಋೖͰ͖·͢
    • 0VUPG#PYͰ࢖͑ΔͷͰ໘౗ͳઃఆ΋ෆཁ
    • ݫ͠Ίͷઃఆʹͯ͋͠ΔͷͰݫ͠Ί͕޷͖ͳํʹ͓͢͢Ί
    42

    View full-size slide

  33. 5IBOLZPV
    ൃදࢿྉ͸ͪ͜Β͔ΒIUUQTTQFBLFSEFDLDPNTJLFUZBO
    43

    View full-size slide