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

PHP_CodeSnifferを使ってPHPのコードをオートレビューしてみた.pdf

 PHP_CodeSnifferを使ってPHPのコードをオートレビューしてみた.pdf

第114回 PHP勉強会@東京でLT発表した際の資料です。

Atsushi Nakatsugawa

May 31, 2017
Tweet

More Decks by Atsushi Nakatsugawa

Other Decks in Technology

Transcript

  1. [Confidential] © 2016 Actcat, Inc. ࣗݾ঺հ 2 !B@OBLBUTVHBXB GCNFNPPOHJGU த௡઒ಞ࢘

    גࣜձࣾ.00/(*'5୅දऔక໾ 4JEF$*/$.#9PKPIJpWFΤϰΝϯδΣϦετ ៉ྷͳίʔυͱจԽΛ࡞ΔίʔυϨϏϡʔ༻$* !TJEFDJ@KBQBO GCNFTJEFDJ
  2. [Confidential] © 2016 Actcat, Inc. PHP_CodeSnifferͱ͸ʁ ▪ن໿ʁ
 Generic
 
 PEAR


    
 PSR1
 
 PSR2
 
 Squiz
 
 Zend 3 ▪ PHPͷίʔυΛن໿ʹ߹͍ͬͯΔ͔νΣοΫͯ͘͠ΕΔπʔϧ http://www.infiniteloop.co.jp/docs/psr/psr-2-coding-style-guide.html https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md ͳ͘ͳͬͨʁʁ https://framework.zend.com/manual/1.12/ja/coding-standard.overview.html ։ൃݩ
  3. [Confidential] © 2016 Actcat, Inc. Πϯετʔϧ 4 ▪ComposerΛ࢖͏ͷ͕Ұ൪खܰ $ composer

    global require "squizlabs/php_codesniffer=*" wgetͰ΋Մ $ curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar $ php phpcs.phar -h
 $ curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar $ php phpcbf.phar -h
  4. [Confidential] © 2016 Actcat, Inc. ࢖͍ํ 5 ▪phpcs + σΟϨΫτϦ

    $ phpcs . FILE: /path/to/your/code.php -------------------------------------------------------------------------------------------------- FOUND 61 ERRORS AFFECTING 38 LINES -------------------------------------------------------------------------------------------------- 2 | ERROR | [ ] Missing file doc comment 7 | ERROR | [ ] Missing @category tag in class comment ɿ 34 | ERROR | [x] Tag value indented incorrectly; expected 2 spaces but found 1 35 | ERROR | [ ] Tag cannot be grouped with parameter tags in a doc comment
  5. [Confidential] © 2016 Actcat, Inc. ίʔσΟϯάن໿ͷ֬ೝ 6 ▪-iͰ֬ೝͰ͖·͢ $ phpcs

    -i The installed coding standards are MySource, PEAR, PSR1, PSR2, Squiz and Zend
  6. [Confidential] © 2016 Actcat, Inc. ίʔυ΋֬ೝ͢Δ৔߹ 8 ▪--report=codeΛ͚ͭΔ $ phpcs

    --report=code FILE: /path/to/your/code.php --------------------------------------------------------------------------------------------------- FOUND 61 ERRORS AFFECTING 38 LINES --------------------------------------------------------------------------------------------------- LINE 2: ERROR [ ] Missing file doc comment --------------------------------------------------------------------------------------------------- 1: <?php >> 2: 3: namespace·Ncmb; 4:
  7. [Confidential] © 2016 Actcat, Inc. ࢦఠʹ߹Θͤͯमਖ਼͍ͯ͘͠ 9 ▪phpcbfͰ·ͱΊͯमਖ਼΋Ͱ͖Δ $ phpcbf

    . PHPCBF RESULT SUMMARY --------------------------------------------------------------------------------------------------- FILE FIXED REMAINING --------------------------------------------------------------------------------------------------- /path/to/your/code.php 23 38
  8. [Confidential] © 2016 Actcat, Inc. ͱ͋ΔϓϩδΣΫτͰࢼͨ݁͠Ռ ▪ଞͷࢦఠ͸ೲಘͰ͖Δ΋ͷ΋Ұ෦͋Δʢۭ നͱ͔ʣ ▪ن໿ʹΑͬͯ࠷ޙͷ ?>

    ʹ͍ͭͯղऍ͕ҟͳ Δ ▪phpcbfͰमਖ਼Ͱ͖ͪΌ͏͚Ͳ…Ͷ͐ʁ 11 ▪ίϝϯτͷറΓ͕ਏ͍͕࣌͋Δ…