ã¯ããã«
ããã«ã¡ã¯ã社å ã§ã³ã¼ãã£ã³ã°è¦ç´ããããã¨å¼ã°ãå§ãã¦ä¹ ããé«é(@fortkle)ã§ãï¼ ãã2ã¶æéã»ã©ã«æ¸¡ã£ã¦é常ã®éçºæ¥åã¨ã¯å¥ã«ç¤¾å ã®ã¢ããªã±ã¼ã·ã§ã³ã«ã³ã¼ãã£ã³ã°è¦ç´ãå°å ¥ãã試ã¿ããã¦ãããPHP7 Casual Talks ã PHP BLT ãªã©ã®PHPé¢é£ã®åå¼·ä¼ã§é½åº¦å ±æãã¦ãã¾ããã
ä»åã¯ããããã¾ã¨ããå ±æãããã¨æãã¾ããèå³ã®ããæ¹ã®åèã«ãªãã°å¹¸ãã§ãã
éçºå¹çãé»å®³ãããã®
å¼ç¤¾ãéå¶ãã¦ãã ãããªjpããããªQã¨ãã£ããµã¼ãã¹ãé 調ã«æé·ãã¦ãã£ã¦ããä¸ããã®æé·ãæ¯ããéçºãã¼ã ã®äººæ°ãå°ããã¤æ¡å¤§ãã¦ãã¾ããä»å¾ããã®æµãã¯ç¶ãã¨æãã¾ãããã¨ã³ã¸ãã¢ãããã®ãããã£ãå ´åã§ããã³ã¼ãã®è³ªãã¯è½ã¨ããã«æé·ãããããã®ã§ãã
å¼ç¤¾ã§ã¯é常ã«ä¸å¯§ã«ã³ã¼ãã¬ãã¥ã¼ãå®æ½ãã¦ãã¾ããã質ã®é«ãã³ã¼ããç¶æãç¶ããããã«ã¯ã³ã¼ãã¬ãã¥ã¼ãããå¹ççã«å®æ½ã§ãã工夫ãããå¿ è¦ãããããã«æãã¦ãã¾ããéçºã¡ã³ãã¼ãå¢ããã°å¢ããã»ã©ãã³ã¼ãã¯è¤éã«ãªãä»æ§ã®å ¨ä½åãææ¡ããã¨ã³ã¸ãã¢ã®æ°ã¯ç¸å¯¾çã«å°ãªããªãããã§ãã
ãã®ãããªç¶æ³ã®ä¸ã§ãå¼ç¤¾ã§ã¯ä»¥åãããã³ã¼ãã¹ã¿ã¤ã«ã®äºç´°ãªéãããã¬ãã¥ã¼ã§ææããããã¨ãå¤ã ããã大åã§ã¯ããããã©éçºå¹çãé»å®³ãã¦ããã®ã§ã¯ãªãããã¨ãã課é¡ãããã¾ããã
ã³ã¼ãã£ã³ã°è¦ç´ã®å½¢éª¸å
åè¿°ãã課é¡ã解決ãããããå¼ç¤¾ã§ãå½åããã³ã¼ãã£ã³ã°è¦ç´ãç¨æãä¸é¨ããã¥ã¡ã³ãçãæ®ããã¦ãã¾ãããããã¾ãå¹æçãªãã®ã§ã¯ããã¾ããã§ããããã®åå ã¨ãã¦ä¸è¨ã®ãããªç¹ãæãããã¾ãã
åå | 説æ |
---|---|
ãã§ãã¯æ©æ§ãèªåæ´å½¢ããªãã£ã | ã³ã¼ãã£ã³ã°è¦ç´ãå®ãããä»çµã¿ãä½ããªãã¨éçºè ã®åæã ãã§ã¯ç¶ç¶ããããã¾ããããªãã |
ç¬èªã³ã¼ãã£ã³ã°è¦ç´ãå°å ¥ãã | PSRãªã©ã®æ¥çæ¨æºã®è¦ç´ã§ã¯ãªãç¬èªã®ã³ã¼ãã£ã³ã°è¦ç´ãå°å ¥ãããã¨ããã¨ããã§ãã¯ã»èªåæ´å½¢ãã¼ã«ãªã©å¨è¾ºã®ã¨ã³ã·ã¹ãã ã®æ©æµã享åã§ããªãã |
ä¸æ°ã«è¦ç´ãå°å ¥ãããã¨ãã¦è¦ç´ã¨ã©ã¼ãè¨å¤§ | ããã¾ã§è¦ç´ã®ãªãã£ãæ¢åã®ã½ã¼ã¹ã³ã¼ãã«ä½ã®å·¥å¤«ããªãåã«è¦ç´ãã§ãã¯ãããã¨ç°¡åã«æ°åãæ°ä¸è¡ã®ã¨ã©ã¼ãåºã¦ãã¾ããããè¦ç´ãå®ãã¦ããç¶æ ã¾ã§æã£ã¦ããéç¨ã§ç²å¼ãã¦ããã |
phpcs / phpcbf ã«ããèªååã¨ããã¥ã¡ã³ãæ´å
ã¾ããéçºè ã®åæã«å§ããã®ã§ã¯ãªãä»çµã¿ã§ã³ã¼ãã£ã³ã°è¦ç´ãå°å ¥ããããã« phpcs / phpcbf ã使ã£ã¦èªåã§ã³ã¼ãã£ã³ã°è¦ç´ãå®ããä»çµã¿ãä½ãã¾ããã
phpcsã§ã³ã¼ããè¦ç´ã«éåãã¦ããªããã®ãã§ãã¯ãphpcbfã§éåé¨åã®èªåæ´å½¢ããããã¨ãã§ãã¾ããã¾ããphpcs / phpcbfã§ã¯å ±éã§ä½¿ãããã«ã¹ã¿ã è¦ç´ããä½ããã¨ãã§ãã¾ãã
å¼ç¤¾ã§ã¯ãã¾ã大éã«æ¤åºããã¦ãã¾ã£ãã¨ã©ã¼é ç®ãä¸æçã«å ¨ã¦ç¡å¹åããPSR2ãªã©çæ³ã¨ããè¦ç´ããã¼ã¹ã«ãã«ã¹ã¿ã è¦ç´ããä½æãã¾ããã次ã«ãæéãåºåã£ã¦å¾ã ã«ç¡å¹åããé ç®ãæ¸ããã¦ãããã¨ã§ãæçµçã«ãã¼ã¹ã§ããè¦ç´(PSR2)ã«è¿ã¥ãã¦ããã¢ããã¼ããåãã¾ããããã®æ¦ç¥ã§ããã°ç¡çããªãããªãªã¼ã¹éè¿ã®å¿ããææã§ãæè»ã«å¯¾å¿ãããã¨ãã§ãã¾ãã
ã¾ããã³ã¼ãã£ã³ã°è¦ç´ã使ãçç±ããã¼ã ã§æ¯ãè¿ããå®ç¾ããã価å¤ãæéã§å®ç¾ããããã«PSR2ãªã©ã®æ¥çæ¨æºã®è¦ç´ã«ä¹ãæããé¸æããã¾ããã
3ã¤ã®å°å ¥ã¹ããã
åæ
ç§ãã©ããã£ãæé ã§ãã¼ã ã«å°å ¥ãã¦ãã£ãã®ãããç´¹ä»ãã¾ããåæã¨ãã¦ãæ¡ç¨ããã³ã¼ãã£ã³ã°è¦ç´ã¯PSR2ã§ãå°å ¥ãããããã¸ã§ã¯ãã« composer çµç±ã§ phpcs/phpcbf*1ãã¤ã³ã¹ãã¼ã«ãã¦ãããã®ã¨ãã¾ãã
1. æ¢åã³ã¼ãããã«ã¹ã¿ã è¦ç´ãä½ã
ã¾ãã¯phpcs/phpcbfã§å
±éãã¦ä½¿ããã«ã¹ã¿ã è¦ç´ãä½ãã¾ããããè¦ç´ã¯ .xml å½¢å¼ã§ä½æãã¾ãã詳細ã¯å
¬å¼ã®ããã¥ã¡ã³ããåèã«ãã¦ãã ãããããã§ã¯ã·ã³ãã«ã«PSR2ãç¶æ¿ããã ãã®ã«ã¹ã¿ã è¦ç´ãä½ãã¾ããååã仮㫠c-psr2.xml
ã¨ãã¢ããªã±ã¼ã·ã§ã³ã®ã«ã¼ãã«ä¿åãã¾ãï¼ãã®æç¹ã§ã¯PSR2ãæå®ããã®ã¨åãç¶æ
ã§ãï¼ã
<?xml version="1.0"?> <ruleset name="Custom_PSR2"> <description>Custom ruleset Based on PSR2</description> <rule ref="PSR2" /> </ruleset>
次㫠ã©ã¤ãã©ãªããã¹ã対象ã¨ããããªããã£ã¬ã¯ããªãªã©ãé¤å¤å¯¾è±¡ã«æå®ãã¾ãã
<?xml version="1.0"?> <ruleset name="Custom_PSR2"> <description>Custom ruleset Based on PSR2</description> <!-- é¤å¤ããããã¡ã¤ã«ããã£ã¬ã¯ããªãåæ --> <exclude-pattern>*/vendor/*</exclude-pattern> <exclude-pattern>*/app/Console/cake.php</exclude-pattern> <rule ref="PSR2" /> </ruleset>
次㫠phpcs ã®ä¸è¨ã³ãã³ããå®è¡ãã¾ããããç¾æç¹ã®ã«ã¹ã¿ã è¦ç´ï¼ï¼PSR2ï¼ã«å¼ã£ããã£ã¦ããã¨ã©ã¼é ç®ãåæããã¾ãã
$ vendor/bin/phpcs -s --report=source --standard=./c-psr2.xml ./ PHP CODE SNIFFER VIOLATION SOURCE SUMMARY ---------------------------------------------------------------------- SOURCE COUNT ---------------------------------------------------------------------- [x] Generic.WhiteSpace.DisallowTabIndent.TabsUsed 71 [ ] Generic.Files.LineLength.TooLong 23 [x] Squiz.WhiteSpace.SuperfluousWhitespace.EndLine 16 [x] PSR2.Classes.ClassDeclaration.OpenBraceNewLine 15 [x] PSR2.Classes.ClassDeclaration.CloseBraceAfterBody 13 [x] Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword 8 [x] PSR2.Methods.FunctionCallSignature.MultipleArguments 8 [x] Generic.Functions.FunctionCallArgumentSpacing.TooMuchSpaceA 5 [x] Squiz.ControlStructures.ControlSignature.SpaceAfterClosePar 4 [x] Squiz.WhiteSpace.ControlStructureSpacing.SpacingBeforeClose 1 [x] Squiz.WhiteSpace.ControlStructureSpacing.SpacingAfterOpen 1 ---------------------------------------------------------------------- A TOTAL OF 165 SNIFF VIOLATIONS WERE FOUND IN 11 SOURCES ---------------------------------------------------------------------- PHPCBF CAN FIX THE 10 MARKED SOURCES AUTOMATICALLY (142 VIOLATIONS IN TOTAL) ---------------------------------------------------------------------- Time: 1.71 secs; Memory: 23.75Mb
ããã§ããããã«æ¤åºãããé ç®ãå ¨ã¦é¤å¤é ç®ã¨ãã¦c-psr2.xml ã§æå®ãã¾ãã
<?xml version="1.0"?> <ruleset name="Custom_PSR2"> <description>Custom ruleset Based on PSR2</description> <!-- é¤å¤ããããã¡ã¤ã«ããã£ã¬ã¯ããªãåæ --> <exclude-pattern>*/vendor/*</exclude-pattern> <exclude-pattern>*/app/Console/cake.php</exclude-pattern> <!-- PSR2ããã¼ã¹ã¨ããããç¶æ¿ --> <rule ref="PSR2"> <!-- é¤å¤ãããé ç®ãåæ --> <exclude name="Generic.WhiteSpace.DisallowTabIndent.TabsUsed"/> <exclude name="Generic.Files.LineLength.TooLong"/> <exclude name="Squiz.WhiteSpace.SuperfluousWhitespace.EndLine"/> <exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine"/> <exclude name="PSR2.Classes.ClassDeclaration.CloseBraceAfterBody"/> <exclude name="Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword"/> <exclude name="PSR2.Methods.FunctionCallSignature.MultipleArguments"/> <exclude name="Generic.Functions.FunctionCallArgumentSpacing.TooMuchSpaceA"/> <exclude name="Squiz.ControlStructures.ControlSignature.SpaceAfterClosePar"/> <exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingBeforeClose"/> <exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingAfterOpen"/> </rule> </ruleset>
ããã¨å度å®è¡ãã¦ãã¨ã©ã¼ãæ¤åºãããªããªãã¾ãã *2 ãPSR2ããã¼ã¹ã«éåããé ç®ãé¤å¤ããã¦ããç¶æ ã§ããããã§ãæãç·©ãã«ã¹ã¿ã è¦ç´ããå®æãã¾ããã
2. è¦ç´éåãå¢ããªãä»çµã¿ãä½ã
å®æçã«èªåãã§ãã¯ãã
å¼ç¤¾ã§ã¯TravisCIã§CIãåãã¦ããã®ã§ä»åã¯pushãPull Request(以ä¸ãPR)ã®ã¿ã¤ãã³ã°ã«åããã¦TravisCIä¸ã§å®è¡ããããã«ãã¾ããJenkinsãCircleCIãªã©ä»ã®CIãã¼ã«ã§ãåæ§ã®äºãã§ããã®ã§é©å®ä½¿ã£ã¦ãããã¼ã«ã«åããã¦èªã¿é²ãã¦ä¸ããã
TravisCIã®å ´åãåç´ã«å®è¡ã³ãã³ãã .travis.yml
ã«è¨è¿°ããã ãã§ããå¼ç¤¾ã§ã¯PRéçºãæ¡ç¨ãã¦ããã®ã§ä¸ãä¸ãæ°ããªã¨ã©ã¼é
ç®ãçºçããå ´åã§ãTravisãã¨ã©ã¼ãåãã¦ãã¼ã¸ãããã¨ãã§ããªããªãã¾ããããã§æ°ããã¨ã©ã¼é
ç®ãå¢ãããã¨ã¯ãªããªãã¾ãã*3ã
language: php php: - 7.0 sudo: false cache: directories: - $HOME/.composer/cache before_script: - vendor/bin/phpcs --standard=./c-psr2.xml ./
ããã¥ã¡ã³ããæ´åãã
ããã¾ã§ã®ä½æ¥ã§ã¨ã©ã¼é ç®ãæ°ãã«å¢ãããã¨ã¯ãªããªãã¾ããããå ·ä½çã«ä½ãæ£ããã¦ä½ãééã£ãæ¸ãæ¹ãªã®ããç¾æç¹ã§ã¯ãã¾ãã¡åããã¾ããããã®ã¾ã¾ã§ã¯phpcsã®ã¨ã©ã¼ãé »çºãä¿®æ£æéãå¢ãããã¨ãäºæ³ããã¾ãã
ãããªããªãããã«ãã³ã¼ãã£ã³ã°è¦ç´ã«é¢ããããã¥ã¡ã³ãã社å æ å ±å ±æãã¼ã«ã«ã¾ã¨ããå ±æãã¾ãããããã®1ãã¼ã¸ã«æ¸ããã¦ããªããã¨ã¯å®ããªãã¦OKï¼ãã¨ããåæã§æ¸ãããããã¥ã¡ã³ããªã®ã§ãããã¾ã§æ¸ããªãã¦ããã¨ããããã詳細ãªã¬ãã«ã¾ã§è¦å®ãã¦ãã¾ããããã«ããããããphpcsã§ã¨ã©ã¼ã«ãªãé »åº¦ãå°ãªããªãã¾ããã
âç®æ¬¡
â if é¢é£ã®è¦ç´
3. è¦ç´éåãæ¸ããä»çµã¿ãä½ã
å¼ç¤¾ã§ã¯ã¹ããªã³ãæ¯ã«ç¡å¹åãã¦ããè¦ç´ã®ä¸ããããã¤ãæå¹åããé ç®ãé¸ãã§å¯¾å¦ããããã«ãã¾ãããããããé ç®ã«ãã£ã¦ã¯1ã¤æå¹åããã ãã§ã¹ãã¼ã¹ãã¤ã³ãã³ãã®éããªã©ã«ãã£ã¦æ°åè¡ãæ°ç¾è¡ã®è¦ç´éåãæ¤åºãããå ´åãå°ãªããªãããããæåã§1ã¤1ã¤ä¿®æ£ãã¦ããã®ã¯ãã¹ãçºçããä¸ã«ãã¾ãã«éåµé çã§ãã
ããããå調ãªä½æ¥ã¯åºæ¥ãéãæ©æ¢°ã«ä»»ãã¾ãããããããªæã«å½¹ç«ã£ãã®ã phpcbf ã§ããphpcbf ã¯phpcsã¨åãã«ã¹ã¿ã è¦ç´(c-psr2.xml)ã使ã£ã¦èªåæ´å½¢ãããã¨ãåºæ¥ã¾ããã¾ãã³ãã³ããphpcsã¨ã»ã¼åããªã®ãå¬ããã¨ããã§ãã
ã§ã¯ãã£ããphpcbfã使ã£ã¦ã³ã¼ãã®èªåæ´å½¢ããã¦ã¿ã¾ãããã試ãã« c-psr2.xml ã§ç¡å¹åãã¦ããé
ç®ã®ä¸ããããã¤ãé¸ãã§è¨è¿°ãæ¶ããé
ç®ãæå¹å*4ãã¦ããä¸è¨ã³ãã³ããå®è¡ãã¾ãã大éã®å¦çã¡ãã»ã¼ã¸ãåºåããã¾ãã git status
ã§å¤æ´ãããã¡ã¤ã«ã確èªãããã¨ãã§ãã¾ãã
$ vendor/bin/phpcbf --standard=./c-psr2.xml ./app
念ã®ãã git diff
ã§èªåæ´å½¢ããç®æã確èªãåé¡ãªããã°ã³ããããã¦ãã¾ãã¾ãããããããã¦ã³ã¼ãã¬ãã¥ã¼ããã¦ãããã°å¤§ããªé害ãçºçãã確ç«ãããªãæããããã¯ãã§ãã
ãã®æé ããæ¯ã¹ããªã³ãããªã©æ±ºããããã¿ã¤ãã³ã°ã§å®è¡ããã°å°ããã¤ã³ã¼ãã£ã³ã°è¦ç´ã«æºæ ãã¦ãããã¨ãã§ããæçµçã«ã¯PSR2ã«æºæ ããã³ã¼ãã«ä¿®æ£ãããã¨ãã§ãã¾ãã
å®éã«ãã£ã¦ã¿ã¦
å¼ç¤¾ã§ã¯ä¸è¨æé ã«æ²¿ã£ã¦æ¢åã®ã³ã¼ããã¼ã¹ã«PSR2ãå®éã«é©ç¨ãã¾ãããçµæã¯ã2ã¶æç¨åº¦ã®çãæéã§ã»ã¼å ¨ã¦ã®é ç®ã«ã¤ãã¦æå¹åã§ãã¦ãã¾ã*5ã ã©ããã¦ãåªå ããªãã¦ã¯ãããªããªãªã¼ã¹ãªã©ãç«ã¦è¾¼ãã ææã«ãæå¹åããé ç®ã®æ°ãæ¸ããã¦æè»ã«èª¿æ´ã§ããã®ãä»åãç´¹ä»ããæ¹æ³ã®ã¡ãªããã ã¨æãã¾ãã
ãããã«
ãããã ã£ãã§ãããããã³ã¼ãã£ã³ã°è¦ç´ã¯å°å ¥ããç¬éã«ä¾¡å¤ãåºããã®ã§ã¯ããã¾ãããä¸é·æçã«è¦ã¦ãã³ã¼ãã£ã³ã°è¦ç´ã«ãã£ã¦ã³ã¼ãã®å質ãåä¸ããã¡ã³ããã³ã¹æ§ãè¤éæ§ãªã©ãæ¹åãããã¨ãæå¾ ã§ãã¾ããä»ã¾ã§å°å ¥ãé¿ãã¦ããæ¹ããã²ä»åã®"ãããã"ã³ã¼ãã£ã³ã°è¦ç´ã®å°å ¥ã試ãã¦ããã ããã°å¹¸ãã§ãï¼
*1:https://github.com/squizlabs/PHP_CodeSniffer
*2:ãã¾ã«ã¨ã©ã¼é ç®ãé¤å¤ãã¦ãã¨ã©ã¼ãåºç¶ããå ´åãããã¾ãããã®å ´åã¯è¦ªã®é ç®åã§æå®ãã¾ãããããSquiz.ControlStructures.ControlSignature.SpaceAfterCloseParããªããSquiz.ControlStructures.ControlSignatureãã«ããã¨è¦ªã®é ç®åã«ããæå®ã«ãªãã¾ãï¼.ã¾ã§åãï¼
*3:ãã¡ãããã¼ã«ã«ã§ãåéçºè ãã³ãã³ãå®è¡ããããé©å®Linterã®è¨å®ããã¾ãã
*4:æå¹åããé ç®ã¯éçºã¡ã³ãã¼ã«éæå ±æãã¦ãããã¨ä¸å¯§ã ã¨æãã¾ã
*5:CakePHPã使ã£ã¦ããã®ã§åå空éå¨ãã®PSR2è¦ç´ã«å¯¾å¿ã§ããªããã