Skip to content

Instantly share code, notes, and snippets.

@PSF1
Last active September 28, 2023 13:52
Show Gist options
  • Save PSF1/aedf54d8a62e192e72c9de616ac6e04e to your computer and use it in GitHub Desktop.
Save PSF1/aedf54d8a62e192e72c9de616ac6e04e to your computer and use it in GitHub Desktop.
Drupal code style with DDEV
#!/bin/bash
# Fix code style automatically.
#
# To create the command 'ddev phpcbf', create in '.ddev/commands/web' a new file 'phpcbf'.
# This require, in project, install 'squizlabs/php_codesniffer' and 'drupal/coder' how a dev dependency.
# 2023 By Pedro Pelaez <aaaaa976@gmail.com>
folders=""
if [ $# -eq 0 ] ; then
if [ -d "./web/modules/custom" ]; then
echo "- Custom modules dir exist."
folders+=' ./web/modules/custom'
fi
if [ -d "./web/themes/custom" ]; then
echo "- Custom themes dir exist."
folders+=' ./web/themes/custom'
fi
if [ -d "./web/profiles/custom" ]; then
echo "- Custom profiles dir exist."
folders+=' ./web/profiles/custom'
fi
else
if [ ! -d "$1" ]; then
echo "$1 does not exist."
exit 1
fi
folders=$1
fi
echo "Reading $folders ..."
phpcbf --standard=Drupal,DrupalPractice \
--extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml \
${folders}
if [ $? -eq 0 ] ; then
echo "Code is OK"
fi
#!/bin/bash
# Code style review.
#
# To create the command 'ddev phpcs', create in '.ddev/commands/web' a new file 'phpcs'.
# This require, in project, install 'squizlabs/php_codesniffer' and 'drupal/coder' how a dev dependency.
# 2023 By Pedro Pelaez <aaaaa976@gmail.com>
folders=""
if [ $# -eq 0 ] ; then
if [ -d "./web/modules/custom" ]; then
echo "- Custom modules dir exist."
folders+=' ./web/modules/custom'
fi
if [ -d "./web/themes/custom" ]; then
echo "- Custom themes dir exist."
folders+=' ./web/themes/custom'
fi
if [ -d "./web/profiles/custom" ]; then
echo "- Custom profiles dir exist."
folders+=' ./web/profiles/custom'
fi
else
if [ ! -d "$1" ]; then
echo "$1 does not exist."
exit 1
fi
folders=$1
fi
echo "Reading $folders ..."
phpcs --standard=Drupal,DrupalPractice \
--extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml \
${folders}
if [ $? -eq 0 ] ; then
echo "Code is OK"
fi
#!/bin/bash
# PHPStan scans your whole codebase and looks for both obvious & tricky bugs.
#
# To create the command 'ddev phpstan', create in '.ddev/commands/web' a new file 'phpstan'.
# This require, in project, install "phpstan/phpstan", "phpstan/extension-installer",
# "mglaman/phpstan-drupal" & "phpstan/phpstan-deprecation-rules" how a dev dependency.
#
# To setup PHPStan, in the project root folder create a phpstan.neon file with:
# parameters:
# level: 0
# paths:
# - web/modules/custom
# - web/themes/custom
# - web/profiles/custom
# ignoreErrors:
# # new static() is a best practive in Drupal.
# - "#^Unsafe usage of new static#"
#
# 2023 by Pedro Pelaez <aaaaa976@gmail.com>
php vendor/bin/phpstan.phar
if [ $? -eq 0 ] ; then
echo "Code is OK"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment