1
- # 27 Rules Overview
1
+ # 34 Rules Overview
2
2
3
3
## AddArgumentDefaultValueRector
4
4
@@ -9,9 +9,13 @@ Adds default value for arguments in defined methods.
9
9
- class: [ ` RectorLaravel\Rector\ClassMethod\AddArgumentDefaultValueRector ` ] ( ../src/Rector/ClassMethod/AddArgumentDefaultValueRector.php )
10
10
11
11
``` php
12
- use Rector\Config\RectorConfig;
12
+ <?php
13
+
14
+ declare(strict_types=1);
15
+
13
16
use RectorLaravel\Rector\ClassMethod\AddArgumentDefaultValueRector;
14
17
use RectorLaravel\ValueObject\AddArgumentDefaultValue;
18
+ use Rector\Config\RectorConfig;
15
19
16
20
return static function (RectorConfig $rectorConfig): void {
17
21
$rectorConfig->ruleWithConfiguration(AddArgumentDefaultValueRector::class, [
@@ -168,21 +172,164 @@ Convert migrations to anonymous classes.
168
172
169
173
<br >
170
174
175
+ ## ArgumentFuncCallToMethodCallRector
176
+
177
+ Move help facade-like function calls to constructor injection
178
+
179
+ :wrench : ** configure it!**
180
+
181
+ - class: [ ` RectorLaravel\Rector\FuncCall\ArgumentFuncCallToMethodCallRector ` ] ( ../src/Rector/FuncCall/ArgumentFuncCallToMethodCallRector.php )
182
+
183
+ ``` php
184
+ <?php
185
+
186
+ declare(strict_types=1);
187
+
188
+ use RectorLaravel\Rector\FuncCall\ArgumentFuncCallToMethodCallRector;
189
+ use RectorLaravel\ValueObject\ArgumentFuncCallToMethodCall;
190
+ use Rector\Config\RectorConfig;
191
+
192
+ return static function (RectorConfig $rectorConfig): void {
193
+ $rectorConfig->ruleWithConfiguration(ArgumentFuncCallToMethodCallRector::class, [
194
+ new ArgumentFuncCallToMethodCall('view', 'Illuminate\Contracts\View\Factory', 'make'),
195
+ ]);
196
+ };
197
+ ```
198
+
199
+ ↓
200
+
201
+ ``` diff
202
+ class SomeController
203
+ {
204
+ + /**
205
+ + * @var \Illuminate\Contracts\View\Factory
206
+ + */
207
+ + private $viewFactory;
208
+ +
209
+ + public function __construct(\Illuminate\Contracts\View\Factory $viewFactory)
210
+ + {
211
+ + $this->viewFactory = $viewFactory;
212
+ + }
213
+ +
214
+ public function action()
215
+ {
216
+ - $template = view('template.blade');
217
+ - $viewFactory = view();
218
+ + $template = $this->viewFactory->make('template.blade');
219
+ + $viewFactory = $this->viewFactory;
220
+ }
221
+ }
222
+ ```
223
+
224
+ <br >
225
+
171
226
## AssertStatusToAssertMethodRector
172
227
173
- Change ` assertStatus($statusCode ) ` to the equivalent method ` assertOk() ` for example.
228
+ Replace ` (new \Illuminate\Testing\TestResponse)-> assertStatus(200 )` with ` (new \Illuminate\Testing\TestResponse)-> assertOk()`
174
229
175
230
- class: [ ` RectorLaravel\Rector\MethodCall\AssertStatusToAssertMethodRector ` ] ( ../src/Rector/MethodCall/AssertStatusToAssertMethodRector.php )
176
231
177
232
``` diff
178
- use Illuminate\Foundation\Testing\TestCase;
179
-
180
- final class SomeTest extends TestCase
233
+ class ExampleTest extends \Illuminate\Foundation\Testing\TestCase
181
234
{
182
- public function test(): void
235
+ public function testOk()
183
236
{
184
237
- $this->get('/')->assertStatus(200);
238
+ - $this->get('/')->assertStatus(\Illuminate\Http\Response::HTTP_OK);
239
+ - $this->get('/')->assertStatus(\Symfony\Component\HttpFoundation\Response::HTTP_OK);
240
+ + $this->get('/')->assertOk();
185
241
+ $this->get('/')->assertOk();
242
+ + $this->get('/')->assertOk();
243
+ }
244
+
245
+ public function testNoContent()
246
+ {
247
+ - $this->get('/')->assertStatus(204);
248
+ - $this->get('/')->assertStatus(\Illuminate\Http\Response::HTTP_NO_CONTENT);
249
+ - $this->get('/')->assertStatus(\Symfony\Component\HttpFoundation\Response::HTTP_NO_CONTENT);
250
+ + $this->get('/')->assertNoContent();
251
+ + $this->get('/')->assertNoContent();
252
+ + $this->get('/')->assertNoContent();
253
+ }
254
+
255
+ public function testUnauthorized()
256
+ {
257
+ - $this->get('/')->assertStatus(401);
258
+ - $this->get('/')->assertStatus(\Illuminate\Http\Response::HTTP_UNAUTHORIZED);
259
+ - $this->get('/')->assertStatus(\Symfony\Component\HttpFoundation\Response::HTTP_UNAUTHORIZED);
260
+ + $this->get('/')->assertUnauthorized();
261
+ + $this->get('/')->assertUnauthorized();
262
+ + $this->get('/')->assertUnauthorized();
263
+ }
264
+
265
+ public function testForbidden()
266
+ {
267
+ - $this->get('/')->assertStatus(403);
268
+ - $this->get('/')->assertStatus(\Illuminate\Http\Response::HTTP_FORBIDDEN);
269
+ - $this->get('/')->assertStatus(\Symfony\Component\HttpFoundation\Response::HTTP_FORBIDDEN);
270
+ + $this->get('/')->assertForbidden();
271
+ + $this->get('/')->assertForbidden();
272
+ + $this->get('/')->assertForbidden();
273
+ }
274
+
275
+ public function testNotFound()
276
+ {
277
+ - $this->get('/')->assertStatus(404);
278
+ - $this->get('/')->assertStatus(\Illuminate\Http\Response::HTTP_NOT_FOUND);
279
+ - $this->get('/')->assertStatus(\Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND);
280
+ + $this->get('/')->assertNotFound();
281
+ + $this->get('/')->assertNotFound();
282
+ + $this->get('/')->assertNotFound();
283
+ }
284
+
285
+ public function testMethodNotAllowed()
286
+ {
287
+ - $this->get('/')->assertStatus(405);
288
+ - $this->get('/')->assertStatus(\Illuminate\Http\Response::HTTP_METHOD_NOT_ALLOWED);
289
+ - $this->get('/')->assertStatus(\Symfony\Component\HttpFoundation\Response::HTTP_METHOD_NOT_ALLOWED);
290
+ + $this->get('/')->assertMethodNotAllowed();
291
+ + $this->get('/')->assertMethodNotAllowed();
292
+ + $this->get('/')->assertMethodNotAllowed();
293
+ }
294
+
295
+ public function testUnprocessableEntity()
296
+ {
297
+ - $this->get('/')->assertStatus(422);
298
+ - $this->get('/')->assertStatus(\Illuminate\Http\Response::HTTP_UNPROCESSABLE_ENTITY);
299
+ - $this->get('/')->assertStatus(\Symfony\Component\HttpFoundation\Response::HTTP_UNPROCESSABLE_ENTITY);
300
+ + $this->get('/')->assertUnprocessable();
301
+ + $this->get('/')->assertUnprocessable();
302
+ + $this->get('/')->assertUnprocessable();
303
+ }
304
+
305
+ public function testGone()
306
+ {
307
+ - $this->get('/')->assertStatus(410);
308
+ - $this->get('/')->assertStatus(\Illuminate\Http\Response::HTTP_GONE);
309
+ - $this->get('/')->assertStatus(\Symfony\Component\HttpFoundation\Response::HTTP_GONE);
310
+ + $this->get('/')->assertGone();
311
+ + $this->get('/')->assertGone();
312
+ + $this->get('/')->assertGone();
313
+ }
314
+
315
+ public function testInternalServerError()
316
+ {
317
+ - $this->get('/')->assertStatus(500);
318
+ - $this->get('/')->assertStatus(\Illuminate\Http\Response::HTTP_INTERNAL_SERVER_ERROR);
319
+ - $this->get('/')->assertStatus(\Symfony\Component\HttpFoundation\Response::HTTP_INTERNAL_SERVER_ERROR);
320
+ + $this->get('/')->assertInternalServerError();
321
+ + $this->get('/')->assertInternalServerError();
322
+ + $this->get('/')->assertInternalServerError();
323
+ }
324
+
325
+ public function testServiceUnavailable()
326
+ {
327
+ - $this->get('/')->assertStatus(503);
328
+ - $this->get('/')->assertStatus(\Illuminate\Http\Response::HTTP_SERVICE_UNAVAILABLE);
329
+ - $this->get('/')->assertStatus(\Symfony\Component\HttpFoundation\Response::HTTP_SERVICE_UNAVAILABLE);
330
+ + $this->get('/')->asserServiceUnavailable();
331
+ + $this->get('/')->asserServiceUnavailable();
332
+ + $this->get('/')->asserServiceUnavailable();
186
333
}
187
334
}
188
335
```
@@ -270,15 +417,15 @@ Convert DB Expression `__toString()` calls to `getValue()` method calls.
270
417
271
418
## EmptyToBlankAndFilledFuncRector
272
419
273
- Convert ` empty() ` calls to ` blank() ` and ` !empty ()` calls to ` filled() ` .
420
+ Replace use of the unsafe ` empty() ` function with Laravel's safer ` blank ()` & ` filled() ` functions .
274
421
275
- - class: [ ` RectorLaravel\Rector\FuncCall \EmptyToBlankAndFilledFuncRector ` ] ( ../src/Rector/FuncCall /EmptyToBlankAndFilledFuncRector.php )
422
+ - class: [ ` RectorLaravel\Rector\Empty_ \EmptyToBlankAndFilledFuncRector ` ] ( ../src/Rector/Empty_ /EmptyToBlankAndFilledFuncRector.php )
276
423
277
424
``` diff
278
- - $ empty = empty($value );
279
- + $ empty = blank($value );
280
- - $notEmpty = !empty($value );
281
- + $notEmpty = filled($value );
425
+ - empty([] );
426
+ - ! empty([] );
427
+ + blank([] );
428
+ + filled([] );
282
429
```
283
430
284
431
<br >
@@ -441,38 +588,28 @@ Change minutes argument to seconds in `Illuminate\Contracts\Cache\Store` and Ill
441
588
442
589
## NotFilledBlankFuncCallToBlankFilledFuncCallRector
443
590
444
- Change ` !blank() ` func calls to ` filled() ` func calls and vice versa .
591
+ Swap the use of NotBooleans used with ` filled() ` and ` blank() ` to the correct helper .
445
592
446
593
- class: [ ` RectorLaravel\Rector\FuncCall\NotFilledBlankFuncCallToBlankFilledFuncCallRector ` ] ( ../src/Rector/FuncCall/NotFilledBlankFuncCallToBlankFilledFuncCallRector.php )
447
594
448
595
``` diff
449
- class SomeClass
450
- {
451
- public function run()
452
- {
453
- - return !blank($value);
454
- + return filled($value);
455
- }
456
- }
596
+ - !filled([]);
597
+ - !blank([]);
598
+ + blank([]);
599
+ + filled([]);
457
600
```
458
601
459
602
<br >
460
603
461
604
## NowFuncWithStartOfDayMethodCallToTodayFuncRector
462
605
463
- Changes the user of ` now()->startOfDay() ` to be replaced with ` today() ` .
606
+ Use ` today() ` instead of ` now()->startOfDay() `
464
607
465
608
- class: [ ` RectorLaravel\Rector\FuncCall\NowFuncWithStartOfDayMethodCallToTodayFuncRector ` ] ( ../src/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector.php )
466
609
467
610
``` diff
468
- class SomeClass
469
- {
470
- public function run()
471
- {
472
- - now()->startOfDay();
473
- + today();
474
- }
475
- }
611
+ - $now = now()->startOfDay();
612
+ + $now = today();
476
613
```
477
614
478
615
<br >
@@ -486,12 +623,18 @@ Convert simple calls to optional helper to use the nullsafe operator
486
623
- class: [ ` RectorLaravel\Rector\PropertyFetch\OptionalToNullsafeOperatorRector ` ] ( ../src/Rector/PropertyFetch/OptionalToNullsafeOperatorRector.php )
487
624
488
625
``` php
489
- use Rector\Config\RectorConfig;
626
+ <?php
627
+
628
+ declare(strict_types=1);
629
+
490
630
use RectorLaravel\Rector\PropertyFetch\OptionalToNullsafeOperatorRector;
631
+ use Rector\Config\RectorConfig;
491
632
492
633
return static function (RectorConfig $rectorConfig): void {
493
634
$rectorConfig->ruleWithConfiguration(OptionalToNullsafeOperatorRector::class, [
494
- OptionalToNullsafeOperatorRector::EXCLUDE_METHODS => ['present'],
635
+ OptionalToNullsafeOperatorRector::EXCLUDE_METHODS => [
636
+ 'present',
637
+ ],
495
638
]);
496
639
};
497
640
```
@@ -683,8 +826,12 @@ Use PHP callable syntax instead of string syntax for controller route declaratio
683
826
- class: [ ` RectorLaravel\Rector\StaticCall\RouteActionCallableRector ` ] ( ../src/Rector/StaticCall/RouteActionCallableRector.php )
684
827
685
828
``` php
686
- use Rector\Config\RectorConfig;
829
+ <?php
830
+
831
+ declare(strict_types=1);
832
+
687
833
use RectorLaravel\Rector\StaticCall\RouteActionCallableRector;
834
+ use Rector\Config\RectorConfig;
688
835
689
836
return static function (RectorConfig $rectorConfig): void {
690
837
$rectorConfig->ruleWithConfiguration(RouteActionCallableRector::class, [
0 commit comments