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

静的解析の育て方 / How to make your static analysis strong

Yuichi Sugiyama
February 10, 2020
2.5k

静的解析の育て方 / How to make your static analysis strong

Yuichi Sugiyama

February 10, 2020
Tweet

More Decks by Yuichi Sugiyama

Transcript

  1. ੩తղੳͷҭͯํ
    PHPerKaigi 2020
    Yuichi Sugiyama
    @oogFranz

    View full-size slide

  2. Whoami
    • ਿࢁ ༞Ұ @oogFranz
    • αΠϘ΢ζ5೥໨ΤϯδχΞ
    • ෳۀδϟζϛϡʔδγϟϯ@MASHݭָஂ
    • Spotify, Apple MusicͳͲͰ഑৴தʂ

    View full-size slide

  3. αΠϘ΢ζ Garoon
    • େن໛޲͚ͷάϧʔϓ΢ΣΞ
    • ੜ࢈ੑɾνʔϜϫʔΫ޲্ͷࢧԉ

    View full-size slide

  4. αΠϘ΢ζ Garoon
    • PHPͱMySQLͰͰ͖ͯΔWebΞϓϦέʔγϣϯ
    • ։ൃ18೥໨
    • PHP͸ 4͔Β7.2ʹ
    • PHPͷίʔυ͸180ສߦ͘Β͍

    View full-size slide

  5. αΠϘ΢ζ Garoon
    • PHPͱMySQLͰͰ͖ͯΔWebΞϓϦέʔγϣϯ
    • ։ൃ18೥໨
    • PHP͸ 4͔Β7.2ʹ
    • PHPͷίʔυ͸180ສߦ͘Β͍

    ͳ͔ͳ͔ϨΨγʔʂ

    View full-size slide

  6. ϨΨγʔίʔυΛ៉ྷʹͯ͠΍Δʂ
    ·ͣ͸طଘ࣮૷ΛΑ͘ಡΜͰΈΑ͏ʂ
    ϨΨγʔʹཱͪ޲͔͓͏ʂ

    View full-size slide

  7. খੴʹͭ·ͣ͘
    PHPDoc͕ͳͯ͘

    ίʔυ͕௥͑ͳ͍ɻɻɻ
    ͜͜ͷίʔυͣͬͱಡΜͰ͚ͨͲɺ

    ͦ΋ͦ΋୭΋ݺͼग़ͯ͠ͳ͍ɻɻɻʁ
    ͜͜ͷίʔυTypo͹͔ͬΓɻɻɻ

    View full-size slide

  8. খੴʹͭ·ͣ͘
    PHPDoc͕ͳͯ͘

    ίʔυ͕௥͑ͳ͍ɻɻɻ
    ͜͜ͷίʔυͣͬͱಡΜͰ͚ͨͲɺ

    ͦ΋ͦ΋୭΋ݺͼग़ͯ͠ͳ͍ɻɻɻʁ
    ͜͜ͷίʔυTypo͹͔ͬΓɻɻɻ

    ຊ࣭తͳ໰୊ʢઃܭͳͲʣʹͨͲΓண͘લʹɺ

    খ͞ͳ໰୊͕ؾʹͳͬͯίʔυϦʔσΟϯά͕ਐ·ͳ͍

    View full-size slide

  9. খੴʹͭ·ͣ͘
    PHPDoc͕ͳͯ͘

    ίʔυ͕௥͑ͳ͍ɻɻɻ
    ͜͜ͷίʔυͣͬͱಡΜͰ͚ͨͲɺ

    ͦ΋ͦ΋୭΋ݺͼग़ͯ͠ͳ͍ɻɻɻʁ
    ͜͜ͷίʔυTypo͹͔ͬΓɻɻɻ

    ·ͣ͸খੴΛআڈ͢Δɻ

    ੩తղੳπʔϧΛೖΕΑ͏ʂ

    View full-size slide

  10. ੩తղੳͱ͸ʁ
    • ίʔυΛ࣮ߦͤͣʹߦ͏ݕূ
    • ಈతղੳɿUnit TestɺϚχϡΞϧςετ
    • ςετ͕ͳ͍ϨΨγʔϓϩμΫτͰ΋ಋೖՄೳ

    View full-size slide

  11. Phan
    • https://github.com/phan/phan
    • PHPDocͰܕਪ࿦΋͢Δ੩తղੳπʔϧ
    • Etsyࣾͷ։ൃ͕ݩ
    • ※PHPStan΍PHPMDͱͷൺֱ͸ࠓճ͸͠·ͤΜ

    View full-size slide

  12. ਘৗ͡Όͳ͍issue਺

    View full-size slide

  13. ਘৗ͡Όͳ͍issue਺
    ղੳ݁ՌΛॖখදࣔͨ͠΋ͷʢҰ෦ʣ

    View full-size slide

  14. ਘৗ͡Όͳ͍issue਺
    • ͦΕ·Ͱ੩తղੳͰอޢ͞Ε͍ͯͳ͍ίʔυ͸

    جຊతʹແ਺ͷissue͕ใࠂ͞ΕΔ
    • ·ͱ΋ʹऔΓ߹͏ͱ৺͕ંΕΔ
    • Τϥʔͷใࠂ͕গͳͯ͘΋·ͣ͸CIʹೖΕ͍ͨ

    View full-size slide

  15. ਘৗ͡Όͳ͍issue਺
    • ͦΕ·Ͱ੩తղੳͰอޢ͞Ε͍ͯͳ͍ίʔυ͸

    جຊతʹແ਺ͷissue͕ใࠂ͞ΕΔ
    • ·ͱ΋ʹऔΓ߹͏ͱ৺͕ંΕΔ
    • Τϥʔͷใࠂ͕গͳͯ͘΋·ͣ͸CIʹೖΕ͍ͨ

    ग़ͯΔJTTVFͷछྨ͝ͱʹରॲͯ͠

    ͻͱ·ͣ$*Λಋೖ͢Δ

    View full-size slide

  16. Phanͷ৔߹ʢconfig.phpʣ

    View full-size slide

  17. Phanͷ৔߹ʢconfig.phpʣ
    TVQQSFTT@JTTVF@UZQFT
    ཈੍͍ͨ͠*TTVF5ZQFΛ

    ྻڍ͢Δ

    View full-size slide

  18. • Phanͷ࣮ߦ݁Ռ͔Βੜ੒Ͱ͖Δ
    suppress_issue_types
    $cat analysis.txt | cut -f 2 -d ' '|\
    sort -u | sed "s/^/'/g" | sed "s/$/',/g"

    View full-size slide

  19. • Phanͷ࣮ߦ݁Ռ͔Βੜ੒Ͱ͖Δ
    suppress_issue_types
    $cat analysis.txt | cut -f 2 -d ' '|\
    sort -u | sed "s/^/'/g" | sed "s/$/',/g"
    $*͕ಋೖͰ͖ͨʂ

    View full-size slide

  20. • Phanͷ࣮ߦ݁Ռ͔Βੜ੒Ͱ͖Δ
    suppress_issue_types
    $cat analysis.txt | cut -f 2 -d ' '|\
    sort -u | sed "s/^/'/g" | sed "s/$/',/g"
    ͕ɺ΄΅Τϥʔใࠂ͸͞Εͳ͍ɻɻɻ

    View full-size slide

  21. • Phanͷ࣮ߦ݁Ռ͔Βੜ੒Ͱ͖Δ
    suppress_issue_types
    $cat analysis.txt | cut -f 2 -d ' '|\
    sort -u | sed "s/^/'/g" | sed "s/$/',/g"
    ͔͜͜Β੩తղੳΛҭͯΑ͏ʂ

    View full-size slide

  22. ੩తղੳͷҭͯํ

    View full-size slide

  23. ੩తղੳΛҭͯΔϧʔϓ
    ௚͢Issue TypeΛ

    ܾΊΔ
    ͥΜͿ௚͢
    CIʹ൓ө͢Δ
    ʢsuppress_issue_types͔Βͳ͘͢ʣ

    View full-size slide

  24. ੩తղੳΛҭͯΔϧʔϓ
    ௚͢Issue TypeΛ

    ܾΊΔ
    ͥΜͿ௚͢
    CIʹ൓ө͢Δ
    ʢsuppress_issue_types͔Βͳ͘͢ʣ
    ΍Έ͘΋ʹରॲͩ͢͠ͱͭΒ͍ͷͰɺ

    ҭ͕͍ͯͷ͋Δ*TTVF5ZQFΛޮ཰Α͘୳͍ͯ͜͠͏

    View full-size slide

  25. ҭ͕͍ͯͷ͋ΔIssue TypeΛ୳࣠͢
    ΤϥʔͷՄೳੑ͕ߴ͍͔
    ϦεΫ
    ίετ ؆୯ʹ௚ͤΔ͔

    View full-size slide

  26. ҭ͕͍ͯͷ͋ΔIssue TypeΛ୳࣠͢
    ΤϥʔͷՄೳੑ͕ߴ͍͔
    ϦεΫ
    ίετ ؆୯ʹ௚ͤΔ͔

    View full-size slide

  27. ϦεΫ: ΤϥʔͷՄೳੑ͕ߴ͍͔
    • ࠓ͸ۮવಈ͍͍ͯΔ
    • ݺͼग़͠ํʹΑͬͯ͸࣮ߦ࣌ΤϥʔʹͳΔ
    • ࣅͨΑ͏ͳίʔυ͕૿͑Δલʹஅͪ੾Γ͍ͨ
    • ΤϥʔϨϕϧΛઃఆͯ͠੩తղੳ͢Δ

    View full-size slide

  28. Phanͷ৔߹ʢconfig.phpʣ

    View full-size slide

  29. Phanͷ৔߹ʢconfig.phpʣ
    NJOJNVN@TFWFSJUZ
    ൃੜͤ͞ΔΤϥʔϨϕϧͷઃఆ

    View full-size slide

  30. Phanͷ৔߹ʢconfig.phpʣ
    ҰԠஈ֊͕ͩɺ࣮࣭
    Issue::SEVERITY_LOW;
    Issue::SEVERITY_NORMAL;
    Issue::SEVERITY_CRITICAL;
    ͷ̏ஈ֊
    NJOJNVN@TFWFSJUZ
    ൃੜͤ͞ΔΤϥʔϨϕϧͷઃఆ

    View full-size slide

  31. PhpStormͷ৔߹: Inspect Code
    • PhpStorm https://www.jetbrains.com/ja-jp/phpstorm/
    • ΈΜͳେ޷͖PHPͷIDE
    • ϓϩδΣΫτ಺ͷҟৗίʔυΛ

    ϑΝΠϧΛ։͚ͩ͘Ͱݕग़ͯ͘͠ΕΔʂ
    • ϓϩδΣΫτશମͷInspectionϨϙʔτΛ

    ग़ྗͰ͖Δ

    View full-size slide

  32. Inspect Code
    • Find Action (Ctrl + Shift + A, ⌘+⇧+A )ʹ

    “Inspect”ͱೖΕͯΈΔ

    View full-size slide

  33. • ॏཁ౓ผʹInspection ݁Ռ͕ग़ྗ͞ΕΔ
    Inspect Code

    View full-size slide

  34. খωλ
    • Inspection͕࣮ߦͰ͖ͳ͍ͱ͖͸ϝϞϦΛ଍͢
    • Help -> Change Memory Setting͔ΒมߋͰ͖Δ

    View full-size slide

  35. ҭ͕͍ͯͷ͋ΔIssue TypeΛ୳࣠͢
    ΤϥʔͷՄೳੑ͕ߴ͍͔
    ϦεΫ
    ίετ ؆୯ʹ௚ͤΔ͔

    View full-size slide

  36. ίετ: ؆୯ʹ௚ͤΔ͔
    • ָʹमਖ਼Ͱ͖Δ͜ͱ͔Β࢝ΊΑ͏
    • ਺ͷগͳ͍Issue TypeΛ୳͢
    • ػցతʹमਖ਼Ͱ͖ΔIssueΛ୳͢
    • ෆཁίʔυ࡟আ

    View full-size slide

  37. • Phanͷ࣮ߦ݁Ռ͔Βݟસ͏
    • PhanͷWikiʹ͋ΔίϚϯυ
    ਺ͷগͳ͍Issue Type
    https://github.com/phan/phan/wiki/Tutorial-for-Analyzing-a-Large-Sloppy-Code-Base
    $cat analysis.txt | cut -d ' ' -f2 |\
    sort | uniq -c | sort -n -r

    View full-size slide

  38. • Phanͷ࣮ߦ݁Ռ͔Βݟસ͏
    • PhanͷWikiʹ΋ॻ͍ͯ͋ΔίϚϯυ
    ਺ͷগͳ͍Issue Type
    https://github.com/phan/phan/wiki/Tutorial-for-Analyzing-a-Large-Sloppy-Code-Base
    $cat analysis.txt | cut -d ' ' -f2 |\
    sort | uniq -c | sort -n -r

    ؤுΕ͹௚ͤͦ͏

    View full-size slide

  39. ਺ͷଟ͍Issue Type
    • 1ߦमਖ਼͢Δ͚ͩͰɺ਺ඦͷIssueΛ

    ফ͢͜ͱ͕Ͱ͖Δ৔߹΋͋Δ
    • PHPDoc͕͓͔͍͠ͱ͔

    View full-size slide

  40. ਺ͷଟ͍Issue Type
    • 1ߦमਖ਼͢Δ͚ͩͰɺ਺ඦͷIssueΛ

    ফ͢͜ͱ͕Ͱ͖Δ৔߹΋͋Δ
    • PHPDoc͕͓͔͍͠ͱ͔

    /**
    * @return Barɺ઀ଓʹࣦഊͨ͠৔߹͸false
    */
    public function foo()
    {
    ɹ…
    }

    View full-size slide

  41. ਺ͷଟ͍Issue Type
    • 1ߦमਖ਼͢Δ͚ͩͰɺ਺ඦͷIssueΛ

    ফ͢͜ͱ͕Ͱ͖Δ৔߹΋͋Δ
    • PHPDoc͕͓͔͍͠ͱ͔

    /**
    * @return Barɺ઀ଓʹࣦഊͨ͠৔߹͸false
    */
    public function foo()
    {
    ɹ…
    }
    ϊΠζʹͳΔͷͰαΫοͱ௚ͦ͏

    View full-size slide

  42. ػցతʹमਖ਼Ͱ͖ΔIssue
    • PhpStormͷ͍͔ͭ͘ͷInspectionʹ͸

    Auto fixػೳ͕෇͍͍ͯΔ

    View full-size slide

  43. PhpStormͷAuto fix
    • ͲͷInspectionʹAuto fix͕͋Δ͔͸

    ࣮ࡍʹ΍ͬͯΈΔ͔͠ͳͦ͞͏
    • ΋ͷʹΑͬͯ͸Auto fixͰ΋յΕΔ͜ͱ͕͋ΔͷͰ৻ॏ
    • Auto fix͕෇͍͍ͯΔ͜ͱ͕ଟ͍ͷ͸ɺ
    • Unnecessary ~ܥͷInspection
    • Code StyleܥͷInspection
    • Missing ~ ܥͷPHPDocͷInspection

    View full-size slide

  44. Run Inspection by Name

    • Inspectionͷ໊લΛࢦఆ࣮ͯ͠ߦͰ͖Δ

    View full-size slide

  45. ෆཁίʔυ࡟আ
    • ϑΝΠϧɾϝιουɾΫϥε୯ҐͰ͋Ε͹ɺ

    ୯ʹ࡟আ͢Δ͚ͩͰΑ͍
    • ෆཁίʔυ͸ͦ΋ͦ΋࣮ߦ͞Ε͍ͯͳ͍ͷͰɺ

    ෆ۩߹ίʔυ͕࢒͍ͬͯΔ͜ͱ΋ଟ͍
    • ੩తղੳͷअຐʹ΋ͳΔͷͰ

    ·ͣ࡟আ͢Δͷ͕Φεεϝ

    View full-size slide

  46. ෆཁίʔυͷݟ͚ͭํ
    • PhpStormͷUnused ~ ܥͷInspection
    • Phanͷ--dead-code-detection

    View full-size slide

  47. ੩తղੳ͕νʔϜΛҭͯΔ

    View full-size slide

  48. ੩తղੳͰϨϏϡʔ͕͔ΘΔ
    • ϨϏϡʔͰ΋খ͍͞ࢦఠ͕ଟ͘ͳΔͱɺ

    ຊ࣭తͳେ͖ͳ໰୊΁ͷࢦఠ͕೉͘͠ͳΔ
    • ϓϧϦΫͷখੴ͕গͳ͘ͳΕ͹ɺ

    ͦΕ͚ͩຊ࣭తͳ໰୊ͷϨϏϡʔ͕Մೳʹ
    • ؍఺ΛߜΓࠐΜͰͷϨϏϡʔ

    View full-size slide

  49. ؍఺ΛߜΓࠐΜͰͷϨϏϡʔ
    ৿࡚म࢘ஶؒҧ͍ͩΒ͚ͷઃܭϨϏϡʔ<վగ൛>1ΑΓҾ༻

    View full-size slide

  50. ੩తղੳ͕νʔϜΛҭͯΔ
    • ϨϏϡʔͰখ͍͞ࢦఠΛ͠ͳͯ͘Α͘ͳΔͷͰ

    ຊ࣭తͳ఺ΛϨϏϡʔͰ͖Δ
    • νʔϜϝϯόʔ͕ࣗવͱྑ͍

    ίʔσΟϯάΛֶ΂Δ
    • طଘίʔυͱҰ؏ੑΛ࣋ͬͯ

    ৽نίʔυΛॻ͖΍͘͢ͳΔ

    View full-size slide

  51. ·ͱΊ
    • খ͞ͳ໰୊ΛݮΒ͢ɾ૿΍͞ͳ͍ͨΊʹ

    ੩తղੳ͸໾ʹͨͭ
    • ϦεΫͷߴ͍ͱ͜Ζ͔Βɺ੩తղੳΛҭͯΑ͏
    • ίετͷ௿͍ͱ͜Ζ͔Βɺ੩తղੳΛҭͯΑ͏
    • ੩తղੳΛҭͯΔͱνʔϜ͕ҭͭ

    View full-size slide

  52. Speaker asks…
    • @suppress, @noinspectionͳͲΛ࢖͏ͷ͸

    ͋Γʁͳ͠ʁϧʔϧʁ
    • IDEΛνʔϜͰ౷Ұ͍ͤͨ͞೿ͳΜͰ͕͢ɺ

    Έͳ͞ΜͲ͏Ͱ͔͢ʁ
    • ੩తղੳͷίΞͳػೳʢPhanͷΞϊςʔγϣϯͱ͔ʣ

    ࢖͏ͷ͋Γʁͳ͠ʁ

    View full-size slide