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

PHPバージョンアップけもの道

 PHPバージョンアップけもの道

at "PHPバージョンアップ kickoff" 2021/07/15
https://breaktimes.connpass.com/event/218221/

uzulla

July 15, 2021
Tweet

More Decks by uzulla

Other Decks in Programming

Transcript

  1. PHP version, Age Night … • ͔ͩΒɺ͍·͙͋͢ͳͨ΋Version UP!! • ຊτʔΫ͸ɺ্͛ͯͳ͍ਓ͕ର৅Ͱ͢

    • ຊτʔΫ͸ɺPR TIMES಺ͷΤϯδχΞ͞Μ޲͚Ͱ͢ • (࣮ঢ়ͱϑΟοτͯ͠ͳ͍͔΋ɺྃ͝ঝ͍ͩ͘͞) • ࠓ೔ඈ͹ͨ͠ൣғ͸ฉ͍ͯ͘ΕͨΒݸผʹ͍͘ΒͰ΋΍Γ·͢ • (ྫతͳ΋ͷ͸Ұൠ࿦ͰɺPR TIMES͞Μͷݱঢ়Ͱ͸͋Γ·ͤΜ)
  2. આ໌ෆཁ͕ͩɺ࣌ؒ͸ඞཁ • ࣌ؒʹਓ݄㲈ػձଛࣦ ▶ ʮ࠷௿ݶʹ͠Α͏ʯ ▶ Ϧεέ ▶ அ೦ •

    ݟੵࠔ೉࡞ۀͳͷʹɺͳ͔ͥ୹࣌ؒͰݟੵ΋Γ͕ͪ ڍ͛۟ɺ୹ظͷεέδϡʔϧ͕૊·ΕΔ͜ͱ͕ଟ͍… • ʮ৽نͰ࡞ͬͨ΄͏͕޻਺͕ಡΊΔʯ ʮͦΕ͸ʰ৽نͰ࡞ͬͨܦݧʱ͕ଟ͍͚ͩͰ͸ʁʯ (·͋ɺຊ౰ʹ৽نͰ࡞ͬͨ΄͏͕ྑ͍৔߹΋͋Δ)
  3. ໰୊Λղ͘ʹ͸ద੾ͳ՝୊ઃఆ͕ඞཁ • ʮ͜ΕΛόʔδϣϯΞοϓͯ͠ʯ ▶ ݟੵ΋Γࠔ೉ʹݟ͑Δ • ʮ͜ͷςετΛ௨ΔΑ͏ʹͯ͠ʯ ▶ Մೳʹݟ͑Δ •

    ͳͷͰɺςετʹམͱ͠ࠐΉ • ৽؀ڥͱچ؀ڥͷζϨ(ෆ۩߹)Λ໰୊ʹ͢Δ • ͦͷζϨͷϦετΛʮ࡞Δʯͷ͸Մೳͱ͍͑ΔͷͰ͸ͳ͍͔ʁ (໰୊ͷ෼ׂ)
  4. ʮ࣮ྫʂվળͷ༷ࢠʂʂʯ foreach($՝୊Ϧετ as $՝୊){ ʮ͜ͷػೳɾϑϩʔ͕ಈ͔ͳ͍ʯ ▶ ʢݪҼௐࠪͷ։࢝ʣ ʮ͜ͷURL͕ಈ͔ͳ͍ʯ ▶ ʮ͜ͷίʔυͷ͜ͷߦ͕ಈ͔ͳ͍ʯ

    ▶ ʮؔ਺͕ͳ͘ͳ͍ͬͯΔʯ ▶ ʢݪҼௐࠪͷୡ੒ʣ ʮΑ͠ʂ͠Β΂ͯॻ͖׵͑Α͏ʂʯ ▶ʢࢪࡦͷ։࢝ʣ ʮςετͯ͠͏͍ͨͧ͝ʂʯ⾣ ʢୡ੒ʣ } # ͜Εͷ܁Γฦ͠ɺ͜Ε͕ೖΕࢠʹͳΔ
  5. E2EςετʢEnd to End ςετʣ • ೖྗ(ϦΫΤετ)ʹର͠ɺظ଴௨Γͷग़ྗ(Ϩεϙϯε)͔ʁ • ৽؀ڥʹ࣮ߦͯ͠੒ޭɾࣦഊ͕Θ͔Δ (ͦͯ͠ɺݱߦʹ΋͔͚ΒΕΔ) •

    ʮόʔδϣϯΞοϓޙɺE2Eςετ͕௨Ε͹੒ޭʯͱݴ͑Δ • (…ཧ࿦্ͷཧ૝Ͱݴ͑͹ɻྫ֎΋͋Γ·͢) • ϒϥ΢βͰखಈͰ΋Մೳɺ೔ʑօ͞Μͯ͠Δͱࢥ͍·͢ɻ
  6. ͡Ό͋ૉ๿ͳπʔϧͱ͸ʁͷྫ • symfony/http-client + symfony/dom-crawler + phpunitҐͰ͍ ͍ • (HTMLͷ׬શҰக͸ແ஡ͳͷͰ)

    ϨεϙϯείʔυɺϔομʔɺDOM౳ͷ֬ೝ • ʮ࡞ۀʯͳͷͰɺʮ͔ͬ͜Α͍ʯඞཁ͸ͳ͍ ۃ࿦͍͑͹ɺcurlͱγΣϧεΫϦϓτ͍͍ͩͬͯ • (JS͕ͳ͍ͱಈ͔ͳ͍ʁ
  7. <?php /** @noinspection NonAsciiCharacters */ /** @noinspection PhpUnhandledExceptionInspection */ declare(strict_types=1);

    namespace MyTest\Scenario; use PHPUnit\Framework\TestCase; use Symfony\Component\DomCrawler\Crawler; use Symfony\Component\HttpClient\HttpClient; class TopPageTest extends TestCase { public function testτοϓϖʔδΛදࣔ(): void { // https://symfony.com/doc/current/http_client.html $c = HttpClient::create(); $res = $c->request('GET', 'https://cfe.jp/'); // check response header $this->assertEquals(200, $res->getStatusCode()); $header = $res->getHeaders(); $this->assertArrayHasKey('content-type', $header); $this->assertStringStartsWith('text/html', $header['content-type'][0]); $this->assertStringContainsString('html', $res->getContent()); // DomCrawler $d = new Crawler($res->getContent()); // check title $this->assertEquals("cfe.jp - uzulla", $d->filter('title')->text()); // find with css selector $sns_link_list = $d->filter('body > ul:nth-child(4)'); // find in found result $a_that_have_li = $sns_link_list->filter("li a"); // check in loop foreach($a_that_have_li as $a){ $this->assertStringStartsWith('https://', $a->attributes->getNamedItem('href')->nodeValue); } } }
  8. <?php /** @noinspection NonAsciiCharacters */ /** @noinspection PhpUnhandledExceptionInspection */ declare(strict_types=1);

    # ͔ͤͬ͘ͳͷͰ৽͍͠PHPʹ߹Θ͍ͤͯ͘ # ʮػೳʯͰ͸ͳ͘ɺʮURLʯ୯ҐͷςετͰ͋Δ namespace MyTest\Scenario; use PHPUnit\Framework\TestCase; use Symfony\Component\DomCrawler\Crawler; use Symfony\Component\HttpClient\HttpClient; class TopPageTest extends TestCase { public function testτοϓϖʔδΛදࣔ(): void { # ೔ຊޠͷςετ໊Λڐ͢ʢΤϯδχΞͷ฼ࠃޠʹ΋ΑΔʣ // => ࣍ϖʔδ΁ } }
  9. // https://symfony.com/doc/current/http_client.html $c = HttpClient::create(); # ϦΫΤετΛඈ͹͢ $res = $c->request('GET',

    'https://cfe.jp/'); # ϨεϙϯεϔομΛ֬ೝ͢Δʢॏཁʣ $this->assertEquals(200, $res->getStatusCode()); # ҎԼ͸HTMLͷྫͳͷͰɺapplication/jsonͳͲͷ৔߹͸·ͨม͑Δ $header = $res->getHeaders(); $this->assertArrayHasKey('content-type', $header); $this->assertStringStartsWith('text/html', $header['content-type'][0]);
  10. # ͱΓ͋͑ͣจࣈྻͰ৭ʑ൑ఆ͢Δ $this->assertStringContainsString('html', $res->getContent()); # ࠷ޙ·Ͱग़ͯͦ͏ʁ $this->assertStringContainsStringIgnoringCase('</html>', $res->getContent()); # Display

    ErrorsΛर͏ʢௐ੔͕ඞཁͳࣄ΋ଟ͍ͷͰ֤ࣗద౰ʹʣ $this->assertDoesNotMatchRegularExpression( '/(Notice|Error|Warning):/u', $res->getContent() );
  11. # HTMLΛύʔεͯ͠৭ʑ֬ೝ͢Δ $d = new Crawler($res->getContent()); # λΠτϧͷ֬ೝ $this->assertEquals("cfe.jp -

    uzulla", $d->filter('title')->text()); # XPathͰͳ͘ɺ׳ΕͨCSSηϨΫλ͕͔ͭ͑ΔΑ $sns_link_list = $d->filter('body > ul:nth-child(4)'); # બ୒ͨ͠DomNodeList͔Β͞ΒʹߜΓࠐΊΔΑ $a_that_have_li = $sns_link_list->filter("li a"); # ͪΐͬͱ΍΍͍͜͠Μ͚ͩͲɺࠓͲ͖ͷPHPͳΒิ׬Ͱ͖ΔΑ foreach($a_that_have_li as $a){ $this->assertStringStartsWith('https://', $a->attributes->getNamedItem('href')->nodeValue); }
  12. $ composer test > vendor/bin/phpunit PHPUnit 9.5.6 by Sebastian Bergmann

    and contributors. . 1 / 1 (100%) Time: 00:00.202, Memory: 6.00 MB OK (1 test, 16 assertions) αϯϓϧίʔυɿ https://github.com/uzulla/mch
  13. ͲΜͲΜΧελϜͯ͠΋ྑ͍ • TestCaseΫϥεΛϥοϓ͢ΔɺTraitʹ෼཭͢Δ ྫ: clientΛWrapͯ͠ɺStatus CodeνΣοΫͱ͔Λڞ௨Խ • ݱ৔ʹϚον͢Δলུه๏Λ૿΍͢ͱޮ཰্͕͕Γ·͢ɺ ʮෳࡶͰͳ͍ʯͳΒΑ͠ •

    URLϦετΛCSV΍Google Sheets͔ΒҾ͘ͳΜͯศར΋ΞϦ • Ϟμϯͳ੩తղੳ͕ޮ͘ίʔυʹ͢Ε͹ϦϑΝΫλ΋؆୯ʂ • ʮ͋ͨΒ͍͠PHPʯΛ࢖͏͜ͱͰؾ෼ΛΞή͍ͯ͘
  14. LTSVྫͰ͢ time:2021-06-23T03:59:30+00:00 method:GET uri:/ status:200 size:12306 apptime:0.226 type:text/html time:2021-06-23T03:59:30+00:00 method:GET

    uri:/hoge status:404 size:306 apptime:0.226 type:text/html time:2021-06-23T03:59:30+00:00 method:POST uri:/api.php status:200 size:306 apptime:0.226 type:application/json time:2021-06-23T03:59:30+00:00 method:GET uri:/redirect?hoge status:302 size:0 apptime:1.226 type:text/html label:Value{\t}label:Value{\t}label:Value time ΞΫηε೔࣌ method HTTP ϝιου uri ΞΫηεURLʢϗετ͸ͳ͍ʣ status εςʔλείʔυ type Content-Type
  15. ࣮ߦྫ (௕͍) $ gzip -dc access.gz.* | head -10000 |grep

    -v "/ignore/pattern"| \ alp ltsv -m "/i/.+,/static/.+,/assets/.+,/item/search/.+,/wordpress/.+,/item/tag/.+, /item/detail/.+,/cto/diary/.+,/img/.+,/news/page/.+,/rss/some_one/.+, /some_great_page/.+,/campaign_2021/.+,/html/.+,/api/hoge/.+,/ultra-nice-page/.+, /you-and-me-relation/.+,/apple-touch-icon.+,/wp-content/.+,/some-creative/.+, /company-map/.+,/mail/form/.+,/too-many/.+,/already/im-tired/.+,/but-on/going/.+, /nice_feature/(css|font|js)/.+,/too/old/pages/.+,/product/thumbnail/.+,/&unknown_something=.+, /wow/what-is-this-page/.+,/detail/get/.+,/we-must-go-php-8/plan/.+,/we-must-go-php-8/do/.+, /we-must-go-php-8/c-is-what/.+,/we-must-go-php-8/i-dont-know-i/.+, /already/dead/page/(css|font|js)/.+,/2001-future-travel/(css|font|js),/php4/(css|font|js), /php/5/.+,/php/(7|8)/.+,/php/6/.+,/controll/panel/(css|js),/admin/.+,/api/get-json.+, /api/post-json.+,/api/another-json.+,/articles/admin/.+,/articles/page/.+,/articles/tags/.+, /wp2/.*,/special-april-fool-page/.+,/_nuxt/.+,/wp-includes/.+,/x/.+,/index.php.+,/css/.+, /js/.+,/img/.+," -o 2xx,3xx,4xx,5xx,method,uri --sort=uri |grep -v -e "/some/" -e "/attack" -e "/%20../../../pattern" > output.txt
  16. • (mysql/)data ౳΋VolumeͰϚ΢ϯτͨ͠΄͏͕ૣ͍ ͋Δ͍͸ɺ಺෦Ͱ tar.gz Λղౚ͠ɺຖճ্ॻ͖͢Δɻ • σʔλϦηοτͷͨΊʹ࠶౓SQLΛྲྀ͢ΔΑΓɺ dataΛσΟϨΫτϦ͝ͱ্ॻ͖ͯ͠࠶ىಈ͕ૣ͍ •

    ͜Ε͸ͦͷଞͷσʔλ΋ಉ༷ɺॳظԽ͕ߴ଎ʹͰ͖Δํ͕ྑ͍ • (ͦͷͨΊʹ͸ɺσʔλΛ֎͔Βݟ͑ΔΑ͏ʹͨ͠΄͏͕ศརɺ ͨͩɺLinuxҎ֎͸஗͍ΜͩΑͳ…) • (͜ͷσʔλ͸gitʹ͍Εͳͯ͘΋ྑ͍ɺೖΕͯ΋͍͍͚Ͳ)
  17. (࠶ܝ)Ϗοάδϟϯϓ͸೉͍͠ • v4΍v5.1.6͔Β௚઀v8.0͢ΔͳΜͯલ୅ະฉ(࡞Γ௚͠Ͱ͸ʁ) • શ͘ಈ͔ͳ͍··Nϲ݄͋Δ͍͸N೥ܦա͢Δ౳͸ਏ͍ • ྫ: v5.1.6 ▶ v5.3.3

    ▶ v5.4 ▶ v5.6 ▶ v7.1 ▶ v7.4 ▶ v8… • ϛυϧ΢ΣΞ΋্͍͛ͯ͘(ྫ: MySQL v5.1 ▶ v5.6 ▶ v8ͱ͔) • (Deprecatedίʔυ͔ΒDeprecatedίʔυʹ͢Δͷ͸ਏ͍ͷͰɺ ੩తղੳ͚ͩ͸7.4ઃఆͰ౎౓࣮ࢪ༷͠ࢠΛݟΔͷ͸Good)
  18. ͦΕͰ΋੩తղੳπʔϧ͸͋Γ·͢ • ྫ(͜ΕҎ֎ʹ΋৭ʑ͋Δ): • PhpStorm • https://github.com/wapmorgan/PhpDeprecationDetector • https://github.com/phan/phan •

    https://github.com/phpstan/phpstan • ʮͲΕ͕ਖ਼ղͰ͔͢ʁʯ ʮݱ৔(ର৅ίʔυ)ʹΑΔɺਖ਼ղ͸ͳ͍ͷͰશ෦ࢼ͢ʯ
  19. ༨ஊɿPhpStormͰͷ੩తղੳͷ͔͚ํ • PhpStormͷCode inspection͸࢖͑ΔͬͪΌ࢖͑Δ • 1. PHPͷر๬͢ΔόʔδϣϯΛઃఆ͢Δ • 2. ϧʔϧΛΧελϜ͢Δ

    (HTML΍CSSͷΤϥʔΛΦϑʹ͢Δ) (৭ʑ͋ΔPHPͷ߲໨΋ؤுͬͯߟ͑ͯΦϯΦϑ͢Δ…) (໪࿦ɺ͜͜ʹ΋ʮਖ਼ղʯ͸ͳ͍) • 3. ͍͕͔͔࣌ؒͬͯ͢͝ɺେྔʹͰͯ͘Δ
  20. ࣮ࡍͲ͏΍ͬͯίʔυΛ௚͍͔ͯ͘͠ʢ΍ͬͱʂʣ • ςετΛճ͢ ▶ ௚͢ ▶ ςετΛճ͢ ͜Εͷ܁Γฦ͠ʢ࠶ܝ ࡌʣ •

    खΛͱΊͳ͍ɺͲΜͲΜΞυϗοΫʹॻ͖௚͢ • ؾʹͳͬͨΒɺͱΓ͋͑ͣ ʮ// TODO ͕͜͜Ϡό͍ʯ ͱ͔ॻ͍ͯɺ͙͢ʹ๨Ε·͠ΐ͏ɻ • σάϨΛڪΕͯ͸ͳΒͳ͍ɺͦͷͨΊʹςετ͕͋Δ • ͨͩ͠ʮςετ͕յΕͳ͍ͳʁʯͱ͓΋ͬͨΒςετ΋ٙ͏ࣄ
  21. ࿩Λ໭ͯ͠: ϩάͷΤϥʔΛ௵͍ͯ͘͠ • (࠶ܝͰ͕͢) • ϩάΛE_ALLʹͯ͠ɺΤϥʔΛશ෦௚͍ͯ͘͠ Fatal, Error, Warn, Notice,

    Deprecated, Strict. • PHPͷΤϥʔ͸શͯҧ๏ ɺVerUPʹ΋΄΅༗֐ɺશ෦ରԠ͢Δ • ׂͱօ͞ΜΤϥʔແࢹ͕͕ͪͩ͠ɺແࢹͯ͠͸ͳΒͳ͍ɺ DeprecatedͳΜͯݟ͚ͭͨΒϥοΩʔνϟϯεͱݴ͑Δ • (ͳ͓ɺΞϓϦͷʰσόοάϩάʱ͸अຐͳͷͰΦϑ͠·͠ΐ
  22. ಈ͍ͨޙʹɺ͋Βͨͳઓ͍͕࢝·Δʂ • "Include Oriented PHP"͔Β୤٫͠ɺOOPɺAutoloaderԽ • ίʔσΟϯάن໿ͷద༻ • ςϯϓϨʔτΤϯδϯ΍ϧʔλʔͷ࡮৽ •

    ʮຊ෺ͷςετʯ΍ʮຊ෺ͷ੩తղੳʯɺCIɺCDԽ • ڧఢʹΈ͑·͢ʁ͍΍͍΍ʮண࣮ʹ΍Δʯͱେࠩͳ͍Ͱ͢Α • ΍͍͖ͬͯ·͠ΐ͏
  23. ʮ;Ήɺ100000ϑΝΠϧ͔ɺ༗ݶ࣌ؒ಺ʹऴΘΔͳʯ • Keep Calm and Carry On. "ฏ੩Λอͪɺීஈͷੜ׆Λଓ͚Α" • ࣮ࡍɺ΍Ε͹ऴΘΔʢܦݧஊʣ

    ౤͛ग़ͤ͹ऴΘΒͳ͍ʢܦݧஊʣ • ೔ʑͷϧʔνϯϫʔΫ΁ͷམͱ͠ࠐΈ(ࢥ૝,ಓ۩,ମ੍)͕ॏཁ • ͦΕ͕ࣗ৴ͱɺ࡞ۀϋʔυϧΛԼ͛Δ͜ͱʹܨ͕Γ·͢ • ·͋ɺͦΕ͸ผͷ࿩ͳͷͰɺ·͍͔ͨͭ
  24. ϑΝΠϧΛফͯ͠Α͍͔҆৺͢ΔςΫ • ʮ͜ͷϑΝΠϧ͸ফͯ͠΋ྑ͍ഺʯͱ͓΋ͬͨΒɺͦͷίʔυ ʹΞΫηε͕͋ͬͨΒϩάΛग़͢Α͏ʹ͢Δͱྑ͍ɻϚʔΫ୅ ΘΓʹ΋ͳΔɻ • ྫ:error_log("Unexcepted use :".__FILE__, 3,

    "/tmp/log"); ʢϩάج൫ʹͳ͕͢ͷ͸OK͕ͩɺΤϥʔϩάͱ؆୯ʹ෼཭Ͱ͖ Δ͜ͱɻSlackʹྲྀ͢ͷ΋Goodʣ • ೲಘͨ͠ΒɺఆܕจͰ୳ͯ͠ফ͢ɻ