Skip to content

Commit b0dd6a7

Browse files
author
Allyson Silva
committed
Refactoring for bash shell
fix #1
1 parent 7f3403a commit b0dd6a7

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

pre-commit

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/usr/bin/env bash
22

33
##########
44
# Git Pre-Commit file for PHP projects.
@@ -11,8 +11,8 @@
1111
# - PHP Mess Detector (PHPMD) (https://phpmd.org/)
1212
# - PHP Copy/Paste Detector (PHPCPD) (https://github.com/sebastianbergmann/phpcpd)
1313
#
14-
# @version 1.0.0
15-
# @author Allyson Silva <contato@allyson.tech>
14+
# @version 1.0.1
15+
# @author Allyson Silva <allysonsilvaweb@gmail.com>
1616
##########
1717

1818
cat <<\EOF
@@ -126,19 +126,19 @@ color_reset='\033[0m'
126126
###########
127127

128128
function message_failure() {
129-
echo "${color_bold_white}${color_background_red} 🤦 $1 ${color_reset}\n"
129+
printf "${color_bold_white}${color_background_red} 🤦 $1 ${color_reset}\n"
130130
}
131131

132132
function message_success() {
133-
echo "${color_bold_black}${color_background_green} $1 🍺 ${color_reset}\n"
133+
printf "${color_bold_black}${color_background_green} $1 🍺 ${color_reset}\n"
134134
}
135135

136136
function message_warning() {
137-
echo "${color_bold_black}${color_background_yellow}$1 ${color_reset}\n"
137+
printf "${color_bold_black}${color_background_yellow}$1 ${color_reset}\n"
138138
}
139139

140140
function message_info() {
141-
echo "${color_bold_black}${color_background_blue} ☝️️ $1 ${color_reset}\n"
141+
printf "${color_bold_black}${color_background_blue} ☝️️ $1 ${color_reset}\n"
142142
}
143143

144144
######################
@@ -179,7 +179,7 @@ staged_files_separated_by_comma=${staged_files// /,}
179179
if [ -n "$files" ]; then
180180

181181
echo "╭──────────────────────────────────╮"
182-
echo "|〈〈〈 ${color_bold_white}Checking PHP Lint...${color_reset} 〉〉〉|"
182+
printf "|〈〈〈 ${color_bold_white}Checking PHP Lint...${color_reset} 〉〉〉|\n"
183183
echo "╰──────────────────────────────────╯"
184184
echo
185185

@@ -210,7 +210,7 @@ if [ -n "$files" ]; then
210210
fi
211211

212212
echo "╭────────────────────────────────────────────────────────────────────────────────╮"
213-
echo "|〈〈〈 ${color_bold_white}Running PHP CodeSniffer + PHP Code Beautifier. Code standard PSR2.${color_reset} 〉〉〉|"
213+
printf "|〈〈〈 ${color_bold_white}Running PHP CodeSniffer + PHP Code Beautifier. Code standard PSR2.${color_reset} 〉〉〉|\n"
214214
echo "╰────────────────────────────────────────────────────────────────────────────────╯"
215215
echo
216216

@@ -239,6 +239,7 @@ if [ -n "$files" ]; then
239239
phpcs_bin=$phpcs_bin
240240
else
241241
message_warning "No valid PHP Codesniffer executable found! Please have one available as either $phpcs_vendor_bin, $phpcs_global_bin or $phpcs_local_exec"
242+
echo
242243
exit 1
243244
fi
244245
fi
@@ -247,6 +248,7 @@ if [ -n "$files" ]; then
247248
if ! hash phpcbf 2>/dev/null; then
248249
message_warning "No valid PHP Code Beautifier executable found! Please have one available as either [phpcbf]."
249250
message_info "Visit: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Fixing-Errors-Automatically"
251+
echo
250252
exit 1
251253
fi
252254

@@ -259,9 +261,9 @@ if [ -n "$files" ]; then
259261
# Check for errors when running PHPCS.
260262
phpcs_errors=false
261263

262-
echo "${color_bold_black}${color_background_yellow}PHP CodeSniffer Arguments${color_reset} ${color_bold_yellow}${php_cs_cfb_common_arguments} ${phpcs_arguments}${color_reset}"
264+
printf "${color_bold_black}${color_background_yellow}PHP CodeSniffer Arguments${color_reset} ${color_bold_yellow}${php_cs_cfb_common_arguments} ${phpcs_arguments}${color_reset}\n"
263265
echo
264-
echo "${color_bold_black}${color_background_yellow}Use PHPCBF To Fix Problems${color_reset} ${color_bold_green}phpcbf${color_reset}${color_bold_yellow} ${php_cs_cfb_common_arguments} ${color_bold_purple}{FILES OR FOLDER}${color_reset}"
266+
printf "${color_bold_black}${color_background_yellow}Use PHPCBF To Fix Problems${color_reset} ${color_bold_green}phpcbf${color_reset}${color_bold_yellow} ${php_cs_cfb_common_arguments} ${color_bold_purple}{FILES OR FOLDER}${color_reset}\n"
265267
echo
266268

267269
for file in $files; do
@@ -282,9 +284,9 @@ if [ -n "$files" ]; then
282284

283285
if [ -n "$phpcbf_output" ]; then
284286
# File had some issues but they were automatically fixed.
285-
echo "${color_bold_green}Codestyle errors were fixed automatically! add those changes and commit again.${color_reset}"
287+
printf "${color_bold_green}Codestyle errors were fixed automatically! add those changes and commit again.${color_reset}\n"
286288
# Display PHPCBF filtered output.
287-
echo "$phpcbf_output"
289+
printf "${phpcbf_output}\n"
288290
fi
289291

290292
# Run PHP Code Style Check and detect in the fixer was not able to fix code.
@@ -297,7 +299,7 @@ if [ -n "$files" ]; then
297299

298300
# Display error and PHPCS filtered output.
299301
# Display processing file error.
300-
echo "$phpcs_codestyle_errors"
302+
printf "${phpcs_codestyle_errors}\n"
301303

302304
# Adding error message.
303305
errors=("${errors[@]}" "$output")
@@ -308,26 +310,29 @@ if [ -n "$files" ]; then
308310

309311
if [ "$phpcs_errors" = false ]; then
310312
message_success 'No Errors Found - PHP CodeSniffer + PHP Code Beautifier'
313+
echo
311314
fi
312315

313316
echo
314317
echo "╭─────────────────────────────────────────────────╮"
315-
echo "|〈〈〈 ${color_bold_white}PHP Coding Standards Fixer(PSR2)...${color_reset} 〉〉〉|"
318+
printf "|〈〈〈 ${color_bold_white}PHP Coding Standards Fixer(PSR2)...${color_reset} 〉〉〉|\n"
316319
echo "╰─────────────────────────────────────────────────╯"
317320
echo
318321

319322
message_info "Running PHP Code Sniffer..."
323+
echo
320324

321325
# Verificando se arquivo binário executável existe para [php-cs-fixer].
322326
if ! hash php-cs-fixer 2>/dev/null; then
323327
message_warning "No valid PHP Coding Standards Fixer executable found! Please have one available as either ✖✖✖ php-cs-fixer ✖✖✖"
324328
message_info "Visit: https://github.com/FriendsOfPHP/PHP-CS-Fixer#installation"
329+
echo
325330
exit 1
326331
fi
327332

328-
echo "${color_bold_yellow}✔︎ Rules: PSR2, Symfony${color_reset}"
329-
echo "${color_bold_yellow}✔︎ Using Cache: No${color_reset}"
330-
echo "${color_bold_yellow}✔︎ Output Format: Text(default)${color_reset}"
333+
printf "${color_bold_yellow}✔︎ Rules: PSR2, Symfony${color_reset}\n"
334+
printf "${color_bold_yellow}✔︎ Using Cache: No${color_reset}\n"
335+
printf "${color_bold_yellow}✔︎ Output Format: Text(default)${color_reset}\n"
331336
echo
332337

333338
git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | while read line; do
@@ -345,28 +350,29 @@ if [ -n "$files" ]; then
345350

346351
message_info "GIT ADD + PHP Coding Standards Fixer fixing: [ ${line} ]"
347352
else
348-
echo "${color_bold_yellow}File already ${color_background_yellow}${color_bold_black}FIXED${color_reset}${color_bold_yellow} and also already added to the ${color_background_yellow}${color_bold_black}GIT${color_reset}${color_bold_white}${color_underline_white}${line}${color_reset}"
353+
printf "${color_bold_yellow}File already ${color_background_yellow}${color_bold_black}FIXED${color_reset}${color_bold_yellow} and also already added to the ${color_background_yellow}${color_bold_black}GIT${color_reset}${color_bold_white}${color_underline_white}${line}${color_reset}\n"
349354
fi
350355
done
351356

352357
echo
353358
echo "╭──────────────────────────────────────────╮"
354-
echo "|〈〈〈 ${color_bold_white}PHPMD - PHP Mess Detector...${color_reset} 〉〉〉|"
359+
printf "|〈〈〈 ${color_bold_white}PHPMD - PHP Mess Detector...${color_reset} 〉〉〉|\n"
355360
echo "╰──────────────────────────────────────────╯"
356361
echo
357362

358363
if ! hash phpmd 2>/dev/null; then
359364
message_warning "No valid PHP Mess Detector executable found! Please have one available as either ✖✖✖ phpmd ✖✖✖"
360365
message_info "Visit: https://phpmd.org/download/index.html"
366+
echo
361367
exit 1
362368
fi
363369

364370
# @see https://phpmd.org/rules/index.html
365371
phpmd_rules="cleancode,codesize,controversial,design,naming,unusedcode"
366372

367-
echo "✔︎ Report Format: ${color_bold_yellow}text${color_reset}"
368-
echo "✔︎ Ruleset Files: ${color_bold_yellow}${phpmd_rules}${color_reset}"
369-
echo "✔︎ Suffixes: ${color_bold_yellow}php${color_reset}"
373+
printf "✔︎ Report Format: ${color_bold_yellow}text${color_reset}\n"
374+
printf "✔︎ Ruleset Files: ${color_bold_yellow}${phpmd_rules}${color_reset}\n"
375+
printf "✔︎ Suffixes: ${color_bold_yellow}php${color_reset}\n"
370376
echo
371377

372378
message_info "PHPMD - PHP Mess Detector: [ ${staged_files_separated_by_comma} ]"
@@ -379,7 +385,7 @@ if [ -n "$files" ]; then
379385
echo
380386
message_failure "PHPMD - PHP Mess Detector found some errors. Fix the errors before commit."
381387

382-
echo "$phpmd_output"
388+
printf "${phpmd_output}\n"
383389

384390
# Adding error message.
385391
errors=("${errors[@]}" "$phpmd_output")
@@ -390,14 +396,15 @@ if [ -n "$files" ]; then
390396

391397
echo
392398
echo "╭─────────────────────────────────────────────────╮"
393-
echo "|〈〈〈 ${color_bold_white}PHP Copy/Paste Detector (PHPCPD)...${color_reset} 〉〉〉|"
399+
printf "|〈〈〈 ${color_bold_white}PHP Copy/Paste Detector (PHPCPD)...${color_reset} 〉〉〉|\n"
394400
echo "╰─────────────────────────────────────────────────╯"
395401
echo
396402

397403
if ! hash phpcpd 2>/dev/null; then
398404
message_warning "No valid PHP Copy/Paste Detector executable found! Please have one available as either ✖✖✖ phpcpd ✖✖✖"
399-
echo "${color_bold_white}Please install phpcpd, e.g.:${color_reset}"
400-
echo "${color_bold_green}composer global require --dev 'sebastian/phpcpd=*'${color_reset}"
405+
printf "${color_bold_white}Please install phpcpd, e.g.:${color_reset}\n"
406+
printf "${color_bold_green}composer global require --dev 'sebastian/phpcpd=*'${color_reset}\n"
407+
echo
401408
exit 1
402409
fi
403410

@@ -419,7 +426,7 @@ if [ -n "$files" ]; then
419426
echo
420427
phpcpd_output="`cat $phpcpd_tmp`"
421428

422-
echo "$phpcpd_output"
429+
printf "${phpcpd_output}\n"
423430

424431
# Adding error message.
425432
errors=("${errors[@]}" "$phpcpd_output")

0 commit comments

Comments
 (0)