-
Notifications
You must be signed in to change notification settings - Fork 20
101 lines (94 loc) · 3.54 KB
/
code-standards.yml
File metadata and controls
101 lines (94 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
name: PHP Code Standards
on:
workflow_call:
inputs:
version:
type: string
default: "^3.0"
config:
type: string
default: ""
path:
type: string
default: "."
rules:
type: string
default: |
{
"@PSR2": true,
"array_syntax": {"syntax":"short"},
"concat_space": {"spacing":"one"},
"new_with_parentheses": true,
"no_unused_imports": true,
"ordered_imports": true,
"return_type_declaration": {"space_before": "none"},
"single_quote": true,
"single_space_around_construct": true,
"cast_spaces": true,
"whitespace_after_comma_in_array": true,
"no_whitespace_in_blank_line": true,
"binary_operator_spaces": {"default": "at_least_single_space"},
"no_extra_blank_lines": true,
"nullable_type_declaration_for_default_null_value": true
}
add-rules:
type: string
default: "{}"
exclude-patterns:
type: string
default: ""
permissions:
contents: read
jobs:
php_code_standards:
runs-on: ubuntu-latest
name: PHP Code Standards
env:
CONFIG: ${{ inputs.config }}
CONFIG_PATH: ${{ inputs.path }}
steps:
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
# install Google Cloud Tools if a config is provided which starts with "GoogleCloudPlatform/php-tools/"
- if: ${{ startsWith(inputs.config, 'GoogleCloudPlatform/php-tools/') }}
name: Install Google Cloud Tools
run: |
BRANCH=${CONFIG#GoogleCloudPlatform/php-tools/}
composer global require google/cloud-tools:dev-${BRANCH#*@} -q
echo "CONFIG=$HOME/.composer/vendor/google/cloud-tools/${BRANCH%@*}" >> $GITHUB_ENV
- name: 'Setup jq'
uses: dcarbone/install-jq-action@v2
- name: Install PHP CS Fixer
run: composer global require friendsofphp/php-cs-fixer:${INPUTS_VERSION}
env:
INPUTS_VERSION: ${{ inputs.version }}
- name: Run PHP CS Fixer
run: |
# set environment variables in script
export RULES=$(echo ${INPUTS_RULES} ${INPUTS_ADD_RULES}|tr -d '\n\t\r '|jq -s '.[0] * .[1]' -crM)
export EXCLUDE_PATTERNS=$(echo ${INPUTS_EXCLUDE_PATTERNS}|tr -d '\n\t\r ')
# use config path only if EXCLUDE_PATTERN is empty
CMD_PATH=$([ "$EXCLUDE_PATTERNS" = "" ] && echo "$CONFIG_PATH" || echo "")
CONFIG_OR_RULES=$([ ! -z "$CONFIG" ] && echo "--config=$CONFIG" || echo --rules=$RULES)
# do not fail if php-cs-fixer fails (so we can print debugging info)
set +e
~/.composer/vendor/bin/php-cs-fixer fix \
$CMD_PATH \
$CONFIG_OR_RULES \
--dry-run --diff
if [ "$?" -ne 0 ]; then
echo "Run this script locally by executing the following command" \
"from the root of your ${{ github.repository }} repo:"
echo ""
echo " composer global require google/cloud-tools"
echo " ~/.composer/vendor/bin/php-tools cs-fixer ${{ github.repository }} --ref ${{ github.head_ref || github.ref_name }}"
echo ""
exit 1
fi
env:
INPUTS_RULES: ${{ inputs.rules }}
INPUTS_ADD_RULES: ${{ inputs.add-rules }}
INPUTS_EXCLUDE_PATTERNS: ${{ inputs.exclude-patterns }}