├── .gitignore ├── LICENSE.md ├── README.md ├── composer.json └── src ├── pre-commit └── setup.sh /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | DS_Store 3 | /vendor/ 4 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Sergey Gladkovskiy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHPCS git pre-commit hook 2 | 3 | ## About 4 | 5 | Auto installed git pre-commit hook for running [PHP Code Sniffer](https://github.com/squizlabs/PHP_CodeSniffer) 6 | code checking to PSR2 coding standard compliance. It checks only files that are to be committed. 7 | 8 | Inspired by [Enforce code standards with composer, git hooks, and phpcs](http://tech.zumba.com/2014/04/14/control-code-quality/) 9 | 10 | ## Installation 11 | 12 | Install `smgladkovskiy/phpcs-git-pre-commit` with composer require command: 13 | 14 | composer require --dev "smgladkovskiy/phpcs-git-pre-commit" 15 | 16 | Or alternatively, include a dependency for `smgladkovskiy/phpcs-git-pre-commit` in your composer.json file manually: 17 | 18 | { 19 | "require-dev": { 20 | "smgladkovskiy/phpcs-git-pre-commit": "dev-master" 21 | } 22 | } 23 | 24 | To enable code sniff, аdd to `post-install-cmd` and `post-update-cmd` in `composer.json` installation script: 25 | 26 | "scripts": { 27 | "install-hooks": ["sh ./vendor/smgladkovskiy/phpcs-git-pre-commit/src/setup.sh"], 28 | "post-install-cmd": ["@install-hooks"], 29 | "post-update-cmd": ["@install-hooks"] 30 | } 31 | 32 | Then run `composer install` or `composer update`. `pre-commit` hook will be installed or updated if it already exists. 33 | 34 | ## Usage 35 | 36 | Run `git commit` and pre-commit hook will check your committed files like if you run 37 | 38 | php phpcs.phar --standard=PSR2 --encoding=utf-8 -n -p /path/to/file.php 39 | 40 | ## Contributing 41 | 42 | Feel free to make pull requests! 43 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "smgladkovskiy/phpcs-git-pre-commit", 3 | "description": "PHPCS check before commit", 4 | "type": "scripts", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Sergey Gladkovskiy", 9 | "email": "smgladkovskiy@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "squizlabs/php_codesniffer": "^2.7 || ^3.0" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | PROJECT=$(php -r "echo dirname(dirname(dirname(realpath('$0'))));") 4 | STAGED_FILES_CMD=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\.php) 5 | UNSTAGED_FILES_CMD=$(git diff --name-only --diff-filter=ACMR | grep \\.php) 6 | 7 | # Determine if a file list is passed 8 | if [ "$#" -eq 1 ] 9 | then 10 | oIFS=$IFS 11 | IFS=' 12 | ' 13 | SFILES="$1" 14 | IFS=$oIFS 15 | fi 16 | 17 | SFILES=${SFILES:-$STAGED_FILES_CMD} 18 | 19 | STAGED_BUT_MODIFIED_FILES=$(php -r "\$sfiles=(explode(\"\\n\", '$SFILES'));\$usfiles=(explode(\"\\n\", '$UNSTAGED_FILES_CMD'));echo implode(\"\\n\",array_intersect(\$usfiles,\$sfiles));") 20 | 21 | if [ -z "$STAGED_BUT_MODIFIED_FILES" ]; then 22 | echo "OK" 23 | else 24 | echo "Files staged but then modified:\n" 25 | echo "${STAGED_BUT_MODIFIED_FILES}" 26 | exit 1 27 | fi 28 | 29 | 30 | echo "Checking PHP Lint..." 31 | for FILE in $SFILES 32 | do 33 | php -l -d display_errors=0 $PROJECT/$FILE 34 | if [ $? != 0 ] 35 | then 36 | echo "Fix the error before commit." 37 | exit 1 38 | fi 39 | FILES="$FILES $PROJECT/$FILE" 40 | done 41 | 42 | if [ "$FILES" != "" ] 43 | then 44 | echo "Running Code Sniffer." 45 | ./vendor/bin/phpcs -n -p $FILES 46 | if [ $? != 0 ] 47 | then 48 | echo "Fix the error before commit!" 49 | echo "Run" 50 | echo " ./vendor/bin/phpcbf $FILES" 51 | echo "for automatic fix or fix it manually." 52 | exit 1 53 | fi 54 | fi 55 | 56 | exit $? 57 | -------------------------------------------------------------------------------- /src/setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -e .git/hooks/pre-commit ]; 4 | then 5 | PRE_COMMIT_EXISTS=1 6 | else 7 | PRE_COMMIT_EXISTS=0 8 | fi 9 | 10 | cp ./vendor/smgladkovskiy/phpcs-git-pre-commit/src/pre-commit .git/hooks/pre-commit 11 | chmod +x .git/hooks/pre-commit 12 | 13 | if [ "$PRE_COMMIT_EXISTS" = 0 ]; 14 | then 15 | echo "Pre-commit git hook is installed!" 16 | else 17 | echo "Pre-commit git hook is updated!" 18 | fi 19 | --------------------------------------------------------------------------------