themes-wordpress/add-strict-types.sh
Vicente Canales 419613f85f
Add a script that inserts strict type declarations (#7716)
* Add a scripts that adds strict type declarations

Checks all php files in a given directory for existing strict type
declartions, and if it doesn't find one, adds it.

* add strict types on deploy

* add missing quotes

* Added npm command to trigger strict type additions and passed 'last deployed hash' to get changed themes

* fixed bash command and added error catching so errors won't stop deployment

* Add condition for debug output

---------

Co-authored-by: Jason Crist <jcrist@pbking.com>
2024-04-15 16:57:02 -04:00

27 lines
892 B
Bash
Executable file

#!/bin/bash
# Verify an argument is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <directory>"
echo "Error: Directory argument missing."
exit 1
fi
# Directory containing PHP files, passed as an argument
DIRECTORY="$1"
# Loop over each PHP file in the specified directory
find "$DIRECTORY" -type f -name "*.php" | while read -r file; do
[ -n "$DEBUG" ] && echo "Processing file: $file"
# Check if the file contains the strict_types declaration
if ! grep -qE 'declare\s*\(\s*strict_types\s*=\s*1\s*\)\s*;' "$file"; then
[ -n "$DEBUG" ] && echo "Declaration not found in: $file"
# If not, prepend the strict_types declaration
{
echo '<?php declare( strict_types = 1 ); ?>'
cat "$file"
} > "$file.tmp" && mv "$file.tmp" "$file"
else
[ -n "$DEBUG" ] && echo "Declaration found in: $file"
fi
done