فهرست منبع

Proposal: automatically add strict types

This PR is an experiment that aims to check if it would be practical to
automatically add strict types to all PHP files that are missing them.
Initially, it will add them to _all_ PHP files, but con subsequen runs,
it'll only affect new files.

The strict types declaration has become a requirement on WordPress.com
in order to commit new PHP files to the codebase, and solving this
upstream (eg. on Create Block Theme) might not be the best, since
prescribing strict types to its broad userbase could prove problematic.
Vicente Canales 1 سال پیش
والد
کامیت
e87143364c
1فایلهای تغییر یافته به همراه51 افزوده شده و 0 حذف شده
  1. 51 0
      .github/workflows/add-strict-types.yml

+ 51 - 0
.github/workflows/add-strict-types.yml

@@ -0,0 +1,51 @@
+name: Add Strict Types
+
+on:
+  pull_request:
+    branches:
+      - trunk
+
+jobs:
+  add-strict-types:
+    runs-on: ubuntu-latest
+
+    steps:
+      - name: Checkout code
+        uses: actions/checkout@v2
+
+      - name: Install PHP
+        uses: shivammathur/setup-php@v2
+        with:
+          php-version: '8.1'
+
+      - name: Checking for new PHP files
+        id: check
+        run: |
+          if [[ $(git diff --name-only HEAD^ HEAD | grep -E "\.php$") ]]; then
+            if [[ $(git diff --name-only HEAD^ HEAD | grep -E "\.php$" | xargs grep -L "declare\(strict_types=1\)" | wc -l) -gt 0 ]]; then
+              echo "::set-output name=strict_types::false"
+            else
+              echo "::set-output name=strict_types::true"
+            fi
+          else
+            echo "::set-output name=strict_types::true"
+          fi
+      
+      - name: Add strict types
+        if: steps.check.outputs.strict_types == 'false'
+        run: |
+          git diff --name-only HEAD^ HEAD | grep -E "\.php$" | xargs sed -i '1s/^/<?php declare(strict_types=1);\n\n/'
+          git diff --name-only HEAD^ HEAD | grep -E "\.php$" | xargs git add
+
+      - name: Create PR
+        if: steps.check.outputs.strict_types == 'false'
+        uses: peter-evans/create-pull-request@v3
+        with:
+          token: ${{ secrets.GITHUB_TOKEN }}
+          commit-message: "Automation: Add strict types to PHP files"
+          title: "[Automation]: Add strict types"
+          body: |
+            We've found some PHP files that don't have strict types. This PR adds them.
+          branch: add-strict-types-${{ github.sha }}
+          branch-suffix: timestamp
+          labels: Automation