I released my own coding standard
Published: – Leave a comment
Shortly before Christmas, I published my own coding standard and already implemented it in the first project. They make sure to not only maintaining a consistent coding style in the projects of Epiphyt, but also enforce using certain (security) features where necessary.
Why an own standard?
There are multiple coding standards out there, the major one being the WordPress Coding Standards. Unfortunately, they often left too much room for an own coding style. To make it more consistent how the code should look like, I’ve integrated certain other coding standards and created my own sniffs (sniffs are single rules for a specific area of code, e. g. only allowing []
as array notation) as well.
Which standards are included?
For the most part, the WordPress Coding Standards apply (with some exceptions). Additionally, to be more strict, the Neutron PHP Standard from Automattic is used. The third part is a large part of the Slevomat Coding Standard, which covers many rules no other standard contains. There are certain exclusions for all of these standards, but if you’ve already used one of the above, it will be very familiar.
Last, but not least, there are certain custom rules, which are only part of the Epiphyt Coding Standard:
- Enforce a proper tab-limited format for PHPDocs
- Enforce a whitespace after every asterisk in PHPDocs
- Enforce
else if
instead ofelseif
- Enforce a blank line before
continue
andreturn
- Enforce identical indentation of blank lines as the line before
- Enforce no empty line after a comment
- Enforce no whitespace before a newline
- Enforce only a single empty line
PHP compatibility
The Epiphyt Coding Standard requires PHP 8.0 as for now. However, you can also use it for projects supporting older PHP versions. The main compatibility issue for PHP 7.4 is enforcing native type hinting. You can disable it by adding this in your ruleset:
<rule ref="EpiphytCodingStandard">
<exclude name="SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing"/>
</rule>
Code language: HTML, XML (xml)
Using the Epiphyt Coding Standard
You can find the coding standards on GitHub:
To use it, clone the repository and run composer install
. Afterwards, add the path to the Epiphyt Coding Standard to your PHPCS configuration, as noted in the project’s readme.
The first project the coding standard applies, is Form Block. You can see its configuration here: https://github.com/epiphyt/form-block/blob/v1.5.1/.phpcs.xml