Add code-style tool

This commit is contained in:
Timothy Warren 2022-03-03 10:19:01 -05:00
parent 4df250d908
commit e25aae6597
9 changed files with 2317 additions and 66 deletions

7
.gitignore vendored
View File

@ -9,3 +9,10 @@
/phpunit.xml /phpunit.xml
.phpunit.result.cache .phpunit.result.cache
###< phpunit/phpunit ### ###< phpunit/phpunit ###
###> friendsofphp/php-cs-fixer ###
/.php-cs-fixer.php
/.php-cs-fixer.cache
###< friendsofphp/php-cs-fixer ###
tools/php-cs-fixer/vendor/

22
.php-cs-fixer.dist.php Normal file
View File

@ -0,0 +1,22 @@
<?php declare(strict_types=1);
use CodeIgniter\CodingStandard\CodeIgniter4;
use Nexus\CsConfig\Factory;
use PhpCsFixer\Finder;
$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
->exclude('var');
$overrides = [
'blank_line_after_opening_tag' => false,
'linebreak_after_opening_tag' => false,
'declare_strict_types' => true,
'elseif' => false,
];
$options = [
'finder' => $finder,
];
return Factory::create(new CodeIgniter4(), $overrides, $options)->forProjects();

19
justfile Normal file
View File

@ -0,0 +1,19 @@
# Lists the available actions
default:
@just --list
# Runs rector, showing what changes will be make
rector-dry-run:
vendor/bin/rector process --config=tools/rector.php --dry-run src tools
# Runs rector, and updates the files
rector:
vendor/bin/rector process --config=tools/rector.php src tools
# Check code formatting
check-fmt:
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --verbose
# Fix code formatting
fmt:
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --verbose

View File

@ -1,66 +0,0 @@
<?php
declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Symfony\Set\SymfonyLevelSetList;
use Rector\Symfony\Set\SymfonySetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::AUTO_IMPORT_NAMES, TRUE);
$parameters->set(Option::IMPORT_SHORT_CLASSES, TRUE);
$parameters->set(Option::SKIP, [
Rector\Php81\Rector\Property\ReadOnlyPropertyRector::class,
]);
$containerConfigurator->import(DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES);
$containerConfigurator->import(SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES);
$containerConfigurator->import(LevelSetList::UP_TO_PHP_81);
$containerConfigurator->import(SymfonyLevelSetList::UP_TO_SYMFONY_60);
// get services (needed for register a single rule)
$services = $containerConfigurator->services();
foreach ([
Rector\CodeQuality\Rector\BooleanNot\SimplifyDeMorganBinaryRector::class,
Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector::class,
Rector\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector::class,
Rector\CodeQuality\Rector\For_\ForToForeachRector::class,
Rector\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class,
Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector::class,
Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector::class,
Rector\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector::class,
Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector::class,
Rector\CodeQuality\Rector\Ternary\SwitchNegatedTernaryRector::class,
Rector\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector::class,
Rector\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector::class,
Rector\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector::class,
Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector::class,
Rector\CodingStyle\Rector\FuncCall\CallUserFuncArrayToVariadicRector::class,
Rector\CodingStyle\Rector\FuncCall\CallUserFuncToMethodCallRector::class,
Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector::class,
Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector::class,
Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector::class,
Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector::class,
Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector::class,
Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector::class,
Rector\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector::class,
Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector::class,
Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector::class,
Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector::class,
Rector\Restoration\Rector\Property\MakeTypedPropertyNullableIfCheckedRector::class,
Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector::class,
Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector::class,
Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector::class,
Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector::class,
Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByParentCallTypeRector::class,
Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector::class,
Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector::class,
] as $rule) {
$services->set($rule);
}
};

8
tools/common.inc.php Normal file
View File

@ -0,0 +1,8 @@
<?php declare(strict_types=1);
function walk_array(callable $method, array $items): void
{
foreach ($items as $item) {
$method($item);
}
}

19
tools/ecs.php Normal file
View File

@ -0,0 +1,19 @@
<?php declare(strict_types=1);
use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer;
use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
require_once __DIR__ . '/common.inc.php';
return static function (ContainerConfigurator $config): void {
$parameters = $config->parameters();
$services = $config->services();
walk_array([$services, 'set'], [
DeclareStrictTypesFixer::class,
]);
$parameters->set(Option::PARALLEL, true);
$parameters->set(Option::PATHS, [__DIR__ . '/../src', __DIR__]);
};

View File

@ -0,0 +1,6 @@
{
"require": {
"codeigniter/coding-standard": "^1.4",
"friendsofphp/php-cs-fixer": "^3.6"
}
}

2137
tools/php-cs-fixer/composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

99
tools/rector.php Normal file
View File

@ -0,0 +1,99 @@
<?php declare(strict_types=1);
use Rector\CodeQuality\Rector\BooleanNot\SimplifyDeMorganBinaryRector;
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
use Rector\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector;
use Rector\CodeQuality\Rector\For_\ForToForeachRector;
use Rector\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
use Rector\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector;
use Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector;
use Rector\CodeQuality\Rector\Ternary\SwitchNegatedTernaryRector;
use Rector\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector;
use Rector\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector;
use Rector\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector;
use Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector;
use Rector\CodingStyle\Rector\FuncCall\CallUserFuncArrayToVariadicRector;
use Rector\CodingStyle\Rector\FuncCall\CallUserFuncToMethodCallRector;
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
use Rector\Core\Configuration\Option;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
use Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector;
use Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector;
use Rector\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;
use Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Restoration\Rector\Property\MakeTypedPropertyNullableIfCheckedRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Symfony\Set\SymfonyLevelSetList;
use Rector\Symfony\Set\SymfonySetList;
use Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByParentCallTypeRector;
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
require_once __DIR__ . '/common.inc.php';
return static function (ContainerConfigurator $config): void {
$parameters = $config->parameters();
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
$parameters->set(Option::IMPORT_SHORT_CLASSES, true);
$parameters->set(Option::SKIP, [
ReadOnlyPropertyRector::class,
]);
walk_array([$config, 'import'], [
DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
LevelSetList::UP_TO_PHP_81,
SymfonyLevelSetList::UP_TO_SYMFONY_60,
SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
]);
$services = $config->services();
walk_array([$services, 'set'], [
SimplifyDeMorganBinaryRector::class,
CompleteDynamicPropertiesRector::class,
ForRepeatedCountToOwnVariableRector::class,
ForToForeachRector::class,
ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class,
SimplifyIfElseToTernaryRector::class,
SimplifyIfReturnBoolRector::class,
SimplifyDuplicatedTernaryRector::class,
SimplifyTautologyTernaryRector::class,
SwitchNegatedTernaryRector::class,
AddArrayDefaultToArrayPropertyRector::class,
RemoveFinalFromConstRector::class,
NewlineBeforeNewAssignSetRector::class,
WrapEncapsedVariableInCurlyBracesRector::class,
CallUserFuncArrayToVariadicRector::class,
CallUserFuncToMethodCallRector::class,
CountArrayToEmptyArrayComparisonRector::class,
NewlineAfterStatementRector::class,
RemoveUselessParamTagRector::class,
RemoveUselessReturnTagRector::class,
RemoveUnusedForeachKeyRector::class,
RemoveUselessVarTagRector::class,
RemoveDuplicatedCaseInSwitchRector::class,
ChangeNestedForeachIfsToEarlyContinueRector::class,
ChangeIfElseValueAssignToEarlyReturnRector::class,
RemoveAlwaysElseRector::class,
MakeTypedPropertyNullableIfCheckedRector::class,
AddArrayParamDocTypeRector::class,
AddArrayReturnDocTypeRector::class,
AddMethodCallBasedStrictParamTypeRector::class,
ParamTypeByMethodCallTypeRector::class,
ParamTypeByParentCallTypeRector::class,
AddClosureReturnTypeRector::class,
TypedPropertyFromAssignsRector::class,
]);
};