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

Commit c081fd8

Browse files
author
Iikka Timlin
committed
Initial commit
0 parents  commit c081fd8

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed

.editorconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.json]
12+
indent_size = 4
13+
14+
[*.md]
15+
trim_trailing_whitespace = false
16+
indent_size = 4

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Run PHPCS on pre-commit hook
2+
3+
## Installation
4+
1. Add following lines to composer.json:
5+
```json
6+
scripts: {
7+
"install-pre-commit-hook": ["bash ./vendor/urbanproof/phpcs-pre-commit/setup.sh"],
8+
"post-install-cmd": ["@install-pre-commit-hook"],
9+
"post-update-cmd": ["@install-pre-commit-hook"]
10+
},
11+
"repositories": [
12+
{
13+
"type": "vcs",
14+
"url": "https://github.com/Urbanproof/phpcs-pre-commit.git"
15+
}
16+
]
17+
```
18+
2. Require; `composer require --dev "urbanproof/phpcs-pre-commit"`

composer.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "urbanproof/phpcs-pre-commit",
3+
"description": "Run code quality checks automatically before each commit.",
4+
"type": "library",
5+
"require": {
6+
"squizlabs/php_codesniffer": "^3.5.6"
7+
},
8+
"license": "WTFPL v2",
9+
"authors": [
10+
{
11+
"name": "Iikka Timlin",
12+
"homepage": "https://github.com/Urbanproof/"
13+
}
14+
]
15+
}

pre-commit

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
PROJECT=`php -r "echo dirname(dirname(dirname(dirname(realpath('$0')))));"`
3+
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`
4+
5+
# This checks if a list of files was given. If so, it will be used instead of parsing the files in the index
6+
if [ "$#" -eq 1 ]
7+
then
8+
oIFS=$IFS
9+
IFS='
10+
'
11+
SFILES="$1"
12+
IFS=$oIFS
13+
fi
14+
SFILES=${SFILES:-$STAGED_FILES_CMD}
15+
16+
echo "Running PHP Linter..."
17+
for FILE in $SFILES
18+
do
19+
php -l -d display_errors=0 $PROJECT/$FILE
20+
if [ $? != 0 ]
21+
then
22+
echo "File $FILE contains PHP errors. Fix before commit."
23+
exit 1
24+
fi
25+
FILES="$FILES $PROJECT/$FILE"
26+
done
27+
28+
if [ "$FILES" != "" ]
29+
then
30+
echo "Running PHPCS"
31+
./vendor/bin/phpcs
32+
if [ $? != 0 ]
33+
then
34+
echo "Code style / security issues detected. Fix the errors before commit!"
35+
echo "Run the following command to list issues:"
36+
echo " ./vendor/bin/phpcs"
37+
echo "on run"
38+
echo " ./vendor/bin/phpcbf"
39+
echo "to fix issues automatically (wherever possible)."
40+
exit 1
41+
fi
42+
fi
43+
44+
exit $?

setup.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
SOURCE="$PWD/vendor/urbanproof/phpcs-pre-commit/pre-commit"
3+
TARGET="$PWD/.git/hooks/pre-commit"
4+
chmod +x $SOURCE
5+
ln -s $SOURCE $TARGET
6+
echo "Pre-commit hook installed."

0 commit comments

Comments
 (0)