Skip to content

Commit 538cdec

Browse files
authoredApr 22, 2024··
Add a CS workflow & fix issues (#101)
This will prevent coding-standards issues creep-in in future commits
1 parent d605ac4 commit 538cdec

File tree

6 files changed

+362
-156
lines changed

6 files changed

+362
-156
lines changed
 

‎.github/workflows/cs.yml

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: CS
2+
3+
on:
4+
# Run on all relevant pushes (except to main) and on all relevant pull requests.
5+
push:
6+
paths:
7+
- '**.php'
8+
- 'composer.json'
9+
- 'composer.lock'
10+
- '.phpcs.xml.dist'
11+
- 'phpcs.xml.dist'
12+
- '.github/workflows/cs.yml'
13+
pull_request:
14+
paths:
15+
- '**.php'
16+
- 'composer.json'
17+
- 'composer.lock'
18+
- '.phpcs.xml.dist'
19+
- 'phpcs.xml.dist'
20+
- '.github/workflows/cs.yml'
21+
# Allow manually triggering the workflow.
22+
workflow_dispatch:
23+
24+
# Cancels all previous workflow runs for the same branch that have not yet completed.
25+
concurrency:
26+
# The concurrency group contains the workflow name and the branch name.
27+
group: ${{ github.workflow }}-${{ github.ref }}
28+
cancel-in-progress: true
29+
30+
jobs:
31+
checkcs:
32+
name: 'Check code style'
33+
runs-on: ubuntu-latest
34+
35+
steps:
36+
- name: Checkout code
37+
uses: actions/checkout@main
38+
39+
- name: Install PHP
40+
uses: shivammathur/setup-php@v2
41+
with:
42+
php-version: '7.4'
43+
coverage: none
44+
tools: cs2pr
45+
46+
# Validate the composer.json file.
47+
# @link https://getcomposer.org/doc/03-cli.md#validate
48+
- name: Validate Composer installation
49+
run: composer validate --no-check-all
50+
51+
# Install dependencies and handle caching in one go.
52+
# @link https://github.com/marketplace/actions/install-composer-dependencies
53+
- name: Install Composer dependencies
54+
uses: ramsey/composer-install@v2
55+
with:
56+
# Bust the cache at least once a month - output format: YYYY-MM.
57+
custom-cache-suffix: $(date -u "+%Y-%m")
58+
59+
# Check the codestyle of the files.
60+
# The results of the CS check will be shown inline in the PR via the CS2PR tool.
61+
# @link https://github.com/staabm/annotate-pull-request-from-checkstyle/
62+
- name: Check PHP code style
63+
id: phpcs
64+
run: composer check-cs -- --no-cache --report-full --report-checkstyle=./phpcs-report.xml
65+
66+
- name: Show PHPCS results in PR
67+
if: ${{ always() && steps.phpcs.outcome == 'failure' }}
68+
run: cs2pr ./phpcs-report.xml

‎composer.json

+17-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,26 @@
1313
"require-dev": {
1414
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
1515
"squizlabs/php_codesniffer": "^3.7",
16-
"wp-coding-standards/wpcs": "^3.0",
17-
"phpcompatibility/phpcompatibility-wp": "^2.1",
18-
"yoast/phpunit-polyfills": "^1.0.1"
16+
"wp-coding-standards/wpcs": "^3.1",
17+
"yoast/phpunit-polyfills": "^1.0.1",
18+
"phpcompatibility/phpcompatibility-wp": "*",
19+
"php-parallel-lint/php-parallel-lint": "^1.3",
20+
"phpstan/phpstan": "^1.10",
21+
"szepeviktor/phpstan-wordpress": "^1.3",
22+
"phpstan/extension-installer": "^1.3"
1923
},
2024
"config": {
2125
"allow-plugins": {
22-
"dealerdirect/phpcodesniffer-composer-installer": true
26+
"dealerdirect/phpcodesniffer-composer-installer": true,
27+
"phpstan/extension-installer": true
2328
}
29+
},
30+
"scripts": {
31+
"check-cs": [
32+
"@php ./vendor/bin/phpcs"
33+
],
34+
"fix-cs": [
35+
"@php ./vendor/bin/phpcbf"
36+
]
2437
}
2538
}

‎phpcs.xml.dist

+147-25
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
<!-- Only scan PHP files. -->
66
<arg name="extensions" value="php"/>
77

8-
<!-- Set the memory limit to 256M.
8+
<!-- Set the memory limit to 512M.
99
For most standard PHP configurations, this means the memory limit will temporarily be raised.
1010
Ref: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#specifying-phpini-settings
1111
-->
12-
<ini name="memory_limit" value="256M"/>
12+
<ini name="memory_limit" value="512M"/>
1313

1414
<!-- Strip the filepaths down to the relevant bit. -->
1515
<arg name="basepath" value="./"/>
@@ -20,50 +20,172 @@
2020
<!-- Show sniff codes in all reports. -->
2121
<arg value="ps"/>
2222

23-
<file>.</file>
23+
<!--
24+
#############################################################################
25+
FILE SELECTION
26+
Set which files will be subject to the scans executed using this ruleset.
27+
#############################################################################
28+
-->
2429

25-
<rule ref="WordPress-Core"/>
26-
<rule ref="WordPress-Docs"/>
30+
<file>.</file>
2731

2832
<!-- Directories and third party library exclusions. -->
2933
<exclude-pattern>/vendor/*</exclude-pattern>
30-
<!-- <exclude-pattern>/tests/*</exclude-pattern> -->
34+
<exclude-pattern>/wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php</exclude-pattern>
3135

32-
<!-- Allow the WP DB Class and related tests for usage of direct database access functions. -->
33-
<rule ref="WordPress.DB.RestrictedClasses.mysql__PDO">
34-
<exclude-pattern>/wp-includes/*.php</exclude-pattern>
36+
<!--
37+
#############################################################################
38+
SET UP THE RULESET
39+
#############################################################################
40+
-->
41+
42+
<rule ref="WordPress-Core"/>
43+
44+
45+
<!--
46+
#############################################################################
47+
SNIFF-SPECIFIC CONFIGURATION
48+
#############################################################################
49+
-->
50+
51+
<!-- These rules are being set as warnings instead of errors, so we can error check the entire codebase. -->
52+
<rule ref="Generic.Files.OneObjectStructurePerFile.MultipleFound">
53+
<type>warning</type>
54+
<!-- Exclude the unit tests as no warnings are allowed there. Note: these issues should be fixed and the exclude removed! -->
55+
<exclude-pattern>/tests/phpunit/*</exclude-pattern>
3556
</rule>
36-
<rule ref="WordPress.DB.RestrictedFunctions">
37-
<exclude-pattern>/wp-includes/*.php</exclude-pattern>
57+
<rule ref="WordPress.DB.PreparedSQL.InterpolatedNotPrepared">
58+
<type>warning</type>
59+
</rule>
60+
<rule ref="WordPress.DB.PreparedSQL.NotPrepared">
61+
<type>warning</type>
62+
</rule>
63+
<rule ref="WordPress.Files.FileName.InvalidClassFileName">
64+
<type>warning</type>
65+
</rule>
66+
<rule ref="WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase">
67+
<type>warning</type>
3868
</rule>
3969

40-
<!-- Disable some more checks. -->
41-
<rule ref="Generic.Commenting.Todo.CommentFound">
42-
<severity>0</severity>
70+
<rule ref="WordPress.NamingConventions.ValidVariableName">
71+
<properties>
72+
<property name="allowed_custom_properties" type="array">
73+
<!-- From database structure queries. -->
74+
<element value="Collation"/>
75+
<element value="Column_name"/>
76+
<element value="Default"/>
77+
<element value="Extra"/>
78+
<element value="Field"/>
79+
<element value="Index_type"/>
80+
<element value="Key"/>
81+
<element value="Key_name"/>
82+
<element value="Msg_text"/>
83+
<element value="Non_unique"/>
84+
<element value="Null"/>
85+
<element value="Sub_part"/>
86+
<element value="Type"/>
87+
<!-- From plugin/theme data. -->
88+
<element value="authorAndUri"/>
89+
<element value="Name"/>
90+
<element value="Version"/>
91+
<!-- From the result of wp_xmlrpc_server::wp_getPageList(). -->
92+
<element value="dateCreated"/>
93+
94+
<!-- From DOMDocument. -->
95+
<element value="childNodes"/>
96+
<element value="firstChild"/>
97+
<element value="formatOutput"/>
98+
<element value="lastChild"/>
99+
<element value="nodeName"/>
100+
<element value="nodeType"/>
101+
<element value="nodeValue"/>
102+
<element value="parentNode"/>
103+
<element value="preserveWhiteSpace"/>
104+
<element value="textContent"/>
105+
<!-- From PHPMailer. -->
106+
<element value="AltBody"/>
107+
<element value="Body"/>
108+
<element value="CharSet"/>
109+
<element value="ContentType"/>
110+
<element value="Encoding"/>
111+
<element value="Hostname"/>
112+
<element value="mailHeader"/>
113+
<element value="MIMEBody"/>
114+
<element value="MIMEHeader"/>
115+
<element value="Sender"/>
116+
<element value="Subject"/>
117+
<!-- From PHPUnit_Util_Getopt. -->
118+
<element value="longOptions"/>
119+
<!-- From POP3. -->
120+
<element value="ERROR"/>
121+
<!-- From ZipArchive. -->
122+
<element value="numFiles"/>
123+
</property>
124+
</properties>
43125
</rule>
44-
<rule ref="Squiz.Commenting.LongConditionClosingComment.Missing">
45-
<severity>0</severity>
126+
127+
<rule ref="WordPress.PHP.NoSilencedErrors">
128+
<properties>
129+
<property name="customAllowedFunctionsList" type="array">
130+
<element value="ssh2_connect"/>
131+
<element value="ssh2_auth_password"/>
132+
<element value="ssh2_auth_pubkey_file"/>
133+
<element value="ftp_ssl_connect"/>
134+
<element value="ftp_connect"/>
135+
<element value="ftp_get_option"/>
136+
<element value="ftp_set_option"/>
137+
<element value="disk_free_space"/>
138+
<element value="getimagesize"/>
139+
<element value="iptcparse"/>
140+
<element value="exif_read_data"/>
141+
<element value="gzinflate"/>
142+
<element value="gzuncompress"/>
143+
<element value="gzdecode"/>
144+
<element value="imagecreatefromwebp"/>
145+
<element value="imagecreatefromavif"/>
146+
</property>
147+
</properties>
46148
</rule>
47-
<rule ref="Squiz.Commenting.PostStatementComment.Found">
48-
<severity>0</severity>
149+
150+
151+
<!--
152+
#############################################################################
153+
SELECTIVE EXCLUSIONS
154+
Exclude specific files for specific sniffs and/or exclude sub-groups in sniffs.
155+
156+
These exclusions are listed ordered by alphabetic sniff name.
157+
#############################################################################
158+
-->
159+
160+
<rule ref="WordPress.DB.RestrictedClasses.mysql__PDO">
161+
<exclude-pattern>/wp-includes/sqlite/class-wp-sqlite-translator.php</exclude-pattern>
162+
</rule>
163+
164+
<!-- Assignments in while conditions are a valid method of looping over iterables. -->
165+
<rule ref="Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition">
166+
<exclude-pattern>*</exclude-pattern>
49167
</rule>
50168

51-
<!-- Disable some tests for the tests/ files. -->
52-
<rule ref="Squiz.Commenting">
169+
<rule ref="WordPress.DB.RestrictedClasses">
170+
<exclude-pattern>/src/wp-includes/sqlite/*\.php</exclude-pattern>
53171
<exclude-pattern>/tests/*</exclude-pattern>
54-
<exclude-pattern>/wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php</exclude-pattern>
55172
</rule>
56-
<rule ref="WordPress.DB.RestrictedClasses.mysql__PDO">
173+
174+
<!-- Exclude the unit tests from the file name rules. -->
175+
<rule ref="WordPress.Files.FileName">
57176
<exclude-pattern>/tests/*</exclude-pattern>
177+
</rule>
178+
<rule ref="WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase">
58179
<exclude-pattern>/wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php</exclude-pattern>
59180
</rule>
60-
<rule ref="Generic.Commenting">
181+
<rule ref="WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid">
182+
<exclude-pattern>/tests/*</exclude-pattern>
183+
</rule>
184+
<rule ref="WordPress.Files.FileName.NotHyphenatedLowercase">
61185
<exclude-pattern>/tests/*</exclude-pattern>
62-
<exclude-pattern>/wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php</exclude-pattern>
63186
</rule>
64187
<rule ref="WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase">
65188
<exclude-pattern>/tests/*</exclude-pattern>
66-
<exclude-pattern>/wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php</exclude-pattern>
67189
</rule>
68190
<rule ref="WordPress.Files.FileName.InvalidClassFileName">
69191
<exclude-pattern>/wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php</exclude-pattern>

0 commit comments

Comments
 (0)
Please sign in to comment.