Skip to content

Commit

Permalink
Add a CS workflow & fix issues (#101)
Browse files Browse the repository at this point in the history
This will prevent coding-standards issues creep-in in future commits
  • Loading branch information
aristath authored Apr 22, 2024
1 parent d605ac4 commit 538cdec
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 156 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/cs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: CS

on:
# Run on all relevant pushes (except to main) and on all relevant pull requests.
push:
paths:
- '**.php'
- 'composer.json'
- 'composer.lock'
- '.phpcs.xml.dist'
- 'phpcs.xml.dist'
- '.github/workflows/cs.yml'
pull_request:
paths:
- '**.php'
- 'composer.json'
- 'composer.lock'
- '.phpcs.xml.dist'
- 'phpcs.xml.dist'
- '.github/workflows/cs.yml'
# Allow manually triggering the workflow.
workflow_dispatch:

# Cancels all previous workflow runs for the same branch that have not yet completed.
concurrency:
# The concurrency group contains the workflow name and the branch name.
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
checkcs:
name: 'Check code style'
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@main

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
coverage: none
tools: cs2pr

# Validate the composer.json file.
# @link https://getcomposer.org/doc/03-cli.md#validate
- name: Validate Composer installation
run: composer validate --no-check-all

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
- name: Install Composer dependencies
uses: ramsey/composer-install@v2
with:
# Bust the cache at least once a month - output format: YYYY-MM.
custom-cache-suffix: $(date -u "+%Y-%m")

# Check the codestyle of the files.
# The results of the CS check will be shown inline in the PR via the CS2PR tool.
# @link https://github.com/staabm/annotate-pull-request-from-checkstyle/
- name: Check PHP code style
id: phpcs
run: composer check-cs -- --no-cache --report-full --report-checkstyle=./phpcs-report.xml

- name: Show PHPCS results in PR
if: ${{ always() && steps.phpcs.outcome == 'failure' }}
run: cs2pr ./phpcs-report.xml
21 changes: 17 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,26 @@
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
"squizlabs/php_codesniffer": "^3.7",
"wp-coding-standards/wpcs": "^3.0",
"phpcompatibility/phpcompatibility-wp": "^2.1",
"yoast/phpunit-polyfills": "^1.0.1"
"wp-coding-standards/wpcs": "^3.1",
"yoast/phpunit-polyfills": "^1.0.1",
"phpcompatibility/phpcompatibility-wp": "*",
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpstan/phpstan": "^1.10",
"szepeviktor/phpstan-wordpress": "^1.3",
"phpstan/extension-installer": "^1.3"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
}
},
"scripts": {
"check-cs": [
"@php ./vendor/bin/phpcs"
],
"fix-cs": [
"@php ./vendor/bin/phpcbf"
]
}
}
172 changes: 147 additions & 25 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<!-- Only scan PHP files. -->
<arg name="extensions" value="php"/>

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

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

<file>.</file>
<!--
#############################################################################
FILE SELECTION
Set which files will be subject to the scans executed using this ruleset.
#############################################################################
-->

<rule ref="WordPress-Core"/>
<rule ref="WordPress-Docs"/>
<file>.</file>

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

<!-- Allow the WP DB Class and related tests for usage of direct database access functions. -->
<rule ref="WordPress.DB.RestrictedClasses.mysql__PDO">
<exclude-pattern>/wp-includes/*.php</exclude-pattern>
<!--
#############################################################################
SET UP THE RULESET
#############################################################################
-->

<rule ref="WordPress-Core"/>


<!--
#############################################################################
SNIFF-SPECIFIC CONFIGURATION
#############################################################################
-->

<!-- These rules are being set as warnings instead of errors, so we can error check the entire codebase. -->
<rule ref="Generic.Files.OneObjectStructurePerFile.MultipleFound">
<type>warning</type>
<!-- Exclude the unit tests as no warnings are allowed there. Note: these issues should be fixed and the exclude removed! -->
<exclude-pattern>/tests/phpunit/*</exclude-pattern>
</rule>
<rule ref="WordPress.DB.RestrictedFunctions">
<exclude-pattern>/wp-includes/*.php</exclude-pattern>
<rule ref="WordPress.DB.PreparedSQL.InterpolatedNotPrepared">
<type>warning</type>
</rule>
<rule ref="WordPress.DB.PreparedSQL.NotPrepared">
<type>warning</type>
</rule>
<rule ref="WordPress.Files.FileName.InvalidClassFileName">
<type>warning</type>
</rule>
<rule ref="WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase">
<type>warning</type>
</rule>

<!-- Disable some more checks. -->
<rule ref="Generic.Commenting.Todo.CommentFound">
<severity>0</severity>
<rule ref="WordPress.NamingConventions.ValidVariableName">
<properties>
<property name="allowed_custom_properties" type="array">
<!-- From database structure queries. -->
<element value="Collation"/>
<element value="Column_name"/>
<element value="Default"/>
<element value="Extra"/>
<element value="Field"/>
<element value="Index_type"/>
<element value="Key"/>
<element value="Key_name"/>
<element value="Msg_text"/>
<element value="Non_unique"/>
<element value="Null"/>
<element value="Sub_part"/>
<element value="Type"/>
<!-- From plugin/theme data. -->
<element value="authorAndUri"/>
<element value="Name"/>
<element value="Version"/>
<!-- From the result of wp_xmlrpc_server::wp_getPageList(). -->
<element value="dateCreated"/>

<!-- From DOMDocument. -->
<element value="childNodes"/>
<element value="firstChild"/>
<element value="formatOutput"/>
<element value="lastChild"/>
<element value="nodeName"/>
<element value="nodeType"/>
<element value="nodeValue"/>
<element value="parentNode"/>
<element value="preserveWhiteSpace"/>
<element value="textContent"/>
<!-- From PHPMailer. -->
<element value="AltBody"/>
<element value="Body"/>
<element value="CharSet"/>
<element value="ContentType"/>
<element value="Encoding"/>
<element value="Hostname"/>
<element value="mailHeader"/>
<element value="MIMEBody"/>
<element value="MIMEHeader"/>
<element value="Sender"/>
<element value="Subject"/>
<!-- From PHPUnit_Util_Getopt. -->
<element value="longOptions"/>
<!-- From POP3. -->
<element value="ERROR"/>
<!-- From ZipArchive. -->
<element value="numFiles"/>
</property>
</properties>
</rule>
<rule ref="Squiz.Commenting.LongConditionClosingComment.Missing">
<severity>0</severity>

<rule ref="WordPress.PHP.NoSilencedErrors">
<properties>
<property name="customAllowedFunctionsList" type="array">
<element value="ssh2_connect"/>
<element value="ssh2_auth_password"/>
<element value="ssh2_auth_pubkey_file"/>
<element value="ftp_ssl_connect"/>
<element value="ftp_connect"/>
<element value="ftp_get_option"/>
<element value="ftp_set_option"/>
<element value="disk_free_space"/>
<element value="getimagesize"/>
<element value="iptcparse"/>
<element value="exif_read_data"/>
<element value="gzinflate"/>
<element value="gzuncompress"/>
<element value="gzdecode"/>
<element value="imagecreatefromwebp"/>
<element value="imagecreatefromavif"/>
</property>
</properties>
</rule>
<rule ref="Squiz.Commenting.PostStatementComment.Found">
<severity>0</severity>


<!--
#############################################################################
SELECTIVE EXCLUSIONS
Exclude specific files for specific sniffs and/or exclude sub-groups in sniffs.
These exclusions are listed ordered by alphabetic sniff name.
#############################################################################
-->

<rule ref="WordPress.DB.RestrictedClasses.mysql__PDO">
<exclude-pattern>/wp-includes/sqlite/class-wp-sqlite-translator.php</exclude-pattern>
</rule>

<!-- Assignments in while conditions are a valid method of looping over iterables. -->
<rule ref="Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition">
<exclude-pattern>*</exclude-pattern>
</rule>

<!-- Disable some tests for the tests/ files. -->
<rule ref="Squiz.Commenting">
<rule ref="WordPress.DB.RestrictedClasses">
<exclude-pattern>/src/wp-includes/sqlite/*\.php</exclude-pattern>
<exclude-pattern>/tests/*</exclude-pattern>
<exclude-pattern>/wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php</exclude-pattern>
</rule>
<rule ref="WordPress.DB.RestrictedClasses.mysql__PDO">

<!-- Exclude the unit tests from the file name rules. -->
<rule ref="WordPress.Files.FileName">
<exclude-pattern>/tests/*</exclude-pattern>
</rule>
<rule ref="WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase">
<exclude-pattern>/wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php</exclude-pattern>
</rule>
<rule ref="Generic.Commenting">
<rule ref="WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid">
<exclude-pattern>/tests/*</exclude-pattern>
</rule>
<rule ref="WordPress.Files.FileName.NotHyphenatedLowercase">
<exclude-pattern>/tests/*</exclude-pattern>
<exclude-pattern>/wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php</exclude-pattern>
</rule>
<rule ref="WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase">
<exclude-pattern>/tests/*</exclude-pattern>
<exclude-pattern>/wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php</exclude-pattern>
</rule>
<rule ref="WordPress.Files.FileName.InvalidClassFileName">
<exclude-pattern>/wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php</exclude-pattern>
Expand Down
Loading

0 comments on commit 538cdec

Please sign in to comment.