ninjify/coding-standard
PHP_CodeSniffer coding standard for PHP projects. Install via Composer and include the provided ruleset.xml or Contributte profile. Supports excluding paths and ships PhpStorm code style presets. Maintained by Contributte.
composer require --dev ninjify/coding-standard
ruleset.xml in your project root:
<?xml version="1.0"?>
<ruleset name="YourProject">
<rule ref="./vendor/ninjify/coding-standard/ruleset.xml"/>
<!-- Exclude test folders -->
<exclude-pattern>tests/*</exclude-pattern>
</ruleset>
vendor/bin/phpcs --standard=ruleset.xml src/
phpcs to block non-compliant code:
composer require --dev php-parallel-lint/php-parallel-lint
Add to .git/hooks/pre-commit:
#!/bin/sh
vendor/bin/phpcs --standard=ruleset.xml --report=emacs src/ || exit 1
Team Enforcement:
contributte.xml for Contributte/Apitte/Nettrine projects:
<rule ref="./vendor/ninjify/coding-standard/contributte.xml"/>
- name: PHP Coding Standards
run: vendor/bin/phpcs --standard=ruleset.xml --warning-severity=0 src/
IDE Integration:
phpstorm/ rules into PhpStorm:
File > Settings > Editor > Code Style > PHPImport Scheme > IntelliJ IDEA > Select phpstorm/coding-standard.xmlCustom Rulesets:
ruleset.xml to override defaults:
<config name="line_ending" value="LF"/>
<rule ref="ruleset.xml">
<exclude name="SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint"/>
</rule>
php-parallel-lint:
vendor/bin/parallel-lint --no-cache src/ | vendor/bin/phpcs --standard=ruleset.xml --report=diff -
phpcbf for auto-fixes (where possible):
vendor/bin/phpcbf --standard=ruleset.xml src/
phpcs in workflows to gate PRs:
- name: Run PHPCS
if: github.event_name == 'pull_request'
run: vendor/bin/phpcs --standard=ruleset.xml --warning-severity=0 src/ || exit 1
Strict Type Declarations:
declare(strict_types=1); on first line of files (v0.12+).composer.json scripts:
"scripts": {
"post-autoload-dump": "php -r \"file_put_contents('src/index.php', '<?php declare(strict_types=1);\\n');\""
}
Exclusion Patterns:
*) in <exclude-pattern> may not work as expected. Use absolute paths:
<exclude-pattern>./tests/tmp/</exclude-pattern>
PHP Version Mismatch:
v0.9 for PHP 7.2/7.3:
composer require ninjify/coding-standard:^0.9
Ternary Operator:
return $a ? true : false;).return $a; or use <exclude> in ruleset.xml.-v to debug rule failures:
vendor/bin/phpcs -v --standard=ruleset.xml src/
vendor/bin/phpcs --standard=ruleset.xml --report=full src/ | grep "ERROR"
Custom Rules:
ruleset.xml:
<rule ref="vendor/slevomat/coding-standard/Sniffs/Arrays/ArrayPushSniff.php"/>
PHPCS Configuration:
ruleset.xml:
<config name="tab_width" value="4"/>
<config name="encoding" value="utf-8"/>
PHPStorm Integration:
phpstorm/ folder:
phpstorm/coding-standard.xml to ~/.PhpStorm2023.1/config/codestyles/.Settings > Editor > Code Style.vendor/ and .php_cs.cache/ in CI to speed up runs:
- uses: actions/cache@v3
with:
path: |
vendor/
.php_cs.cache/
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
<exclude> for legacy code:
<exclude name="SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint">
<file>src/legacy/</file>
</exclude>
How can I help you explore Laravel packages today?