Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Iikka Timlin committed Sep 17, 2020
0 parents commit c081fd8
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.json]
indent_size = 4

[*.md]
trim_trailing_whitespace = false
indent_size = 4
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Run PHPCS on pre-commit hook

## Installation
1. Add following lines to composer.json:
```json
scripts: {
"install-pre-commit-hook": ["bash ./vendor/urbanproof/phpcs-pre-commit/setup.sh"],
"post-install-cmd": ["@install-pre-commit-hook"],
"post-update-cmd": ["@install-pre-commit-hook"]
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/Urbanproof/phpcs-pre-commit.git"
}
]
```
2. Require; `composer require --dev "urbanproof/phpcs-pre-commit"`
15 changes: 15 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "urbanproof/phpcs-pre-commit",
"description": "Run code quality checks automatically before each commit.",
"type": "library",
"require": {
"squizlabs/php_codesniffer": "^3.5.6"
},
"license": "WTFPL v2",
"authors": [
{
"name": "Iikka Timlin",
"homepage": "https://github.com/Urbanproof/"
}
]
}
44 changes: 44 additions & 0 deletions pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
PROJECT=`php -r "echo dirname(dirname(dirname(dirname(realpath('$0')))));"`
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`

# This checks if a list of files was given. If so, it will be used instead of parsing the files in the index
if [ "$#" -eq 1 ]
then
oIFS=$IFS
IFS='
'
SFILES="$1"
IFS=$oIFS
fi
SFILES=${SFILES:-$STAGED_FILES_CMD}

echo "Running PHP Linter..."
for FILE in $SFILES
do
php -l -d display_errors=0 $PROJECT/$FILE
if [ $? != 0 ]
then
echo "File $FILE contains PHP errors. Fix before commit."
exit 1
fi
FILES="$FILES $PROJECT/$FILE"
done

if [ "$FILES" != "" ]
then
echo "Running PHPCS"
./vendor/bin/phpcs
if [ $? != 0 ]
then
echo "Code style / security issues detected. Fix the errors before commit!"
echo "Run the following command to list issues:"
echo " ./vendor/bin/phpcs"
echo "on run"
echo " ./vendor/bin/phpcbf"
echo "to fix issues automatically (wherever possible)."
exit 1
fi
fi

exit $?
6 changes: 6 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
SOURCE="$PWD/vendor/urbanproof/phpcs-pre-commit/pre-commit"
TARGET="$PWD/.git/hooks/pre-commit"
chmod +x $SOURCE
ln -s $SOURCE $TARGET
echo "Pre-commit hook installed."

0 comments on commit c081fd8

Please sign in to comment.