juliangut / easy-coding-standard-config
easy-coding-standard configuration
Fund package maintenance!
juliangut
Installs: 5 329
Dependents: 7
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 2
Requires
- php: ^8.0
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- friendsofphp/php-cs-fixer: ^3.61.1
- juliangut/php-codesniffer-custom-sniffs: ^1.2
- juliangut/php-cs-fixer-custom-fixers: ^1.1.1
- kubawerlos/php-cs-fixer-custom-fixers: ^3.22
- lcobucci/clock: ^2.2|^3.0
- nikic/php-parser: ^4.19|^5.2
- pedrotroller/php-cs-custom-fixer: ^2.33
- slevomat/coding-standard: ^8.15
- squizlabs/php_codesniffer: ^3.10
- symplify/coding-standard: ^11.1.10|^12.0
- symplify/easy-coding-standard: ^11.2.10|^12.2.1
Requires (Dev)
- juliangut/phpstan-config: ^1.2
- overtrue/phplint: ^9.0
- phpcompatibility/php-compatibility: ^9.3
- phpmd/phpmd: ^2.14
- phpstan/phpstan: ^1.11
- povils/phpmnd: ^3.2
- roave/security-advisories: dev-master
README
easy-coding-standard-config
Opinionated as can be configuration defaults for Easy-Coding-Standard
Installation
Composer
composer require --dev juliangut/easy-coding-standard-config
Usage
Create ecs.php
file at your project's root directory
<?php use Jgut\ECS\ConfigSet82; return (new ConfigSet82()) ->setHeader(<<<'HEADER' Easy Coding Standard config. @license BSD-3-Clause @link https://github.com/juliangut/easy-coding-standard-config HEADER) ->configureBuilder() ->withPaths([ __FILE__, __DIR__ . '/src', ]);
Use one of the provided configurations depending on the PHP version you want to support:
Jgut\ECS\ConfigSet83
, PHP >= 8.3Jgut\ECS\ConfigSet82
, PHP >= 8.2Jgut\ECS\ConfigSet81
, PHP >= 8.1Jgut\ECS\ConfigSet80
, PHP >= 8.0
Configurations
Header
Provide a header string, it will be prepended to every file analysed by php-cs-fixer.
The string {{year}}
will be replaced by the current year, and the string {{package}}
will be replaced by your package name
(new ConfigSet82()) ->setHeader(<<<'HEADER' (c) 2021-{{year}} Julián Gutiérrez <juliangut@gmail.com> This file is part of package {{package}} HEADER);
--- Original +++ New <?php +/* + * (c) 2021-2024 Julián Gutiérrez <juliangut@gmail.com> + * + * This file is part of package juliangut/php-cs-fixer-config + */ + declare(strict_types=1); namespace App;
If {{year}}
is preceded by current year it will be combined into a single date
// Assuming current year is 2024 (new ConfigSet82()) ->setHeader(<<<'HEADER' (c) 2023-{{year}} Julián Gutiérrez <juliangut@gmail.com> This file is part of package {{package}} HEADER);
--- Original +++ New <?php +/* + * (c) 2024 Julián Gutiérrez <juliangut@gmail.com> + * + * This file is part of package juliangut/php-cs-fixer-config + */ + declare(strict_types=1); namespace App;
PHPUnit
If you work with PHPUnit
(new ConfigSet82()) ->enablePhpUnitRules();
Doctrine
If you work with Doctrine
(new ConfigSet82()) ->enableDoctrineRules();
Type Inference
If you're in the middle of "type hinting everything", try enabling type inference rules and let php-cs-fixer migrate types from annotations into properties, parameters and return types
Be aware these rules are experimental and will need human supervision after fixing, so you are advised NOT to permanently enable type inference
(new ConfigSet82()) ->enableTypeInferRules();
--- Original +++ New <?php declare(strict_types=1); namespace App; class Foo { - /** - * @var string|null - */ - protected $foo + protected ?string $foo - /** - * @var Bar - */ - protected $bar + protected Bar $bar - /** - * @var bool - */ - protected $baz + protected bool $baz /** * Foo constructor. - * - * @param string|null $foo - * @param Bar $bar - * @param bool $baz */ - public function __construct($foo, $bar, $baz = false) + public function __construct(?string $foo, Bar $bar, bool $baz = false) { $this->foo = $foo; $this->bar = $bar; $this->baz = $baz; } - /** - * @return bool - * - public function isBaz() + public function isBaz(): bool { return $this->baz; } }
Additional rules
If you need to add a few additional rules, this rules can be new or override rules already set, the easiest way is using setAdditionalRules
method
It is preferred to identify fixers by their class name, anyway using fixer names will work as well
(new ConfigSet82()) ->setAdditionalRules([ SingleLineThrowFixer::class => true, ]);
Custom config
If you need more control over applied rules or prefer a cleaner setup, you can easily create your custom fixer config instead of setting additional rules
use Jgut\ECS\ConfigSet82; use PhpCsFixer\Fixer\FunctionNotation\SingleLineThrowFixer; class CustomConfigSet extends ConfigSet82 { protected function getRules(): array { // Return your custom rules, or add/remove rules from parent's getRules() return array_merge( parent::getRules(), [ SingleLineThrowFixer::class => true, ] ); } }
Contributing
Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.
See file CONTRIBUTING.md
License
See file LICENSE included with the source code for a copy of the license terms.