Skip to content

Commit 97ece0a

Browse files
calebdwpeterfox
andauthored
chore: move orphaned rules into sets (#377)
* docs: add configurable rules to README This adds all the rules that require configuration to the README. * docs: add opinionated rules This adds the more opinionated rules to the README that (in my opinion) should not be included in any sets by default, and could be more dangerous to use. * chore: move orphaned rules into sets --------- Co-authored-by: Peter Fox <[email protected]>
1 parent 38bc7af commit 97ece0a

File tree

7 files changed

+147
-8
lines changed

7 files changed

+147
-8
lines changed

README.md

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,65 @@ return RectorConfig::configure()
8181
| Set | Purpose |
8282
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
8383
| [LaravelSetList::LARAVEL_ARRAYACCESS_TO_METHOD_CALL](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-arrayaccess-to-method-call.php) | Converts uses of things like `$app['config']` to `$app->make('config')`. |
84-
| [LaravelSetList::LARAVEL_ARRAY_STR_FUNCTION_TO_STATIC_CALL](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-array-str-functions-to-static-call.php) | Converts most string and array helpers into Str and Arr Facades' static calls.<br/>https://laravel.com/docs/12.x/facades#facades-vs-helper-functions |
84+
| [LaravelSetList::LARAVEL_ARRAY_STR_FUNCTION_TO_STATIC_CALL](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-array-str-functions-to-static-call.php) | Converts most string and array helpers into Str and Arr Facades' static calls.<br/><https://laravel.com/docs/12.x/facades#facades-vs-helper-functions> |
8585
| [LaravelSetList::LARAVEL_CODE_QUALITY](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-code-quality.php) | Replaces magical call on `$this->app["something"]` to standalone variable with PHPDocs. |
8686
| [LaravelSetList::LARAVEL_COLLECTION](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-collection.php) | Improves the usage of Laravel Collections by using simpler, more efficient, or more readable methods. |
87-
| [LaravelSetList::LARAVEL_CONTAINER_STRING_TO_FULLY_QUALIFIED_NAME](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-container-string-to-fully-qualified-name.php) | Changes the string or class const used for a service container make call.<br/>https://laravel.com/docs/12.x/container#the-make-method |
88-
| [LaravelSetList::LARAVEL_ELOQUENT_MAGIC_METHOD_TO_QUERY_BUILDER](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-eloquent-magic-method-to-query-builder.php) | Transforms magic method calls on Eloquent Models into corresponding Query Builder method calls.<br/>https://laravel.com/docs/12.x/eloquent |
89-
| [LaravelSetList::LARAVEL_FACADE_ALIASES_TO_FULL_NAMES](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-facade-aliases-to-full-names.php) | Replaces Facade aliases with full Facade names.<br/>https://laravel.com/docs/12.x/facades#facade-class-reference |
87+
| [LaravelSetList::LARAVEL_CONTAINER_STRING_TO_FULLY_QUALIFIED_NAME](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-container-string-to-fully-qualified-name.php) | Changes the string or class const used for a service container make call.<br/><https://laravel.com/docs/12.x/container#the-make-method> |
88+
| [LaravelSetList::LARAVEL_ELOQUENT_MAGIC_METHOD_TO_QUERY_BUILDER](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-eloquent-magic-method-to-query-builder.php) | Transforms magic method calls on Eloquent Models into corresponding Query Builder method calls.<br/><https://laravel.com/docs/12.x/eloquent> |
89+
| [LaravelSetList::LARAVEL_FACADE_ALIASES_TO_FULL_NAMES](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-facade-aliases-to-full-names.php) | Replaces Facade aliases with full Facade names.<br/><https://laravel.com/docs/12.x/facades#facade-class-reference> |
9090
| [LaravelSetList::LARAVEL_FACTORIES](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-factories.php) | Makes working with Laravel Factories easier and more IDE friendly. |
91-
| [LaravelSetList::LARAVEL_IF_HELPERS](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-if-helpers.php) | Replaces `abort()`, `report()`, `throw` statements inside conditions with `abort_if()`, `report_if()`, `throw_if()` function calls.<br/>https://laravel.com/docs/12.x/helpers#method-abort-if |
92-
| [LaravelSetList::LARAVEL_LEGACY_FACTORIES_TO_CLASSES](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-legacy-factories-to-classes.php) | Migrates Eloquent legacy model factories (with closures) into class based factories.<br/>https://laravel.com/docs/8.x/releases#model-factory-classes |
93-
| [LaravelSetList::LARAVEL_STATIC_TO_INJECTION](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-static-to-injection.php) | Replaces Laravel's Facades with Dependency Injection.<br/>https://tomasvotruba.com/blog/2019/03/04/how-to-turn-laravel-from-static-to-dependency-injection-in-one-day/<br/>https://laravel.com/docs/12.x/facades#facades-vs-dependency-injection |
91+
| [LaravelSetList::LARAVEL_IF_HELPERS](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-if-helpers.php) | Replaces `abort()`, `report()`, `throw` statements inside conditions with `abort_if()`, `report_if()`, `throw_if()` function calls.<br/><https://laravel.com/docs/12.x/helpers#method-abort-if> |
92+
| [LaravelSetList::LARAVEL_LEGACY_FACTORIES_TO_CLASSES](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-legacy-factories-to-classes.php) | Migrates Eloquent legacy model factories (with closures) into class based factories.<br/><https://laravel.com/docs/8.x/releases#model-factory-classes> |
93+
| [LaravelSetList::LARAVEL_STATIC_TO_INJECTION](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-static-to-injection.php) | Replaces Laravel's Facades with Dependency Injection.<br/><https://tomasvotruba.com/blog/2019/03/04/how-to-turn-laravel-from-static-to-dependency-injection-in-one-day/><br/><https://laravel.com/docs/12.x/facades#facades-vs-dependency-injection> |
94+
| [LaravelSetList::LARAVEL_TESTING](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-testing.php) | Improves Laravel testing by converting deprecated methods and adding better assertions. |
95+
| [LaravelSetList::LARAVEL_TYPE_DECLARATIONS](https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel-type-declarations.php) | Adds type hints and generic return types to improve Laravel code type safety. |
96+
97+
## Configurable Rules
98+
99+
These rules require configuration and must be added manually to your `rector.php` file.
100+
101+
```php
102+
<?php declare(strict_types=1);
103+
104+
use Rector\Config\RectorConfig;
105+
use RectorLaravel\Rector\FuncCall\RemoveDumpDataDeadCodeRector;
106+
107+
return RectorConfig::configure()
108+
->withConfiguredRule(RemoveDumpDataDeadCodeRector::class, [
109+
'dd', 'dump', 'var_dump'
110+
]);
111+
```
112+
113+
| Rule | Description |
114+
|------|-------------|
115+
| [RemoveDumpDataDeadCodeRector](https://github.com/driftingly/rector-laravel/blob/main/src/Rector/FuncCall/RemoveDumpDataDeadCodeRector.php) | Removes debug function calls like `dd()`, `dump()`, etc. from code. Configure with an array of function names to remove (default: `['dd', 'dump']`). |
116+
| [RouteActionCallableRector](https://github.com/driftingly/rector-laravel/blob/main/src/Rector/StaticCall/RouteActionCallableRector.php) | Converts route action strings like `'UserController@index'` to callable arrays `[UserController::class, 'index']`. Configure with `NAMESPACE` for controller namespace and `ROUTES` for file-specific namespaces. |
117+
| [WhereToWhereLikeRector](https://github.com/driftingly/rector-laravel/blob/main/src/Rector/MethodCall/WhereToWhereLikeRector.php) | Converts `where('column', 'like', 'value')` to `whereLike('column', 'value')` calls. Configure with `USING_POSTGRES_DRIVER` boolean to handle PostgreSQL vs MySQL differences. |
118+
119+
## Opinionated Rules
120+
121+
These rules are more opinionated and are not included in any sets by default.
122+
123+
```php
124+
<?php declare(strict_types=1);
125+
126+
use Rector\Config\RectorConfig;
127+
use RectorLaravel\Rector\MethodCall\ResponseHelperCallToJsonResponseRector;
128+
129+
return RectorConfig::configure()
130+
->withRules([
131+
ResponseHelperCallToJsonResponseRector::class,
132+
]);
133+
```
134+
135+
| Rule | Description |
136+
|------|-------------|
137+
| [RemoveModelPropertyFromFactoriesRector](https://github.com/driftingly/rector-laravel/blob/main/src/Rector/Class_/RemoveModelPropertyFromFactoriesRector.php) | Removes the `$model` property from Factories. |
138+
| [ResponseHelperCallToJsonResponseRector](https://github.com/driftingly/rector-laravel/blob/main/src/Rector/MethodCall/ResponseHelperCallToJsonResponseRector.php) | Converts `response()->json()` to `new JsonResponse()`. |
139+
| [MinutesToSecondsInCacheRector](https://github.com/driftingly/rector-laravel/blob/main/src/Rector/StaticCall/MinutesToSecondsInCacheRector.php) | Change minutes argument to seconds in cache methods. |
140+
| [UseComponentPropertyWithinCommandsRector](https://github.com/driftingly/rector-laravel/blob/main/src/Rector/MethodCall/UseComponentPropertyWithinCommandsRector.php) | Use `$this->components` property within commands. |
141+
| [UseForwardsCallsTraitRector](https://github.com/driftingly/rector-laravel/blob/main/src/Rector/Class_/UseForwardsCallsTraitRector.php) | Replaces the use of `call_user_func` and `call_user_func_array` method with the CallForwarding trait. |
142+
| [EmptyToBlankAndFilledFuncRector](https://github.com/driftingly/rector-laravel/blob/main/src/Rector/Empty_/EmptyToBlankAndFilledFuncRector.php) | Converts `empty()` to `blank()` and `filled()` |
94143

95144
## Creating New Rules
96145

config/sets/laravel-code-quality.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,29 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6+
use RectorLaravel\Rector\ArrayDimFetch\EnvVariableToEnvHelperRector;
7+
use RectorLaravel\Rector\ArrayDimFetch\RequestVariablesToRequestFacadeRector;
8+
use RectorLaravel\Rector\ArrayDimFetch\ServerVariableToRequestFacadeRector;
9+
use RectorLaravel\Rector\ArrayDimFetch\SessionVariableToSessionFacadeRector;
610
use RectorLaravel\Rector\Assign\CallOnAppArrayAccessToStandaloneAssignRector;
11+
use RectorLaravel\Rector\Class_\AnonymousMigrationsRector;
712
use RectorLaravel\Rector\ClassMethod\MakeModelAttributesAndScopesProtectedRector;
813
use RectorLaravel\Rector\Coalesce\ApplyDefaultInsteadOfNullCoalesceRector;
914
use RectorLaravel\Rector\Expr\AppEnvironmentComparisonToParameterRector;
15+
use RectorLaravel\Rector\Expr\SubStrToStartsWithOrEndsWithStaticMethodCallRector\SubStrToStartsWithOrEndsWithStaticMethodCallRector;
16+
use RectorLaravel\Rector\FuncCall\NotFilledBlankFuncCallToBlankFilledFuncCallRector;
17+
use RectorLaravel\Rector\FuncCall\NowFuncWithStartOfDayMethodCallToTodayFuncRector;
18+
use RectorLaravel\Rector\FuncCall\RemoveRedundantValueCallsRector;
19+
use RectorLaravel\Rector\FuncCall\RemoveRedundantWithCallsRector;
20+
use RectorLaravel\Rector\FuncCall\SleepFuncToSleepStaticCallRector;
21+
use RectorLaravel\Rector\FuncCall\ThrowIfAndThrowUnlessExceptionsToUseClassStringRector;
22+
use RectorLaravel\Rector\MethodCall\EloquentOrderByToLatestOrOldestRector;
23+
use RectorLaravel\Rector\MethodCall\RedirectBackToBackHelperRector;
24+
use RectorLaravel\Rector\MethodCall\RedirectRouteToToRouteHelperRector;
1025
use RectorLaravel\Rector\MethodCall\ReverseConditionableMethodCallRector;
26+
use RectorLaravel\Rector\MethodCall\ValidationRuleArrayStringValueToArrayRector;
27+
use RectorLaravel\Rector\PropertyFetch\OptionalToNullsafeOperatorRector;
28+
use RectorLaravel\Rector\StaticCall\DispatchToHelperFunctionsRector;
1129

1230
return static function (RectorConfig $rectorConfig): void {
1331
$rectorConfig->import(__DIR__ . '/../config.php');
@@ -16,4 +34,22 @@
1634
$rectorConfig->rule(ApplyDefaultInsteadOfNullCoalesceRector::class);
1735
$rectorConfig->rule(MakeModelAttributesAndScopesProtectedRector::class);
1836
$rectorConfig->rule(AppEnvironmentComparisonToParameterRector::class);
37+
$rectorConfig->rule(ThrowIfAndThrowUnlessExceptionsToUseClassStringRector::class);
38+
$rectorConfig->rule(EnvVariableToEnvHelperRector::class);
39+
$rectorConfig->rule(RequestVariablesToRequestFacadeRector::class);
40+
$rectorConfig->rule(ServerVariableToRequestFacadeRector::class);
41+
$rectorConfig->rule(SessionVariableToSessionFacadeRector::class);
42+
$rectorConfig->rule(SubStrToStartsWithOrEndsWithStaticMethodCallRector::class);
43+
$rectorConfig->rule(NowFuncWithStartOfDayMethodCallToTodayFuncRector::class);
44+
$rectorConfig->rule(RemoveRedundantValueCallsRector::class);
45+
$rectorConfig->rule(RemoveRedundantWithCallsRector::class);
46+
$rectorConfig->rule(OptionalToNullsafeOperatorRector::class);
47+
$rectorConfig->rule(ValidationRuleArrayStringValueToArrayRector::class);
48+
$rectorConfig->rule(RedirectBackToBackHelperRector::class);
49+
$rectorConfig->rule(RedirectRouteToToRouteHelperRector::class);
50+
$rectorConfig->rule(AnonymousMigrationsRector::class);
51+
$rectorConfig->rule(SleepFuncToSleepStaticCallRector::class);
52+
$rectorConfig->rule(DispatchToHelperFunctionsRector::class);
53+
$rectorConfig->rule(NotFilledBlankFuncCallToBlankFilledFuncCallRector::class);
54+
$rectorConfig->rule(EloquentOrderByToLatestOrOldestRector::class);
1955
};

config/sets/laravel-testing.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use RectorLaravel\Rector\MethodCall\AssertStatusToAssertMethodRector;
7+
use RectorLaravel\Rector\MethodCall\JsonCallToExplicitJsonCallRector;
8+
use RectorLaravel\Rector\StaticCall\AssertWithClassStringToTypeHintedClosureRector;
9+
use RectorLaravel\Rector\StaticCall\CarbonSetTestNowToTravelToRector;
10+
11+
return static function (RectorConfig $rectorConfig): void {
12+
$rectorConfig->import(__DIR__ . '/../config.php');
13+
$rectorConfig->rule(JsonCallToExplicitJsonCallRector::class);
14+
$rectorConfig->rule(AssertStatusToAssertMethodRector::class);
15+
$rectorConfig->rule(AssertWithClassStringToTypeHintedClosureRector::class);
16+
$rectorConfig->rule(CarbonSetTestNowToTravelToRector::class);
17+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use RectorLaravel\Rector\ClassMethod\AddGenericReturnTypeToRelationsRector;
7+
use RectorLaravel\Rector\FuncCall\TypeHintTappableCallRector;
8+
use RectorLaravel\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector;
9+
use RectorLaravel\Rector\MethodCall\EloquentWhereTypeHintClosureParameterRector;
10+
11+
return static function (RectorConfig $rectorConfig): void {
12+
$rectorConfig->import(__DIR__ . '/../config.php');
13+
$rectorConfig->rule(TypeHintTappableCallRector::class);
14+
$rectorConfig->rule(AddGenericReturnTypeToRelationsRector::class);
15+
$rectorConfig->rule(EloquentWhereRelationTypeHintingParameterRector::class);
16+
$rectorConfig->rule(EloquentWhereTypeHintClosureParameterRector::class);
17+
};

config/sets/lumen.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use RectorLaravel\Rector\MethodCall\LumenRoutesStringActionToUsesArrayRector;
7+
use RectorLaravel\Rector\MethodCall\LumenRoutesStringMiddlewareToArrayRector;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->import(__DIR__ . '/../config.php');
11+
12+
$rectorConfig->rule(LumenRoutesStringActionToUsesArrayRector::class);
13+
$rectorConfig->rule(LumenRoutesStringMiddlewareToArrayRector::class);
14+
};

src/Rector/MethodCall/EloquentOrderByToLatestOrOldestRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class EloquentOrderByToLatestOrOldestRector extends AbstractRector implements Co
2929
/**
3030
* @var string[]
3131
*/
32-
private array $allowedPatterns = [];
32+
private array $allowedPatterns = ['*_at', '*_date', '*_on'];
3333

3434
public function __construct(private readonly QueryBuilderAnalyzer $queryBuilderAnalyzer) {}
3535

src/Set/LaravelSetList.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,10 @@ final class LaravelSetList
6161
final public const string LARAVEL_LEGACY_FACTORIES_TO_CLASSES = __DIR__ . '/../../config/sets/laravel-legacy-factories-to-classes.php';
6262

6363
final public const string LARAVEL_STATIC_TO_INJECTION = __DIR__ . '/../../config/sets/laravel-static-to-injection.php';
64+
65+
final public const string LARAVEL_TESTING = __DIR__ . '/../../config/sets/laravel-testing.php';
66+
67+
final public const string LARAVEL_TYPE_DECLARATIONS = __DIR__ . '/../../config/sets/laravel-type-declarations.php';
68+
69+
final public const string LUMEN = __DIR__ . '/../../config/sets/lumen.php';
6470
}

0 commit comments

Comments
 (0)