-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from craig-davis/php-cs-fixer
Add new php-cs-fixer hook with new helpers
- Loading branch information
Showing
7 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ Just add to your `.pre-commit-config.yaml` file with the following | |
- id: php-cs | ||
files: \.(php)$ | ||
args: [--standard=PSR1 -p] | ||
- id: php-cbf | ||
- id: php-cbf | ||
files: \.(php)$ | ||
args: [--standard=PSR1 -p] | ||
``` | ||
|
@@ -108,3 +108,17 @@ If you have multiple standards or a comma in your `args` property, escape the co | |
``` | ||
|
||
To install PHP Codesniffer (phpcs & phpcbf), follow the [recommended steps here](https://github.com/squizlabs/PHP_CodeSniffer#installation). | ||
|
||
## php-cs-fixer | ||
```yaml | ||
- repo: [email protected]:hootsuite/pre-commit-php.git | ||
sha: 1.1.0 | ||
hooks: | ||
- id: php-cs-fixer | ||
files: \.(php)$ | ||
args: [--level=PSR2] | ||
``` | ||
Similar pattern as the php-cs hook. A bash script that will run the appropriate [PHP Coding Standards Fixer](http://cs.sensiolabs.org/) executable and to fix errors according to the configuration. It accepts all of the args from the `php-cs-fixer` command, in particular the `--level`, `--config`, and `--config-file` options. | ||
|
||
The tool will fail a build when it has made changes to the staged files. This allows a developer to do a `git diff` and examine the changes that it has made. Remember that you may omit this if needed with a `SKIP=php-cs-fixer git commit`. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
################################################################################ | ||
# | ||
# Terminal Colors | ||
# | ||
################################################################################ | ||
|
||
# Regular | ||
txtblk='\033[0;30m' # Black | ||
txtred='\033[0;31m' # Red | ||
txtgrn='\033[0;32m' # Green | ||
txtylw='\033[0;33m' # Yellow | ||
txtblu='\033[0;34m' # Blue | ||
txtpur='\033[0;35m' # Purple | ||
txtcyn='\033[0;36m' # Cyan | ||
txtwht='\033[0;37m' # White | ||
|
||
# Bold | ||
bldblk='\033[1;30m' # Black | ||
bldred='\033[1;31m' # Red | ||
bldgrn='\033[1;32m' # Green | ||
bldylw='\033[1;33m' # Yellow | ||
bldblu='\033[1;34m' # Blue | ||
bldpur='\033[1;35m' # Purple | ||
bldcyn='\033[1;36m' # Cyan | ||
bldwht='\033[1;37m' # White | ||
|
||
# Underline | ||
unkblk='\033[4;30m' # Black | ||
undred='\033[4;31m' # Red | ||
undgrn='\033[4;32m' # Green | ||
undylw='\033[4;33m' # Yellow | ||
undblu='\033[4;34m' # Blue | ||
undpur='\033[4;35m' # Purple | ||
undcyn='\033[4;36m' # Cyan | ||
undwht='\033[4;37m' # White | ||
|
||
# Background | ||
bakblk='\033[40m' # Black | ||
bakred='\033[41m' # Red | ||
bakgrn='\033[42m' # Green | ||
bakylw='\033[43m' # Yellow | ||
bakblu='\033[44m' # Blue | ||
bakpur='\033[45m' # Purple | ||
bakcyn='\033[46m' # Cyan | ||
bakwht='\033[47m' # White | ||
|
||
txtrst='\033[0m' # Text Reset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
################################################################################ | ||
# | ||
# Text formatters and manipulators | ||
# | ||
################################################################################ | ||
|
||
hr() { | ||
local start=$'\e(0' end=$'\e(B' line='qqqqqqqqqqqqqqqq' | ||
local cols=${COLUMNS:-$(tput cols)} | ||
while ((${#line} < cols)); do line+="$line"; done | ||
printf '%s%s%s\n' "$start" "${line:0:cols}" "$end" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
################################################################################ | ||
# | ||
# Locate the command from a list of options | ||
# | ||
################################################################################ | ||
|
||
# Final location of the executable that we found by searching | ||
exec_command="" | ||
|
||
# A phar file will need to be called by php | ||
prefixed_local_command="php $local_command" | ||
|
||
if [ -f "$vendor_command" ]; then | ||
exec_command=$vendor_command | ||
elif hash $global_command 2>/dev/null; then | ||
exec_command=$global_command | ||
elif [ -f "$local_command" ]; then | ||
phpcsfixer_command=$prefixed_local_command | ||
else | ||
echo -e "${bldred}No valid ${title} found!${txtrst}" | ||
echo "Please have one available as one of the following:" | ||
echo " * $local_command" | ||
echo " * $vendor_command" | ||
echo " * $global_command" | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
################################################################################ | ||
# | ||
# Show our welcome banner | ||
# | ||
################################################################################ | ||
|
||
echo -en "${bldgrn}Begin ${title}${txtrst} \n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/bin/bash | ||
################################################################################ | ||
# | ||
# Bash PHP Coding Standards Fixer | ||
# | ||
# This will prevent a commit if the tool has made changes to the files. This | ||
# allows a develop to look at the diff and make changes before doing the | ||
# commit. | ||
# | ||
# Exit 0 if no errors found | ||
# Exit 1 if errors were found | ||
# | ||
# Requires | ||
# - php | ||
# | ||
# Arguments | ||
# - None | ||
# | ||
################################################################################ | ||
|
||
# Plugin title | ||
title="PHP Code Fixer" | ||
|
||
# Possible command names of this tool | ||
local_command="php-cs-fixer.phar" | ||
vendor_command="vendor/bin/php-cs-fixer" | ||
global_command="php-cs-fixer" | ||
|
||
# Print a welcome and locate the exec for this tool | ||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
source $DIR/helpers/colors.sh | ||
source $DIR/helpers/formatters.sh | ||
source $DIR/helpers/welcome.sh | ||
source $DIR/helpers/locate.sh | ||
|
||
# Build our list of files, and our list of args by testing if the argument is | ||
# a valid path | ||
args="" | ||
files=() | ||
for arg in ${*} | ||
do | ||
if [ -e $arg ]; then | ||
files+=("$arg") | ||
else | ||
args+=" $arg" | ||
fi | ||
done; | ||
|
||
# Run the command on each file | ||
echo -e "${txtgrn} $exec_command fix${args}${txtrst}" | ||
php_errors_found=false | ||
error_message="" | ||
for path in "${files[@]}" | ||
do | ||
${exec_command} fix${args} ${path} 1> /dev/null | ||
if [ $? -ne 0 ]; then | ||
error_message+=" - ${txtylw}${path}${txtrst}\n" | ||
php_errors_found=true | ||
fi | ||
done; | ||
|
||
# There is currently debate about exit codes in php-cs-fixer | ||
# https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/1211 | ||
if [ "$php_errors_found" = true ]; then | ||
echo -en "\n${txtylw}${title} updated the following files:${txtrst}\n" | ||
echo -en "${error_message}" | ||
echo -en "\n${bldred}Please review and commit.${txtrst}\n" | ||
exit 1 | ||
fi | ||
|
||
exit 0 |