add-strict-types.sh 892 B

123456789101112131415161718192021222324252627
  1. #!/bin/bash
  2. # Verify an argument is provided
  3. if [ "$#" -ne 1 ]; then
  4. echo "Usage: $0 <directory>"
  5. echo "Error: Directory argument missing."
  6. exit 1
  7. fi
  8. # Directory containing PHP files, passed as an argument
  9. DIRECTORY="$1"
  10. # Loop over each PHP file in the specified directory
  11. find "$DIRECTORY" -type f -name "*.php" | while read -r file; do
  12. [ -n "$DEBUG" ] && echo "Processing file: $file"
  13. # Check if the file contains the strict_types declaration
  14. if ! grep -qE 'declare\s*\(\s*strict_types\s*=\s*1\s*\)\s*;' "$file"; then
  15. [ -n "$DEBUG" ] && echo "Declaration not found in: $file"
  16. # If not, prepend the strict_types declaration
  17. {
  18. echo '<?php declare( strict_types = 1 ); ?>'
  19. cat "$file"
  20. } > "$file.tmp" && mv "$file.tmp" "$file"
  21. else
  22. [ -n "$DEBUG" ] && echo "Declaration found in: $file"
  23. fi
  24. done