From e3413b21eebf1e440674e5f01a16a08ddd82b286 Mon Sep 17 00:00:00 2001 From: Alex Williams Date: Tue, 30 May 2023 10:49:41 +0100 Subject: [PATCH 1/4] WIP --- composer.json | 5 ++- rector.php | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index a70c535..6cf500c 100644 --- a/composer.json +++ b/composer.json @@ -27,9 +27,10 @@ ], "require": { "php": "^8.0", + "nikic/php-parser": "^4.0", + "rector/rector": "^0.16.0", "slevomat/coding-standard": "^8.0", - "symplify/easy-coding-standard": "^10.0.6", - "nikic/php-parser": "^4.0" + "symplify/easy-coding-standard": "^10.0.6" }, "autoload": { "psr-4": { diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..58c9e0e --- /dev/null +++ b/rector.php @@ -0,0 +1,114 @@ +paths([ + __DIR__ . '/app', + __DIR__ . '/tests', + ]); + + //Code Quality + $rectorConfig->rule(CombineIfRector::class); + $rectorConfig->rule(ExplicitBoolCompareRector::class); + $rectorConfig->rule(FlipTypeControlToUseExclusiveTypeRector::class); + $rectorConfig->rule(ReturnTypeFromStrictScalarReturnExprRector::class); + $rectorConfig->rule(SimplifyEmptyCheckOnEmptyArrayRector::class); + $rectorConfig->rule(SimplifyUselessVariableRector::class); + $rectorConfig->rule(ShortenElseIfRector::class); + $rectorConfig->rule(StrvalToTypeCastRector::class); + + //Dead Code + $rectorConfig->rule(RemoveUnreachableStatementRector::class); + $rectorConfig->rule(RemoveUnusedPrivatePropertyRector::class); + $rectorConfig->rule(RemoveUselessReturnTagRector::class); + + //Early Return + $rectorConfig->rule(ChangeAndIfToEarlyReturnRector::class); + $rectorConfig->rule(RemoveAlwaysElseRector::class); + $rectorConfig->rule(ReturnEarlyIfVariableRector::class); + + //Privatisation + $rectorConfig->rule(ChangeReadOnlyPropertyWithDefaultValueToConstantRector::class); + $rectorConfig->rule(FinalizeClassesWithoutChildrenRector::class); + $rectorConfig->rule(PrivatizeFinalClassMethodRector::class); + $rectorConfig->rule(PrivatizeFinalClassPropertyRector::class); + + //Type Declarations + $rectorConfig->rule(AddClosureReturnTypeRector::class); + $rectorConfig->rule(AddParamTypeSplFixedArrayRector::class); + $rectorConfig->rule(AddVoidReturnTypeWhereNoReturnRector::class); + $rectorConfig->rule(BinaryOpNullableToInstanceofRector::class); + $rectorConfig->rule(ReturnNeverTypeRector::class); + $rectorConfig->rule(ReturnTypeFromStrictTypedCallRector::class); + $rectorConfig->rule(ParamTypeByMethodCallTypeRector::class); + $rectorConfig->rule(TypedPropertyFromStrictConstructorRector::class); + + //PHP 7.1 + if (PHP_VERSION_ID >= 71000) { + $rectorConfig->rule(CountOnNullRector::class); + } + + //PHP 7.3 + if (PHP_VERSION_ID >= 73000) { + $rectorConfig->rule(JsonThrowOnErrorRector::class); + } + + //PHP 8.0 + if (PHP_VERSION_ID >= 80000) { + $rectorConfig->rule(ClassOnObjectRector::class); + $rectorConfig->rule(ClassOnThisVariableObjectRector::class); + $rectorConfig->rule(ClassPropertyAssignToConstructorPromotionRector::class); + $rectorConfig->rule(MixedTypeRector::class); + $rectorConfig->rule(RemoveUnusedVariableInCatchRector::class); + $rectorConfig->rule(StrContainsRector::class); + $rectorConfig->rule(StrStartsWithRector::class); + $rectorConfig->rule(UnionTypesRector::class); + } + + //PHP 8.1 + if (PHP_VERSION_ID >= 81000) { + $rectorConfig->rule(NullToStrictStringFuncCallArgRector::class); + $rectorConfig->rule(NewInInitializerRector::class); + } +}; From df01f57c26ecff485d4c4afed51020fb6a83ae87 Mon Sep 17 00:00:00 2001 From: Alex Williams Date: Tue, 30 May 2023 11:20:08 +0100 Subject: [PATCH 2/4] Readme change --- README.md | 48 +++++++++++++++++++++++++++++++++++++++++------- rector.php | 2 +- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1bec819..348fc9f 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,10 @@ At [Jump24](https://jump24.co.uk/) we pride ourselves on keeping our coding stan To install this package, simply use composer: ```bash -composer require jumptwentyfour/php-coding-standards +composer require jumptwentyfour/php-coding-standards --dev ``` -## Setup -======= -Then run the following commands:- - +## PHPStan Once installed you will have access to our PHPStan configuration file, which you can easily add to your `phpstan.neon`: ```neon @@ -21,7 +18,9 @@ includes: - ./vendor/jumptwentyfour/php-coding-standards/phpstan.neon ``` -## Running +## Easy Coding Standard (ECS) + +### Running ECS To run the code standard checks, simply run the following command: @@ -30,7 +29,7 @@ To run the code standard checks, simply run the following command: ``` This will run the configured code standard checks for you, giving you feedback on where your code is and what improvements you need to implement -## Extending +### Extending ECS These code standards are extendable, all you need to do is create your own `ecs.php` in the root directory of your project: @@ -67,3 +66,38 @@ return static function (ECSConfig $ecsConfig): void { ])); }; ``` +## Rector + +### Running Rector + +#### Dry Run + +```bash +vendor/bin/rector process app --dry-run +``` + +#### Normal Run + +```bash +vendor/bin/rector process app +``` + +### Extending Rector + +These rector rules are extendable, all you need to do is create your own `rector.php` in the root directory of your project: + +```php +import(__DIR__ . '/vendor/jumptwentyfour/php-coding-standards/rector.php'); + + $rectorConfig->skip([ + PrivatizeFinalClassPropertyRector::class, + ]); +}; +``` \ No newline at end of file diff --git a/rector.php b/rector.php index 58c9e0e..ba715dd 100644 --- a/rector.php +++ b/rector.php @@ -108,7 +108,7 @@ //PHP 8.1 if (PHP_VERSION_ID >= 81000) { - $rectorConfig->rule(NullToStrictStringFuncCallArgRector::class); $rectorConfig->rule(NewInInitializerRector::class); + $rectorConfig->rule(NullToStrictStringFuncCallArgRector::class); } }; From 2800a54fd2bad35827cb4f1768f70f447ea59a26 Mon Sep 17 00:00:00 2001 From: Alex Williams Date: Tue, 30 May 2023 11:40:47 +0100 Subject: [PATCH 3/4] Changes --- rector.php | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/rector.php b/rector.php index ba715dd..efc4ba8 100644 --- a/rector.php +++ b/rector.php @@ -13,26 +13,23 @@ use Rector\CodeQuality\Rector\If_\ShortenElseIfRector; use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector; use Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector; +use Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector; +use Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector; use Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector; use Rector\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector; +use Rector\EarlyReturn\Rector\If_\ChangeNestedIfsToEarlyReturnRector; use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector; +use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector; use Rector\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector; use Rector\Php71\Rector\FuncCall\CountOnNullRector; +use Rector\Php71\Rector\TryCatch\MultiExceptionCatchRector; use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector; -use Rector\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector; -use Rector\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector; -use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector; -use Rector\Php80\Rector\FuncCall\ClassOnObjectRector; -use Rector\Php80\Rector\FunctionLike\MixedTypeRector; -use Rector\Php80\Rector\FunctionLike\UnionTypesRector; -use Rector\Php80\Rector\Identical\StrStartsWithRector; -use Rector\Php80\Rector\NotIdentical\StrContainsRector; -use Rector\Php81\Rector\ClassMethod\NewInInitializerRector; -use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector; +use Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector; use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector; use Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector; use Rector\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector; use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector; +use Rector\Set\ValueObject\SetList; use Rector\TypeDeclaration\Rector\BooleanAnd\BinaryOpNullableToInstanceofRector; use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector; use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector; @@ -61,10 +58,14 @@ //Dead Code $rectorConfig->rule(RemoveUnreachableStatementRector::class); $rectorConfig->rule(RemoveUnusedPrivatePropertyRector::class); + $rectorConfig->rule(RemoveUselessParamTagRector::class); $rectorConfig->rule(RemoveUselessReturnTagRector::class); + $rectorConfig->rule(RemoveUselessVarTagRector::class); //Early Return $rectorConfig->rule(ChangeAndIfToEarlyReturnRector::class); + $rectorConfig->rule(ChangeNestedIfsToEarlyReturnRector::class); + $rectorConfig->rule(PreparedValueToEarlyReturnRector::class); $rectorConfig->rule(RemoveAlwaysElseRector::class); $rectorConfig->rule(ReturnEarlyIfVariableRector::class); @@ -87,6 +88,7 @@ //PHP 7.1 if (PHP_VERSION_ID >= 71000) { $rectorConfig->rule(CountOnNullRector::class); + $rectorConfig->rule(MultiExceptionCatchRector::class); } //PHP 7.3 @@ -94,21 +96,23 @@ $rectorConfig->rule(JsonThrowOnErrorRector::class); } + //PHP 7.4 + if (PHP_VERSION_ID >= 74000) { + $rectorConfig->rule(RestoreDefaultNullToNullableTypePropertyRector::class); + } + //PHP 8.0 if (PHP_VERSION_ID >= 80000) { - $rectorConfig->rule(ClassOnObjectRector::class); - $rectorConfig->rule(ClassOnThisVariableObjectRector::class); - $rectorConfig->rule(ClassPropertyAssignToConstructorPromotionRector::class); - $rectorConfig->rule(MixedTypeRector::class); - $rectorConfig->rule(RemoveUnusedVariableInCatchRector::class); - $rectorConfig->rule(StrContainsRector::class); - $rectorConfig->rule(StrStartsWithRector::class); - $rectorConfig->rule(UnionTypesRector::class); + $rectorConfig->sets([SetList::PHP_80]); } //PHP 8.1 if (PHP_VERSION_ID >= 81000) { - $rectorConfig->rule(NewInInitializerRector::class); - $rectorConfig->rule(NullToStrictStringFuncCallArgRector::class); + $rectorConfig->sets([SetList::PHP_81]); + } + + //PHP 8.2 + if (PHP_VERSION_ID >= 82000) { + $rectorConfig->sets([SetList::PHP_82]); } }; From dd2b7fa3a18d14c1513acce25bbcc4d844ac80af Mon Sep 17 00:00:00 2001 From: Alex Williams Date: Tue, 30 May 2023 13:05:31 +0100 Subject: [PATCH 4/4] Fix --- rector.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/rector.php b/rector.php index efc4ba8..0aae360 100644 --- a/rector.php +++ b/rector.php @@ -101,18 +101,22 @@ $rectorConfig->rule(RestoreDefaultNullToNullableTypePropertyRector::class); } + $sets = []; + //PHP 8.0 if (PHP_VERSION_ID >= 80000) { - $rectorConfig->sets([SetList::PHP_80]); + $sets[] = SetList::PHP_80; } //PHP 8.1 if (PHP_VERSION_ID >= 81000) { - $rectorConfig->sets([SetList::PHP_81]); + $sets[] = SetList::PHP_81; } //PHP 8.2 if (PHP_VERSION_ID >= 82000) { - $rectorConfig->sets([SetList::PHP_82]); + $sets[] = SetList::PHP_82; } + + $rectorConfig->sets($sets); };