2. Anything that can go wrong will go wrong (Murphy's law)
3. A man is (a little bit) lazy
3. A man is (a little bit) non-conformist
2. Not following code standards, best practices, different approach to code formatting in the same team
3. Structural problems: not secure, not performing, not maintainable code
2. Reputational
3. Time
Processes
Instruments
Built-in native functionality
php -l {filename}
Example:
php -l sites/all/modules/custom/with_error.php
PHP Parse error: syntax error, unexpected '?>'
in sites/all/modules/custom/with_error.php on line 4
Errors parsing sites/all/modules/custom/with_error.php
Automatic code analysis on violation of the coding standards
<?xml version="1.0"?>
<ruleset name="Minimal rule set"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="
http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>
Custom rule set to be integrated into pre-commit hook.
</description>
<rule ref="rulesets/codesize.xml/CyclomaticComplexity"/>
<rule ref="rulesets/codesize.xml/ExcessiveMethodLength"/>
<rule ref="rulesets/codesize.xml/ExcessiveClassLength"/>
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity"/>
<rule ref="rulesets/design.xml/CouplingBetweenObjects"/>
<rule ref="rulesets/design.xml/DevelopmentCodeFragment"/>
</ruleset>
How many people will run this beautiful tools during daily routine?
And maybe let's try to automate this somehow?
#!/bin/bash
continue_consent() {
printf "${BLUE}Do you want to force commit without fixing? (y or n) ${NC}\n"
read inp </dev/tty
if [ "$inp" != "y" ]
then
printf "${GREEN}Fix the error and commit again.${NC}\n"
exit 1
else
printf "${RED}I hope you know what you do... :(${NC}\n"
fi
}
# Colours list
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
printf "${BLUE}Starting pre-commit hook...${NC}\n"
PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"`
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep -E \\\\.php\|\.module\|\.inc\|\.install\|\.test\|\.profile\|\.theme\|\.info`
# Determine if a file list is passed
if [ "$#" -eq 1 ]
then
oIFS=$IFS
IFS='
'
SFILES="$1"
IFS=$oIFS
fi
SFILES=${SFILES:-$STAGED_FILES_CMD}
printf "${BLUE}Running PHP Lint...${NC}\n"
for FILE in $SFILES
do
php -l -d display_errors=0 $PROJECT/$FILE
if [ $? != 0 ]
then
printf "${RED}Fix the error before commit.${NC}\n"
exit 1
fi
FILES="$FILES $PROJECT/$FILE"
FILES_COMMA="$FILES_COMMA$PROJECT/$FILE,"
done
# Remove trailing comma character
FILES_COMMA="${FILES_COMMA%"${FILES_COMMA##*[![:punct:]]}"}"
if [ "$FILES" != "" ]
then
printf "${BLUE}Running Code Sniffer - Drupal...${NC}\n"
{path_to_cs} --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,info
--ignore=core,node_modules,bower_components,vendor --encoding=utf-8 -n -p $FILES
if [ $? != 0 ]
then
printf "${BLUE}Do you want to run PHPCBF to automatically fix the issues? (y or n) ${NC}\n"
read phpcbf </dev/tty
if [ "$phpcbf" == "y" ]
then
{path_to_bf} --standard=Drupal
--extensions=php,module,inc,install,test,profile,theme,info
--ignore=core,node_modules,bower_components,vendor --encoding=utf-8 -n -p $FILES
{path_to_cs} --standard=Drupal
--extensions=php,module,inc,install,test,profile,theme,info
--ignore=core,node_modules,bower_components,vendor --encoding=utf-8 -n -p $FILES
if [ $? != 0 ]
then
continue_consent
else
printf "${GREEN}All is fixed!${NC}\n"
fi
else
continue_consent
fi
fi
printf "${BLUE}Running Code Sniffer - DrupalPractice...${NC}\n"
{path_to_cs} --standard=DrupalPractice
--extensions=php,module,inc,install,test,profile,theme,info
--ignore=core,node_modules,bower_components,vendor --encoding=utf-8 -n -p $FILES
if [ $? != 0 ]
then
continue_consent
fi
printf "${BLUE}Running PHPMD...${NC}\n"
{path_to_md} $FILES_COMMA text minimal.xml --exclude core,node_modules,bower_components,vendor --suffixes php,module,inc,install
if [ $? != 0 ]
then
continue_consent
fi
fi
exit $?