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/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..0aae360 --- /dev/null +++ b/rector.php @@ -0,0 +1,122 @@ +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(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); + + //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); + $rectorConfig->rule(MultiExceptionCatchRector::class); + } + + //PHP 7.3 + if (PHP_VERSION_ID >= 73000) { + $rectorConfig->rule(JsonThrowOnErrorRector::class); + } + + //PHP 7.4 + if (PHP_VERSION_ID >= 74000) { + $rectorConfig->rule(RestoreDefaultNullToNullableTypePropertyRector::class); + } + + $sets = []; + + //PHP 8.0 + if (PHP_VERSION_ID >= 80000) { + $sets[] = SetList::PHP_80; + } + + //PHP 8.1 + if (PHP_VERSION_ID >= 81000) { + $sets[] = SetList::PHP_81; + } + + //PHP 8.2 + if (PHP_VERSION_ID >= 82000) { + $sets[] = SetList::PHP_82; + } + + $rectorConfig->sets($sets); +};