Skip to content

Instantly share code, notes, and snippets.

@tdlm
Created December 3, 2024 22:52
Show Gist options
  • Save tdlm/35872cfa508f551764cd65b824eda2bb to your computer and use it in GitHub Desktop.
Save tdlm/35872cfa508f551764cd65b824eda2bb to your computer and use it in GitHub Desktop.
PHP Compatibility Checker
#!/usr/bin/env bash
# CLI tool to check PHP compatibility for a WordPress codebase (or, just a PHP codebase).
#
# Requirements: PHP, Composer, PHP CodeSniffer (installed globally or via Composer)
# Check if Composer is installed
if ! [ -x "$(command -v composer)" ]; then
echo "Error: Composer is not installed." >&2
exit 1
fi
# Check if PHPCS is installed
if ! [ -x "$(command -v phpcs)" ]; then
echo "Installing PHP CodeSniffer and PHPCompatibility standards..."
composer global require squizlabs/php_codesniffer --no-progress --dev
phpcs --config-set installed_paths ~/.composer/vendor/phpcompatibility/php-compatibility
else
echo "PHPCS is already installed. Proceeding to compatibility check..."
fi
# Install PHPCompatibility using Composer
composer global require phpcompatibility/php-compatibility --no-progress --dev
# Function to check compatibility
check_compatibility() {
local path=$1
local php_version=$2
if [ ! -d "$path" ] && [ ! -f "$path" ]; then
echo "Error: The specified path '$path' does not exist."
exit 1
fi
# Create a timestamped and encoded report filename
local timestamp=$(date +"%Y%m%d%H%M%S")
local encoded_path=$(echo "$path" | tr '/' '_')
local report_filename="compatibility_report_${encoded_path}_php${php_version}_${timestamp}.txt"
echo "Running PHPCompatibility check on path: $path for PHP version: $php_version"
echo "Using PHPCompatibility with testVersion=${php_version}"
find "$path" -type f -name "*.php" ! -path "*/vendor/*" ! -path "*/cache/*" | xargs phpcs -d memory_limit=2048M -p -n -v --standard=PHPCompatibility --runtime-set testVersion "${php_version}" | grep -v "(0 errors, 0 warnings)" > "$report_filename"
echo "PHPCompatibility check completed. Report saved to $report_filename"
}
# Usage information
if [ $# -ne 2 ]; then
echo "Usage: ./check_php_compatibility.sh [codebase_path] [php_version]"
echo "Example: ./check_php_compatibility.sh /path/to/wordpress 7.4"
exit 1
fi
# Run compatibility check
codebase_path=$1
php_version=$2
check_compatibility "$codebase_path" "$php_version"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment