Skip to content

Commit 7e07220

Browse files
authored
Add LARAVEL_FACTORIES set (#376)
* Add LARAVEL_FACTORIES set * Update docs * Update README.md
1 parent 12cfbbc commit 7e07220

File tree

6 files changed

+69
-13
lines changed

6 files changed

+69
-13
lines changed

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
[![Packagist Downloads](https://img.shields.io/packagist/dm/driftingly/rector-laravel)](https://packagist.org/packages/driftingly/rector-laravel/stats)
88
[![Packagist Version](https://img.shields.io/packagist/v/driftingly/rector-laravel)](https://packagist.org/packages/driftingly/rector-laravel)
99

10-
See available [Laravel rules](/docs/rector_rules_overview.md)
10+
## Available Rules
11+
12+
See all available Laravel rules [here](/docs/rector_rules_overview.md). This list includes even the rules that are not yet released, but are available under the `dev-main` branch.
13+
14+
You can also find the released rules on the Rector [Find Rule](https://getrector.com/find-rule?activeRectorSetGroup=laravel) page.
1115

1216
## Install
1317

@@ -77,15 +81,16 @@ return RectorConfig::configure()
7781
| Set | Purpose |
7882
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
7983
| [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')`. |
80-
| [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/11.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 |
8185
| [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. |
8286
| [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. |
83-
| [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/11.x/container#the-make-method |
84-
| [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/11.x/eloquent |
85-
| [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/11.x/facades#facade-class-reference |
86-
| [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/11.x/helpers#method-abort-if |
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 |
90+
| [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 |
8792
| [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 |
88-
| [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/11.x/facades#facades-vs-dependency-injection |
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 |
8994

9095
## Creating New Rules
9196

config/sets/laravel-factories.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\Class_\AddExtendsAnnotationToModelFactoriesRector;
7+
use RectorLaravel\Rector\Class_\AddHasFactoryToModelsRector;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->import(__DIR__ . '/../config.php');
11+
12+
$rectorConfig->rule(AddHasFactoryToModelsRector::class);
13+
$rectorConfig->rule(AddExtendsAnnotationToModelFactoriesRector::class);
14+
};

config/sets/laravel-legacy-factories-to-classes.php

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

55
use Rector\Config\RectorConfig;
6-
use RectorLaravel\Rector\Class_\AddHasFactoryToModelsRector;
76
use RectorLaravel\Rector\FuncCall\FactoryFuncCallToStaticCallRector;
87
use RectorLaravel\Rector\MethodCall\FactoryApplyingStatesRector;
98
use RectorLaravel\Rector\Namespace_\FactoryDefinitionRector;
@@ -14,7 +13,6 @@
1413
// https://laravel.com/docs/7.x/database-testing#writing-factories
1514
// https://laravel.com/docs/8.x/database-testing#defining-model-factories
1615
$rectorConfig->rule(FactoryDefinitionRector::class);
17-
$rectorConfig->rule(AddHasFactoryToModelsRector::class);
1816

1917
// https://laravel.com/docs/7.x/database-testing#using-factories
2018
// https://laravel.com/docs/8.x/database-testing#creating-models-using-factories

docs/rector_rules_overview.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 84 Rules Overview
1+
# 85 Rules Overview
22

33
## AbortIfRector
44

@@ -229,13 +229,19 @@ Convert migrations to anonymous classes.
229229

230230
## AppEnvironmentComparisonToParameterRector
231231

232-
Replace `$app->environment() === 'local'` with `$app->environment('local')`
232+
Replace app environment comparison with parameter or method call
233233

234234
- class: [`RectorLaravel\Rector\Expr\AppEnvironmentComparisonToParameterRector`](../src/Rector/Expr/AppEnvironmentComparisonToParameterRector.php)
235235

236236
```diff
237-
-$app->environment() === 'production';
238-
+$app->environment('production');
237+
-$app->environment() === 'local';
238+
-$app->environment() !== 'production';
239+
-$app->environment() === 'testing';
240+
-in_array($app->environment(), ['local', 'testing']);
241+
+$app->isLocal();
242+
+! $app->isProduction();
243+
+$app->environment('testing');
244+
+$app->environment(['local', 'testing']);
239245
```
240246

241247
<br>
@@ -892,6 +898,32 @@ Changes middlewares from rule definitions from string to array notation.
892898

893899
<br>
894900

901+
## MakeModelAttributesAndScopesProtectedRector
902+
903+
Makes Model attributes and scopes protected
904+
905+
- class: [`RectorLaravel\Rector\ClassMethod\MakeModelAttributesAndScopesProtectedRector`](../src/Rector/ClassMethod/MakeModelAttributesAndScopesProtectedRector.php)
906+
907+
```diff
908+
class User extends Model
909+
{
910+
- public function foo(): Attribute
911+
+ protected function foo(): Attribute
912+
{
913+
return Attribute::get(fn () => $this->bar);
914+
}
915+
916+
#[Scope]
917+
- public function active(Builder $query): Builder
918+
+ protected function active(Builder $query): Builder
919+
{
920+
return $query->where('active', true);
921+
}
922+
}
923+
```
924+
925+
<br>
926+
895927
## MigrateToSimplifiedAttributeRector
896928

897929
Migrate to the new Model attributes syntax

src/Set/LaravelSetList.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ final class LaravelSetList
5454

5555
final public const string LARAVEL_FACADE_ALIASES_TO_FULL_NAMES = __DIR__ . '/../../config/sets/laravel-facade-aliases-to-full-names.php';
5656

57+
final public const string LARAVEL_FACTORIES = __DIR__ . '/../../config/sets/laravel-factories.php';
58+
5759
final public const string LARAVEL_IF_HELPERS = __DIR__ . '/../../config/sets/laravel-if-helpers.php';
5860

5961
final public const string LARAVEL_LEGACY_FACTORIES_TO_CLASSES = __DIR__ . '/../../config/sets/laravel-legacy-factories-to-classes.php';

src/Set/LaravelSetProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public function provide(): array
6767
'Container strings to FQN types',
6868
LaravelSetList::LARAVEL_CONTAINER_STRING_TO_FULLY_QUALIFIED_NAME,
6969
),
70+
new Set(
71+
self::GROUP_NAME,
72+
'Factory improvements',
73+
LaravelSetList::LARAVEL_FACTORIES,
74+
),
7075
new Set(
7176
self::GROUP_NAME,
7277
'Rename Aliases to FQN Classes',

0 commit comments

Comments
 (0)