Browse Source

refactor how the declaration is added to the file

Vicente Canales 1 year ago
parent
commit
efc56c6165
1 changed files with 7 additions and 1 deletions
  1. 7 1
      .github/workflows/add-strict-types.yml

+ 7 - 1
.github/workflows/add-strict-types.yml

@@ -26,7 +26,13 @@ jobs:
           git fetch origin +refs/heads/trunk:refs/remotes/origin/trunk --quiet
           NEW_PHP_FILES=$(git diff --name-only refs/remotes/origin/trunk HEAD -- '*.php' | xargs -r grep -LE 'declare\( strict_types=1 \)' || true)
           if [ -n "$NEW_PHP_FILES" ]; then
-            echo "$NEW_PHP_FILES" | xargs -I {} sed -i '1s|^|<?php declare( strict_types=1 );\n|' {}
+            for file in $NEW_PHP_FILES; do
+              if grep -q '^<?php' "$file"; then
+                sed -i '/^<?php/a declare( strict_types=1 );' "$file"
+              else
+                sed -i '1s|^|<?php declare( strict_types=1 );\n|' "$file"
+              fi
+            done
             git add $NEW_PHP_FILES
             git commit -m "Add strict types to new PHP files"
             git push -u origin HEAD:refs/heads/add-strict-types-${{ github.sha }}