update devhelpers to use ecs instead of phpcs and php-cs-fixer

This commit is contained in:
lubiana 2022-05-13 14:52:56 +02:00 committed by Andre Lubian
parent 99a31e45d0
commit e92869c00c

View file

@ -7,7 +7,7 @@ used only for development they should not be used in a production environment. C
file called "dev-dependencies", everything that is required in this section does not get installen in production. file called "dev-dependencies", everything that is required in this section does not get installen in production.
Let's install our dev-helpers and i will explain them one by one: Let's install our dev-helpers and i will explain them one by one:
`composer require --dev phpstan/phpstan php-cs-fixer/shim symfony/var-dumper squizlabs/php_codesniffer` `composer require --dev phpstan/phpstan symfony/var-dumper slevomat/coding-standard symplify/easy-coding-standard`
#### Static Code Analysis with phpstan #### Static Code Analysis with phpstan
@ -116,57 +116,104 @@ Note: Using configuration file /home/vagrant/app/phpstan.neon.
you can read more about the possible parameters and usage options in the [documentation](https://phpstan.org/user-guide/getting-started) you can read more about the possible parameters and usage options in the [documentation](https://phpstan.org/user-guide/getting-started)
#### PHP-CS-Fixer #### Easy-Coding-Standard
Another great tool is the php-cs-fixer, which just applies a specific style to your code. There are two great tools that help us with applying a consistent coding style to our project as well as check and
automatically fix some other errors and oversights that we might not bother with when writing our code.
when you run `./vendor/bin/php-cs-fixer fix ./` it applies the psr-12 code style to every php file in you current The first one is [PHP Coding Standards Fixer](https://cs.symfony.com/) which can automatically detect violations of
directory. a defined coding standard and fix them. The second tool is [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer)
which basically does the same has in my experience some more Rules available that we can apply to our code.
You can read more about its usage and possible rulesets in the [documentation](https://github.com/FriendsOfPHP/PHP-CS-Fixer#documentation) But we are going to use neither of those tools directly and instead choose the [Easy Coding Standard](https://github.com/symplify/easy-coding-standard)
which allows us to combine rules from both mentioned tools, and also claims to run much faster. You could check out the
personally i like to have a more opiniated version with some rules added to the psr-12 standard and have therefore setup documentation and decide on your own coding standard. Or use the one provided by me, which is base on PSR-12 but adds
a configuration file that i use in all my projects `.php-cs-fixer.php`: some highly opiniated options. First create a file 'ecs.php' and either add your own configuration or copy the my
prepared one:
```php ```php
<?php declare(strict_types=1); <?php declare(strict_types=1);
/*
* This document has been generated with use PhpCsFixer\Fixer\Import\OrderedImportsFixer;
* https://mlocati.github.io/php-cs-fixer-configurator/#version:3.1.0|configurator use PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer;
* you can change this configuration by importing this file. use SlevomatCodingStandard\Sniffs\Classes\ClassConstantVisibilitySniff;
*/ use SlevomatCodingStandard\Sniffs\Namespaces\AlphabeticallySortedUsesSniff;
$config = new PhpCsFixer\Config(); use SlevomatCodingStandard\Sniffs\Namespaces\DisallowGroupUseSniff;
return $config use SlevomatCodingStandard\Sniffs\Namespaces\MultipleUsesPerLineSniff;
->setRiskyAllowed(true) use SlevomatCodingStandard\Sniffs\Namespaces\NamespaceSpacingSniff;
->setRules([ use SlevomatCodingStandard\Sniffs\Namespaces\ReferenceUsedNamesOnlySniff;
'@PSR12:risky' => true, use SlevomatCodingStandard\Sniffs\Namespaces\UseSpacingSniff;
'@PSR12' => true, use SlevomatCodingStandard\Sniffs\TypeHints\DeclareStrictTypesSniff;
'@PHP80Migration' => true, use SlevomatCodingStandard\Sniffs\TypeHints\UnionTypeHintFormatSniff;
'@PHP80Migration:risky' => true, use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
'@PHP81Migration' => true, use Symplify\EasyCodingStandard\ValueObject\Option;
'array_indentation' => true, use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
'include' => true,
'blank_line_after_opening_tag' => false, return static function (ContainerConfigurator $containerConfigurator): void {
'native_constant_invocation' => true, $parameters = $containerConfigurator->parameters();
'new_with_braces' => false, $parameters->set(Option::PATHS, [__DIR__ . '/src', __DIR__ . '/ecs.php']);
'native_function_invocation' => [ $parameters->set(Option::PARALLEL, true);
'include' => ['@all'] $parameters->set(Option::SKIP, [BlankLineAfterOpeningTagFixer::class, OrderedImportsFixer::class]);
],
'no_unused_imports' => true, $containerConfigurator->import(SetList::PSR_12);
'global_namespace_import' => [ $containerConfigurator->import(SetList::STRICT);
'import_classes' => true, $containerConfigurator->import(SetList::ARRAY);
'import_constants' => true, $containerConfigurator->import(SetList::SPACES);
'import_functions' => true, $containerConfigurator->import(SetList::DOCBLOCK);
], $containerConfigurator->import(SetList::CLEAN_CODE);
'ordered_interfaces' => true, $containerConfigurator->import(SetList::COMMON);
]) $containerConfigurator->import(SetList::COMMENTS);
->setFinder( $containerConfigurator->import(SetList::NAMESPACES);
PhpCsFixer\Finder::create() $containerConfigurator->import(SetList::SYMPLIFY);
->in([ $containerConfigurator->import(SetList::CONTROL_STRUCTURES);
__DIR__ . '/src',
]) $services = $containerConfigurator->services();
);
// force visibitily declaration on class constants
$services->set(ClassConstantVisibilitySniff::class)
->property('fixable', true);
// sort all use statements
$services->set(AlphabeticallySortedUsesSniff::class);
$services->set(DisallowGroupUseSniff::class);
$services->set(MultipleUsesPerLineSniff::class);
$services->set(NamespaceSpacingSniff::class);
// import all namespaces, and event php core functions and classes
$services->set(ReferenceUsedNamesOnlySniff::class)
->property('allowFallbackGlobalConstants', false)
->property('allowFallbackGlobalFunctions', false)
->property('allowFullyQualifiedGlobalClasses', false)
->property('allowFullyQualifiedGlobalConstants', false)
->property('allowFullyQualifiedGlobalFunctions', false)
->property('allowFullyQualifiedNameForCollidingClasses', true)
->property('allowFullyQualifiedNameForCollidingConstants', true)
->property('allowFullyQualifiedNameForCollidingFunctions', true)
->property('searchAnnotations', true)
->property('fixable', true);
// define newlines between use statements
$services->set(UseSpacingSniff::class)
->property('linesCountBeforeFirstUse', 1)
->property('linesCountBetweenUseTypes', 1)
->property('linesCountAfterLastUse', 1);
// strict types declaration should be on same line as opening tag
$services->set(DeclareStrictTypesSniff::class)
->property('declareOnFirstLine', true)
->property('spacesCountAroundEqualsSign', 0);
// disallow ?Foo typehint in favor of Foo|null
$services->set(UnionTypeHintFormatSniff::class)
->property('withSpaces', 'no')
->property('shortNullable', 'no')
->property('nullPosition', 'last');
};
``` ```
You can now use `./vendor/bin/ecs` to list all violations of the defined standard and `./vendor/bin/ecs --fix` to
automatically fix them.
#### PHP Codesniffer #### PHP Codesniffer