Skip to content

Instantly share code, notes, and snippets.

@phlbnks
Last active February 9, 2023 14:52
Show Gist options
  • Save phlbnks/48c1cc71d8bba3e4558488c3a7cb9bde to your computer and use it in GitHub Desktop.
Save phlbnks/48c1cc71d8bba3e4558488c3a7cb9bde to your computer and use it in GitHub Desktop.
Check directory for PHP version compatibility, no dependencies except bash, PHP, Wget and Tar
#!/bin/bash
# vars
default_workdir=~/phpcompatabilitytest
default_targetversion="8.1"
default_ignore="*/vendor/*,*/sites/*,*/tests/*"
default_testdir="/var/www"
phpcsconfigfile="CodeSniffer.conf"
# config
echo
echo " Setup - customise variables or hit enter to accept defaults:"
read -r -p " Temp directory to create/use [ $default_workdir ]: " workdir
workdir=${workdir:-$default_workdir}
read -r -p " Target PHP version to check against [ $default_targetversion ]: " targetversion
targetversion=${targetversion:-$default_targetversion}
read -r -p " Ignore pattern [ $default_ignore ]: " ignore
ignore=${ignore:-$default_ignore}
read -r -p " Directory to test [ $default_testdir ]: " testdir
testdir=${testdir:-$default_testdir}
# sanity checks
if [[ ! -d "$testdir" ]] ; then echo "Error: $testdir doesn't exist, aborting"; exit ; fi
# setup
echo " Downloading requirements into $workdir"
mkdir -p "$workdir"
if [[ ! -d "$workdir" ]] ; then echo "Error: $workdir couldn't be created, check permissions or choose another location, aborting"; exit ; fi
cd "$workdir" || exit
## Get PHPCS
wget -q -O phpcs https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
## Get compatabilty sniff
if [[ -d "$workdir"/PHPCompatibility ]]
then rm -rf "$workdir"/PHPCompatibility
fi
mkdir "$workdir"/PHPCompatibility
wget -q -O "$workdir"/PHPCompatibility.tar.gz $(wget -q -O - 'https://api.github.com/repos/PHPCompatibility/PHPCompatibility/releases/latest' | jq -r '.tarball_url')
tar -xzf "$workdir"/PHPCompatibility.tar.gz -C "$workdir"/PHPCompatibility --strip-components 1
rm "$workdir"/PHPCompatibility.tar.gz
# config phpcs
if [[ -e ~/"$phpcsconfigfile" ]] ; then mv ~/"$phpcsconfigfile" "$workdir"/"$phpcsconfigfile".backup ; fi
php "$workdir"/phpcs --config-set installed_paths "$workdir"/PHPCompatibility/ >/dev/null
# run
echo " Starting test on $testdir"
php -d memory_limit=-1 "$workdir"/phpcs -p --ignore="$ignore" --extensions=php --runtime-set testVersion "$targetversion" --standard=PHPCompatibility "$testdir"
echo " Cleaning up files"
if [[ -e ~/"$phpcsconfigfile" ]] ; then rm ~/"$phpcsconfigfile" ; fi
if [[ -e "$workdir"/"$phpcsconfigfile".backup ]] ; then mv "$workdir"/"$phpcsconfigfile".backup ~/"$phpcsconfigfile" ; fi
rm -rf "$workdir"
echo "Done"
@phlbnks
Copy link
Author

phlbnks commented Feb 9, 2023

If you're happy with the contents, then you can run without downloading first:
bash <(wget -qO- https://gist.githubusercontent.com/phlbnks/48c1cc71d8bba3e4558488c3a7cb9bde/raw/66182b458359cc038c08c8eaf2a9ad26313cc71c/phpcompatabilitychecker.sh)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment