Tooling: lint new php files for strict types directive (take 2). (#48943)

This PRs is take two on expanding new php-files linting to validate strict types directive.
This commit is contained in:
Vladimir Reznichenko 2024-07-01 13:58:13 +02:00 committed by GitHub
parent d709d53d9a
commit e2ce65d02d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 6 deletions

View File

@ -10,12 +10,21 @@
baseBranch=${1:-"trunk"}
# Lint changed php-files to match code style.
changedFiles=$(git diff $(git merge-base HEAD $baseBranch) --relative --name-only --diff-filter=d -- '*.php')
# Only complete this if changed files are detected.
if [[ -z $changedFiles ]]; then
echo "No changed files detected."
exit 0
if [[ -n $changedFiles ]]; then
composer exec phpcs-changed -- -s --git --git-base $baseBranch $changedFiles
fi
composer exec phpcs-changed -- -s --git --git-base $baseBranch $changedFiles
# Lint new (excl. renamed) php-files to contain strict types directive.
newFiles=$(git diff $(git merge-base HEAD $baseBranch) --relative --name-only --diff-filter=dmr -- '*.php')
if [[ -n $newFiles ]]; then
passingFiles=$(find $newFiles -type f -exec grep -xl --regexp='declare(\s*strict_types\s*=\s*1\s*);' /dev/null {} +)
violatingFiles=$(grep -vxf <(printf "%s\n" $passingFiles | sort) <(printf "%s\n" $newFiles | sort))
if [[ -n violatingFiles ]]; then
redColoured='\033[0;31m'
printf "${redColoured}Following files are missing 'declare( strict_types = 1)' directive:\n"
printf "${redColoured}%s\n" $violatingFiles
exit 1
fi
fi

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Lint new PHP files for strict types directive