chez14 / phpcs
PHP Code Style for chez14 Projects
Requires
- squizlabs/php_codesniffer: ^3.13 || ^4.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.47
Suggests
- net.christianto/codestyle-meta: Meta package for chez14 coding standards
Provides
None
Conflicts
None
Replaces
None
README
The code style standard based on PSR12 with additional extras that chez14 expect contributors and maintainer to fill in.
Just to make sure everyone write clean documentation for the project they wanted.
Additional Ruleset:
PEAR.Commenting.FunctionComment\ Make sure to document functions. This will help people that have their IntelliSense enabled on their IDE.PEAR.Commenting.InlineComment\ Disable perl style commenting. To chez14, this looks distracting.Squiz.PHP.DisallowInlineIf\ Bans ternary expressions, both$a ? $b : $cand the short form$a ?: $b. Use an early return,if/elseormatchinstead. The null coalescing operator (??) and the nullsafe operator (?->) are still allowed.
Working with PHP-CS-Fixer (@PER-CS)
Formatting is owned by PHP-CS-Fixer
with the @PER-CS rule set; phpcs with this standard only enforces rules
(documentation, no ternaries, naming, ...). The two tools must never disagree, so
the ruleset excludes the PSR12 sniffs that report code PHP-CS-Fixer produces:
| Excluded sniff | Why |
|---|---|
Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine | PER-CS keeps empty bodies on the declaration line, e.g. promoted constructors ) {}. |
Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore | Same {} empty body. |
PSR12.Operators.OperatorSpacing.NoSpaceBefore / NoSpaceAfter | PER-CS writes multi-catch types without spaces: catch (A\|B $e). |
Run PHP-CS-Fixer first (php-cs-fixer fix), then phpcs. PEAR.Commenting.FunctionComment
is kept as is: it works with promoted constructor parameters, variadics and
generic types such as list<Node>.
Known limitation: PHP_CodeSniffer (3.13 and 4.0) does not understand PHP 8.4 property hooks yet, so the PSR2 property sniffs may report hooked properties.
Supported PHP_CodeSniffer versions: ^3.13 || ^4.0.
Installation
Make sure you have installed CodeSniffer: https://github.com/squizlabs/PHP_CodeSniffer
Add the coding standard to your project.
composer req --dev chez14/phpcs squizlabs/php_codesnifferMake phpcs use this standard:
By making a new phpcs configuration file:
- Add
phpcs.xml<?xml version="1.0"?> <ruleset name="f3-ilgar"> <description></description> <config name="installed_paths" value="../../chez14/phpcs/src/CHEZ14Standards"/> <rule ref="CHEZ14Standards" scope="path"></rule> </ruleset>See what filename is acceptable for phpcs: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-default-configuration-file \ See the documentation: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-Ruleset
- Add
via commandline script
- Add script to
composer.json:{ "script": { "style": "phpcs --standard=./vendor/chez14/phpcs/src/CHEZ14Standards src/" } }See the documentation: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage#specifying-a-coding-standard
- Add script to
Recommended project setup (formatting by PHP-CS-Fixer, rules by phpcs):
composer req --dev friendsofphp/php-cs-fixer chez14/phpcs squizlabs/php_codesnifferphpcs.xml.dist:<?xml version="1.0"?> <ruleset name="my-project" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"> <file>src</file> <file>tests</file> <arg name="colors"/> <arg value="s"/> <rule ref="vendor/chez14/phpcs/src/CHEZ14Standards/ruleset.xml"/> <!-- Optional: tests do not need function docblocks. --> <rule ref="PEAR.Commenting.FunctionComment"> <exclude-pattern>tests/*</exclude-pattern> </rule> </ruleset>.php-cs-fixer.dist.php:<?php declare(strict_types=1); return (new PhpCsFixer\Config()) ->setRiskyAllowed(true) ->setRules(['@PER-CS' => true, 'declare_strict_types' => true]) ->setFinder(PhpCsFixer\Finder::create()->in([__DIR__ . '/src', __DIR__ . '/tests']));composer.jsonscripts:{ "scripts": { "lint": ["php-cs-fixer fix --dry-run --diff", "phpcs"], "lint:fix": "php-cs-fixer fix" } }Add the code sniffer script to the Composer.
{ "script": { "style": "phpcs src/", "style:fix": "phpcbf src/" } }
License
MIT.