Code& Coding Standards is a project with rulesets for code style and quality tools to be used in Code& WordPress projects.
It is a mix with PSR-1, PSR-2, PSR-4, PSR-12 and WordPress Coding Standards.
Whenever there's a conflict between PSR and WPCS, we almost always prefer PSR.
Add this repository to the composer.json
for the WP project and add to require-dev
:
# composer.json
"repositories": [
{
"type": "vcs",
"url": "git@github.com:teamcodeand/codeand-wp-coding-standards.git",
"no-api": true
}
],
"require-dev": {
"teamcodeand/codeand-wp-coding-standards": "dev-master"
}
First, create phpcs.xml
on project root:
<?xml version="1.0"?>
<ruleset name="codeandbedrock">
<!-- Check all files under app/ (assuming Bedrock) -->
<file>./web/app/</file>
<!-- Show colors in console -->
<arg value="-colors"/>
<!-- Show progress and sniff codes in all reports; Show progress of the run -->
<arg value="sp"/>
<!-- Scan only PHP files -->
<arg name="extensions" value="php"/>
<!-- Install custom rulesets -->
<config name="installed_paths" value="vendor/teamcodeand/codeand-wp-coding-standards"/>
<!-- TODO: Probably change everything below! -->
<!-- TODO: Exclude specific rules if necessary -->
<!-- Use Code& WP Coding Standards -->
<rule ref="codeand">
<!-- And only inspect custom stuffs -->
<include-pattern>/web/app/plugins/myplugin/*</include-pattern>
<include-pattern>/web/app/themes/childtheme/*</include-pattern>
</rule>
<!-- TODO: Define minimum supported WordPress version -->
<config name="minimum_supported_wp_version" value="5.2"/>
<!-- TODO: Define expected text domains -->
<rule ref="WordPress.WP.I18n">
<properties>
<property name="text_domain" type="array" value="my-plugin,my-theme,woocommerce,sage"/>
</properties>
</rule>
</ruleset>
Either define the project's composer scripts:
# composer.json
{
"scripts": {
"test": [
"phpcs"
],
"fix": [
"phpcbf"
]
}
}
Run the commands:
$ composer test
$ composer fix
Or alternatively, set this up in your IDE to check your code as you're working on it. You'll probably find a guide for how to do that in the WPCS Readme under Using PHPCS and WPCS from within your IDE
Largely based on Itineris WP Coding Standards created by Tang Rufus. Also makes use of some rules from Human Made with a smattering of input from Code&.