assertぐらいでエバルんじゃねえ!

 assertぐらいでエバルんじゃねえ!

〜Phanがもたらす堅牢なPHP〜

003f59d1b2e3b864c744f8eb21f68e82?s=128

Atsushi Takayama

December 15, 2016
Tweet

Transcript

  1. assert͙Β͍Ͱ ΤόϧΜ͡ΌͶ͑ʂ ʙPhan͕΋ͨΒ͢ݎ࿚ͳPHPʙ

  2. ࣗݾ঺հ ߴࢁԹ @edvakf ϐΫγϒגࣜձࣾ ࣥߦ໾һ CTO ISUCON6ຊબग़୊͠·ͨ͠ JavaScript, PHP, Ruby,

    Go, Scala ͋ͨΓ͕कඋൣғ
  3. assertศརͰ͢Ͷ

  4. function div($a, $b) {
 assert($b !== 0);
 return $a /

    $b;
 }
  5. assertͷ᠘

  6. None
  7. ʊਓਓਓਓਓਓʊ ʼɹΤϰΝϧɹʻ ʉ:?:?:?:?:ʉ

  8. ͭ·Γ

  9. ͜͏ॻ͚Δ function div($a, $b) {
 assert(“{$b} !== 0”);
 return $a

    / $b;
 }
  10. ͜͏͢Δͱassertͷத਎͕ධՁ͞Εͳ͍ assert_options(ASSERT_ACTIVE, false); function div($a, $b) {
 assert(“{$b} !== 0”);


    return $a / $b;
 }
  11. pixivͷϙϦγʔͰ͸ɺ։ൃ؀ڥͱϓϩμΫγϣϯͷࠩ ҟͷ࠷খԽͷͨΊɺͰ͖Ε͹ASSERT_ACTIVEΛfalse ʹͨ͘͠ͳ͍ assertʹ౉͞ΕΔ஋͕ʢকདྷʹ౉ͬͯʣ֎෦ೖྗʹ༝ དྷ͢Δ஋Ͱͳ͍͜ͱΛอূ͢Δͷ͸ඇৗʹࠔ೉ ·ͨɺassert͕ग़ͯ͘Δͨͼʹ౉͞ΕΔ஋͕֎෦ೖྗ ༝དྷ͔Ͳ͏͔ΛϨϏϡʔͰݟΔඞཁ͕͋Γɺίετ ͕͔͔Δ

  12. ͱ͍͏Θ͚Ͱ pixivͰ͸ಠࣗͷassertʹ૬౰͢Δؔ਺Λఆٛͯ͠ɺ PHPͷ૊ΈࠐΈͷassert͸࢖Θͳ͘ͳΓ·ͨ͠

  13. ΊͰͨ͠ΊͰͨ͠

  14. Ͱ͸ͳ͘

  15. ϨϏϡʔͷίετΛ্͛ͣʹ assertΛ࢖͑ͳ͍͔ߟ͑ͯΈͨ

  16. Ҿ਺Λboolඞਢʹ͍ͨ͠ assert($a === 1); // OK assert(empty($a)); // OK assert(!is_null($a));

    // OK assert(“$b !== 1”); // NG assert(someFunction($a)); // ?
  17. Phan ੩తղੳπʔϧ PHP7͔Βಋೖ͞Εͨ಺෦ASTΛPHPଆ͔Β৮ΕΔΑ ͏ʹ͢ΔͨΊͷphp-astͱ͍͏C֦ுʹґଘ

  18. $ phan some.php
 some.php:5 PhanTypeMismatchArgumentInternal Argument 1 (assertion) is int

    but \assert() takes bool|string PHPͷඪ४ؔ਺ͷҾ਺ͱฦΓ஋ͷܕͷҰཡΛ࣋ͬͯ ͍ͯɺܕਪ࿦ͯ͘͠ΕΔ
  19. assertͷҾ਺͕boolͰ͋Δ͜ ͱΛอূ͢Δʹ͸

  20. →PhanϓϥάΠϯͰ % tree -a
 .
 !"" .phan
 # !"" config.php


    # $"" plugins
 # $"" NonBoolAssertPlugin.php
 $"" some.php
  21. None
  22. $ phan some.php
 some.php:5 PhanPluginNonBoolAssert Non bool value passed to

    assert
 some.php:5 PhanTypeMismatchArgumentInternal Argument 1 (assertion) is int but \assert() takes bool|string
  23. ·ͱΊ PhanͰ੩తղੳͯ͠ϨϏϡʔͷίετΛԼ͛Α͏ Phan͕௨Βͳ͚Ε͹CI͕௨Βͳ͍Α͏ʹ͠Α͏ PhanϓϥάΠϯͰಠࣗίʔσΟϯάن໿΋lintԽͯ͠
 ϨϏϡʔ࣌ͷ҉໧஌ΛݮΒͦ͏

  24. ฐࣾͷ৽ଔ͕ࣥච͠·ͨ͠