Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

独自フレームワークPHPアプリケーションの改善戦略 / Original Framework ...

tzmfreedom
October 02, 2021

独自フレームワークPHPアプリケーションの改善戦略 / Original Framework PHP Kaizen

PHP Conference Japan 2021の発表資料です。
https://fortee.jp/phpcon-2021/proposal/7ae96df3-84a7-4d5d-a496-bd9b72841219

tzmfreedom

October 02, 2021
Tweet

More Decks by tzmfreedom

Other Decks in Technology

Transcript

  1. ಠࣗϑϨʔϜϫʔΫͷߏ଄ w ਌ϑΝΠϧࢠϑΝΠϧଙϑΝΠϧͷJODMVEFϦϨʔ w ݺͼग़͠ݩϑΝΠϧͰఆٛͨ͠ม਺ɾؔ਺Λݺͼग़͠ઌͰར༻ JODMVEFͰݺͼग़͠ $application = [ ...

    ] ; // ... // ࢠεΫϦϓτͷݺͼग़͠ $scriptPath = getScript($requestPath) ; include "/path/to/root/$scriptPath"; if ($application['xxx'] === 'yyy') { // ... } // ଙεΫϦϓτͷݺͼग़͠ $scriptPath = getScript($_GET) ; include "/path/to/root/$scriptPath";
  2. "1*ςετίʔυྫ fi nal class PhotoTest extends TestBase { public function

    test_Ұཡදࣔ( ) { $client = new GuzzleHttp\Client() ; $res = $client->get(self::HOST. self::PATH, []) ; $actual = json_decode($response->getBody()->getContents(), true) ; $this->assertSame( [ // ... ], $actual) ; } }
  3. "1*ςετͷϞοΫ w ࣮αʔόͷ"1*ςετͰ΋ಛఆͷϞδϡʔϧΛϞοΫ͍ͨ͠ w ୯ମςετͰ͋Ε͹ϞοΫ͢ΔॲཧΛݺͼग़ͤ͹ྑ͍ w 1)17$3 $BSCPOTFU5FTU/PX VPQ[ ʜ

    w ͕ɺࠓճ͸ςετͱςετର৅͕ผϓϩηεʹͳ͍ͬͯΔͷͰ 1)16OJUͷϓϩηεͰϞοΫΛݺͼͩͯ͠΋ϞοΫ͕Ͱ͖ͳ͍
  4. $cassetteName = $_SERVER['HTTP_X_VCR_CASSETTE_NAME'] ; $mockTime = $_SERVER['HTTP_X_CURRENT_TIMESTAMP'] ; if ($mockTime

    !== '') { $time = Carbon::parse($mockTime) ; Carbon\Carbon::setTestNow($time) ; } if ($cassetteName !== '') { ini_set('opcache.enable', 0) ; \VCR\VCR::con fi gure( ) ->setCassettePath(__DIR__ . '/tests/cassette') ; \VCR\VCR::turnOn() ; \VCR\VCR::insertCassette($cassetteName) ; } ϦΫΤετϔομͰϞοΫઃఆΛऔಘ $BSCPOͰ࣌ؒΛϞοΫ 7$3Ͱ)551ϦΫΤετΛϞοΫ
  5. class CoverageDumper { function __destruct( ) { $coverageName = ‘/path/to/coverage-‘

    . microtime(true); xdebug_stop_code_coverage(false) ; $codecoverageData = json_encode(xdebug_get_code_coverage()) ; fi le_put_contents($coverageName . '.json', $codecoverageData) ; } } xdebug_start_code_coverage ( XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_COD E ) ; $coverageDumper = new CoverageDumper(); ΧόϨοδܭଌ։࢝ ΧόϨοδܭଌ݁ՌͷϑΝΠϧॻ͖ग़͠
  6. $ fi nalCoverage = new SebastianBergmann\CodeCoverage\CodeCoverage() ; $ fi lter

    = $ fi nalCoverage-> fi lter() ; $ fi lter->addDirectoryToWhitelist("/path/to/dir") ; $coverages = glob('./coverage/coverage-*.json') ; foreach ($coverages as $coverageFile ) { $codecoverageData = json_decode( fi le_get_contents($coverageFile)) ; $testName = str_ireplace(basename($coverageFile,".json"),"coverage-", "") ; $ fi nalCoverage->append($codecoverageData, $testName) ; } $ fi nalCoverage->merge(include './coverage/phpunit.php') ; $report = new \SebastianBergmann\CodeCoverage\Report\Html\Facade() ; $report->process($ fi nalCoverage, 'reports'); "1*ςετͷશΧόϨοδϑΝΠϧΛूܭɾϚʔδ ୯ମςετͷΧόϨοδϑΝΠϧΛϚʔδ ΧόϨοδΛ)5.-Ͱॻ͖ग़͠
  7. parameters : level: 0 bootstrapFiles : - %rootDir%/../../../phpstan.bootstrap.php ignoreErrors :

    - ’#xxxx#’ … $ php ./vendor/bin/phpstan analyse --memory-limit=-1 /path/to/dir w·ͣ͸CBTFMJOF΍ɺͬ͘͟ΓJHOPSF&SSPSTͯ͠ελʔτ wϨϕϧΛ্͍͔͛ͯ͘ϧʔϧΛݸผ௥Ճ͍͔ͯ͘͠
  8. 6OEFGJOFEWBSJBCMF໰୊ͷճආํ๏ w ʢ࠶ܝʣ਌ϑΝΠϧͰఆٛͨ͠ม਺ɾؔ਺ΛࢠϑΝΠϧͰ࢖͍ͬͯΔ w ࢠϑΝΠϧΛͦͷ··ղੳͨ͠Β6OEF fi OFEWBSJBCMF 'VODUJPOOPU FYJTUTࡇΓ w

    ϑΝΠϧͷઌ಄ʹ1)1%PDͷม਺એݴʢ!WBSʣΛೖΕΔ͜ͱͰճආ Մೳ͕ͩɺࢠϑΝΠϧશͯʹೖΕͳ͍ͱ͍͚ͳ͍ͨΊอकੑ͕ѱ͍ w Ұ୴JHOPSF&SSPSTͰճආʢ͋Δఔ౓ͷGBMTFOFHBUJWF͸ڐ༰ʣ
  9. $ch = curl_init() ; curl_setopt($ch, CURLOPT_URL, 'http://api.example.com/path') ; curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,

    5) ; curl_setopt($ch, CURLOPT_TIMEOUT, 5) ; curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ; curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1) ; curl_setopt($ch, CURLOPT_POST, 1) ; curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ ... ])) ; $res = curl_exec($ch) ; if (!curl_errno($ch)) { $info = curl_getinfo($ch) ; if ($info['http_code'] === 200) { // ... } } curl_close($ch); DVSM@TFUPQUͷཛྷ ΤϥʔνΣοΫ͕େม DMPTF
  10. $response = self::getClient()->post('/path', [ 'form_params' => [ ... ] ,

    'headers' => [ ... ] , ]) ; $body = $response->getBody() ; return json_decode($body, true); $ch = curl_init() ; curl_setopt($ch, CURLOPT_URL, 'http:// api.example.com/path') ; curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5) ; curl_setopt($ch, CURLOPT_TIMEOUT, 5) ; curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ; curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1) ; curl_setopt($ch, CURLOPT_POST, 1) ; curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ ... ])) ; $res = curl_exec($ch) ; if (!curl_errno($ch)) { $info = curl_getinfo($ch) ; if ($info['http_code'] === 200) { // ... } } curl_close($ch);
  11. w ࣮ࡍʹ͸͜ΕΛϥοϓͨ͠TUBUJDϝιουΛఆٛͯ͠ϩΪϯά public static function getLogger( ) { self::$logger =

    (new \Monolog\Logger($env)) ) ->pushHandler(new \Monolog\ErrorLogHandler() ) ->pushProcessor(function($record) { $record['extra']['uid'] = self::getRequestID() ; return $record ; }) ; return self::$logger ; }