首页 » 乱七八糟 » PHP PSR2 与 PHPCS 相关

PHP PSR2 与 PHPCS 相关

PSR2 is ?

github上一个中文介绍的项目

phpcs (php_codesniffer)

  • Install : composer global require "squizlabs/php_codesniffer=*"

  • PSR-1 的程式出現在 1.3.5 ,PSR-2 出現在 1.4.0

  • phpcs --config-set default_standard PSR2

IDE

  • sublime text 参考配置

  • phpstorm

      preferences -> Editor -> Code Style -> PHP 
    
      -> set from...(右上角) -> Predefined Style -> PSR1/PSR2
      
    

git hook

  • 安装 phpcs

      composer global require "squizlabs/php_codesniffer=*"
    
  • Link

      ln -s ~/.composer/vendor/bin/phpcs /usr/local/bin/phpcs
    
  • edit hook

  1.  vi {project_root}/.git/hooks/pre-commit
    
  2.  #!/bin/sh
     PHPCS_BIN=/usr/local/bin/phpcs
     PHPCS_CODING_STANDARD=PSR2
     PHPCS_FILE_PATTERN="\.(php)$"
    
     FILES=$(git diff --name-only  --cached)
    
     if [ "$FILES" == "" ]; then
         exit 0
     fi
    
     for FILE in $FILES
     do
         echo "$FILE" | egrep -q "$PHPCS_FILE_PATTERN"
         RETVAL=$?
         if [ "$RETVAL" -eq "0" ]
         then
    
             PHPCS_OUTPUT=$($PHPCS_BIN --standard=$PHPCS_CODING_STANDARD $FILE)
             PHPCS_RETVAL=$?
    
             if [ $PHPCS_RETVAL -ne 0 ];
             then
                 echo $PHPCS_OUTPUT
                 exit 1
             fi
         fi
     done
     exit 0
    
  3.  chmod +x {project_root}/.git/hooks/pre-commit
    
  • all