1- # 27 Rules Overview
1+ # 34 Rules Overview
22
33## AddArgumentDefaultValueRector
44
@@ -9,9 +9,13 @@ Adds default value for arguments in defined methods.
99- class: [ ` RectorLaravel\Rector\ClassMethod\AddArgumentDefaultValueRector ` ] ( ../src/Rector/ClassMethod/AddArgumentDefaultValueRector.php )
1010
1111``` php
12- use Rector\Config\RectorConfig;
12+ <?php
13+
14+ declare(strict_types=1);
15+
1316use RectorLaravel\Rector\ClassMethod\AddArgumentDefaultValueRector;
1417use RectorLaravel\ValueObject\AddArgumentDefaultValue;
18+ use Rector\Config\RectorConfig;
1519
1620return static function (RectorConfig $rectorConfig): void {
1721 $rectorConfig->ruleWithConfiguration(AddArgumentDefaultValueRector::class, [
@@ -168,21 +172,164 @@ Convert migrations to anonymous classes.
168172
169173<br >
170174
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+
171226## AssertStatusToAssertMethodRector
172227
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()`
174229
175230- class: [ ` RectorLaravel\Rector\MethodCall\AssertStatusToAssertMethodRector ` ] ( ../src/Rector/MethodCall/AssertStatusToAssertMethodRector.php )
176231
177232``` diff
178- use Illuminate\Foundation\Testing\TestCase;
179-
180- final class SomeTest extends TestCase
233+ class ExampleTest extends \Illuminate\Foundation\Testing\TestCase
181234 {
182- public function test(): void
235+ public function testOk()
183236 {
184237- $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();
185241+ $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();
186333 }
187334 }
188335```
@@ -270,15 +417,15 @@ Convert DB Expression `__toString()` calls to `getValue()` method calls.
270417
271418## EmptyToBlankAndFilledFuncRector
272419
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 .
274421
275- - class: [ ` RectorLaravel\Rector\FuncCall \EmptyToBlankAndFilledFuncRector ` ] ( ../src/Rector/FuncCall /EmptyToBlankAndFilledFuncRector.php )
422+ - class: [ ` RectorLaravel\Rector\Empty_ \EmptyToBlankAndFilledFuncRector ` ] ( ../src/Rector/Empty_ /EmptyToBlankAndFilledFuncRector.php )
276423
277424``` 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([] );
282429```
283430
284431<br >
@@ -441,38 +588,28 @@ Change minutes argument to seconds in `Illuminate\Contracts\Cache\Store` and Ill
441588
442589## NotFilledBlankFuncCallToBlankFilledFuncCallRector
443590
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 .
445592
446593- class: [ ` RectorLaravel\Rector\FuncCall\NotFilledBlankFuncCallToBlankFilledFuncCallRector ` ] ( ../src/Rector/FuncCall/NotFilledBlankFuncCallToBlankFilledFuncCallRector.php )
447594
448595``` 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([]);
457600```
458601
459602<br >
460603
461604## NowFuncWithStartOfDayMethodCallToTodayFuncRector
462605
463- Changes the user of ` now()->startOfDay() ` to be replaced with ` today() ` .
606+ Use ` today() ` instead of ` now()->startOfDay() `
464607
465608- class: [ ` RectorLaravel\Rector\FuncCall\NowFuncWithStartOfDayMethodCallToTodayFuncRector ` ] ( ../src/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector.php )
466609
467610``` diff
468- class SomeClass
469- {
470- public function run()
471- {
472- - now()->startOfDay();
473- + today();
474- }
475- }
611+ - $now = now()->startOfDay();
612+ + $now = today();
476613```
477614
478615<br >
@@ -486,12 +623,18 @@ Convert simple calls to optional helper to use the nullsafe operator
486623- class: [ ` RectorLaravel\Rector\PropertyFetch\OptionalToNullsafeOperatorRector ` ] ( ../src/Rector/PropertyFetch/OptionalToNullsafeOperatorRector.php )
487624
488625``` php
489- use Rector\Config\RectorConfig;
626+ <?php
627+
628+ declare(strict_types=1);
629+
490630use RectorLaravel\Rector\PropertyFetch\OptionalToNullsafeOperatorRector;
631+ use Rector\Config\RectorConfig;
491632
492633return static function (RectorConfig $rectorConfig): void {
493634 $rectorConfig->ruleWithConfiguration(OptionalToNullsafeOperatorRector::class, [
494- OptionalToNullsafeOperatorRector::EXCLUDE_METHODS => ['present'],
635+ OptionalToNullsafeOperatorRector::EXCLUDE_METHODS => [
636+ 'present',
637+ ],
495638 ]);
496639};
497640```
@@ -683,8 +826,12 @@ Use PHP callable syntax instead of string syntax for controller route declaratio
683826- class: [ ` RectorLaravel\Rector\StaticCall\RouteActionCallableRector ` ] ( ../src/Rector/StaticCall/RouteActionCallableRector.php )
684827
685828``` php
686- use Rector\Config\RectorConfig;
829+ <?php
830+
831+ declare(strict_types=1);
832+
687833use RectorLaravel\Rector\StaticCall\RouteActionCallableRector;
834+ use Rector\Config\RectorConfig;
688835
689836return static function (RectorConfig $rectorConfig): void {
690837 $rectorConfig->ruleWithConfiguration(RouteActionCallableRector::class, [
0 commit comments