lmc / coding-standard
Coding standard used in LMC projects
Installs: 155 672
Dependents: 24
Suggesters: 0
Security: 0
Stars: 14
Watchers: 8
Forks: 7
Open Issues: 4
Requires
- php: ^7.3 || ^8.0
- friendsofphp/php-cs-fixer: ^3.0
- nette/utils: ^3.2
- slevomat/coding-standard: ^6.4.1 || ^7.0
- squizlabs/php_codesniffer: ^3.6
- symplify/easy-coding-standard: ^10.0 <10.2.4
Requires (Dev)
- ergebnis/composer-normalize: ^2.13.2
- php-parallel-lint/php-parallel-lint: ^1.2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.3.0
- phpstan/phpstan-phpunit: ^1.0.0
- phpunit/phpunit: ^9.5.2
README
PHP coding standard used in LMC projects.
Standard is based on PSR-2 and adds various checks to make sure the code is readable, does follow the same conventions and does not contain common mistakes.
We use EasyCodingStandard to define and execute checks created for both PHP-CS-Fixer and PHP_CodeSniffer.
Installation
composer require --dev lmc/coding-standard
Usage
- Create
ecs.php
file in the root directory of your project and import the LMC code-style rules:
<?php declare(strict_types=1); use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $containerConfigurator): void { $containerConfigurator->import(__DIR__ . '/vendor/lmc/coding-standard/ecs.php'); // Be default only checks compatible with PHP 7.3 are enabled. // Depending on the lowest PHP version your project need to support, you can enable additional checks for PHP 7.4, 8.0 or 8.1. // Import one of ecs-7.4.php, ecs-8.0.php or ecs-8.1.php. Use only one file (for the highest possible PHP version). //$containerConfigurator->import(__DIR__ . '/vendor/lmc/coding-standard/ecs-7.4.php'); };
- Run the check command (for
src/
andtests/
directories):
vendor/bin/ecs check src/ tests/
- Optionally we recommend adding this to
scripts
section of yourcomposer.json
:
"scripts": { "analyze": [ "vendor/bin/ecs check src/ tests/ --ansi", "[... other scripts, like PHPStan etc.]" ], "fix": [ "...", "vendor/bin/ecs check ./src/ ./tests/ --ansi --fix" ], }
Now you will be able to run the fix using composer analyze
and execute automatic fixes with composer fix
.
Add custom checks or override default settings
On top of default code-style rules you are free to add any rules from PHP-CS-Fixer or PHP_CodeSniffer. If needed, you can also override some default settings.
Be aware you must add these settings after import of the base LMC code-style:
<?php declare(strict_types=1); use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff; use PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $containerConfigurator): void { $containerConfigurator->import(__DIR__ . '/vendor/lmc/coding-standard/ecs.php'); $services = $containerConfigurator->services(); // Enforce line-length to 120 characters $services->set(LineLengthSniff::class) ->property('absoluteLineLimit', 120); // Tests must have @test annotation $services->set(PhpUnitTestAnnotationFixer::class) ->call('configure', [['style' => 'annotation']]); };
See EasyCodingStandard docs for more configuration options.
Exclude (skip) checks or files
You can configure your ecs.php
to entirely skip some files, disable specific checks of suppress specific errors.
Unlike adding/modifying checks, skips must be added before import of the base LMC code-style.
<?php declare(strict_types=1); use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff; use PHP_CodeSniffer\Standards\Squiz\Sniffs\Arrays\ArrayDeclarationSniff; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symplify\EasyCodingStandard\ValueObject\Option; return static function (ContainerConfigurator $containerConfigurator): void { $parameters = $containerConfigurator->parameters(); $parameters->set( Option::SKIP, [ // Ignore specific check only in specific files ForbiddenFunctionsSniff::class => [__DIR__ . '/src-tests/bootstrap.php'], // Disable check entirely ArrayDeclarationSniff::class, // Skip one file __DIR__ . '/file/to/be/skipped.php', // Skip entire directory __DIR__ . '/ignored/directory/*', ] ); $containerConfigurator->import(__DIR__ . '/vendor/lmc/coding-standard/ecs.php'); };
See EasyCodingStandard docs for more configuration options.
IDE integration
For integration with PHPStorm etc. follow instructions in EasyCodingStandard README.
Changelog
For latest changes see CHANGELOG.md file. We follow Semantic Versioning.
License
This library is open source software licensed under the MIT license.