Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: automatically add strict types #7542

Open
wants to merge 9 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions .github/workflows/add-strict-types.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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: Add strict types to new PHP files
id: add_strict_types
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
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
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 }}
echo "::set-output name=strict_types_needed::true"
else
echo "No PHP files need strict types."
echo "::set-output name=strict_types_needed::false"
fi

- name: Comment on PR
if: steps.add_strict_types.outputs.strict_types_needed == 'true'
uses: thollander/actions-comment-pull-request@v1
with:
message: "We've found some PHP files that don't have strict types. This commit adds them. Branch: add-strict-types-${{ github.sha }}"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# - name: Create PR
# if: steps.add_strict_types.outputs.strict_types_needed == 'true'
# uses: peter-evans/create-pull-request@v3
# with:
# token: ${{ secrets.GITHUB_TOKEN }}
# commit-message: "Add strict types to new 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
3 changes: 3 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo 'hi';
Loading