Skip to content

Commit dbb3411

Browse files
authored
[10.x] Uses PHP Native Type Declarations 🐘 (#8287)
* Adds types to `up` and `down` migration methods * Adds types casts `get` and `set` methods * Adds types to `handle` middleware methods * Adds types to `join` channel method * Adds types to `handle` command method * Adds types to `render` component method * Adds types to `broadcastOn` event method * Adds types to `definition` model factory method * Adds missing types to `render` blade component * Adds missing types to `handle` middleware methods, `handle` listeners methods, and exceptions `render` methods * Adds missing types to `build` mail component * Adds missing types to notification component * Adds types to observers * Adds types to policies * Adds types to providers * Adds types to form requests * Adds types to resources * Adds types to rules * Adds types to scopes * Adds types to seeds * Adds types to tests * Fully types `artisan.md` * Fully types `authentication.md` * Fully types `authorization.md` * Fully types `billing.md` * Fully types `blade.md` * Fully types `broadcasting.md` * Fully types `cache.md` * Fully types `collections.md` * Fully types `container.md` * Fully types `controllers.md` * Fully types `database.md` * Fully types `cashier-paddle.md` * Fully types `container.md` * Fully types `controllers.md` * Fully types `database.md` * Fully types `dusk.md` * Fully types `eloquent-collections.md` * Fully types `eloquent-factories.md` * Fully types `eloquent-mutators.md` * Fully types `eloquent-relationships.md` * Fully types `eloquent-resources.md` * Fully types `eloquent-serialization.md` * Fully types `eloquent.md` * Fully types `encryption.md` * Fully types `errors.md` * Fully types `events.md` * Fully types `facades.md` * Fully types `filesystem.md` * Fully types `fortify.md` * Fully types `frontend.md` * Fully types `hashing.md` * Fully types `helpers.md` * Fully types `horizon.md` * Fully types `http-client.md` * Fully types `locatization.md` * Fully types `logging.md` * Fully types `mail.md` * Fully types `middleware.md` * Fully types `migrations.md` * Fully types `mocking.md` * Fully types `notifications.md` * Fully types `octane.md` * Fully types `pagination.md` * Fully types `passport.md` * Fully types `passport.md` * Fully types `passwords.md` * Fully types `providers.md` * Re-adds missing type * Fully types `queries.md` * Fully types `queues.md` * Fully types `rate-limiting.md` * Fully types `redirects.md` * Fully types `redis.md` * Fully types `requests.md` * Fully types `responses.md` * Fully types `routing.md` * Fully types `scheduling.md` * Fully types `scout.md` * Fully types `session.md` * Fully types `telescope.md` * Fully types `testing.md` * Fully types `urls.md` * Fully types `validation.md` * Fully types `verification.md` * Fully types `views.md` * Fully types `vite.md` * Adds some missing types * Types middlewares `$next` argument * Types missing responses * Uses `void` as command return type * Reverts changes on dots * Fixes signature * Reverts changes on Authenticable * Reverts changes on `UserProvider` * Reverts changes on `UserProvider` * Fixes type * Reverts changes on `resolveChildRouteBinding` * Clarifies returns on components * Reverts order of types * Updates types order * Reworks broadcasting docs * Removes unused import * Uses dots * Applies more types on casts * Fixes `withResponse` type * Improves order of types * Fixes order of imports * More dots * Fixes return type of scope * Improves `report` on exceptions * Improves errors * Fixes style * Adds missing type on Horizon's gate method * Revert changes on `sendPasswordResetNotifications` * Adds missing import * Revert changes on upgrade * More adjustsments * Clarifies that `$notifiable` is an object * Removes non-needed imports * Uses `response()->noContent()` * Improves rules types * Adjusts isolable commands types * Typo on comments * Adds remaining docs types
1 parent 767dc47 commit dbb3411

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1594
-2317
lines changed

artisan.md

+27-43
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,8 @@ Let's take a look at an example command. Note that we are able to request any de
147147

148148
/**
149149
* Execute the console command.
150-
*
151-
* @param \App\Support\DripEmailer $drip
152-
* @return mixed
153150
*/
154-
public function handle(DripEmailer $drip)
151+
public function handle(DripEmailer $drip): void
155152
{
156153
$drip->send(User::find($this->argument('user')));
157154
}
@@ -167,17 +164,15 @@ Closure based commands provide an alternative to defining console commands as cl
167164

168165
/**
169166
* Register the closure based commands for the application.
170-
*
171-
* @return void
172167
*/
173-
protected function commands()
168+
protected function commands(): void
174169
{
175170
require base_path('routes/console.php');
176171
}
177172

178173
Even though this file does not define HTTP routes, it defines console based entry points (routes) into your application. Within this file, you may define all of your closure based console commands using the `Artisan::command` method. The `command` method accepts two arguments: the [command signature](#defining-input-expectations) and a closure which receives the command's arguments and options:
179174

180-
Artisan::command('mail:send {user}', function ($user) {
175+
Artisan::command('mail:send {user}', function (string $user) {
181176
$this->info("Sending email to: {$user}!");
182177
});
183178

@@ -191,7 +186,7 @@ In addition to receiving your command's arguments and options, command closures
191186
use App\Models\User;
192187
use App\Support\DripEmailer;
193188

194-
Artisan::command('mail:send {user}', function (DripEmailer $drip, $user) {
189+
Artisan::command('mail:send {user}', function (DripEmailer $drip, string $user) {
195190
$drip->send(User::find($user));
196191
});
197192

@@ -200,7 +195,7 @@ In addition to receiving your command's arguments and options, command closures
200195

201196
When defining a closure based command, you may use the `purpose` method to add a description to the command. This description will be displayed when you run the `php artisan list` or `php artisan help` commands:
202197

203-
Artisan::command('mail:send {user}', function ($user) {
198+
Artisan::command('mail:send {user}', function (string $user) {
204199
// ...
205200
})->purpose('Send a marketing email to a user');
206201

@@ -242,12 +237,13 @@ php artisan mail:send 1 --isolated=12
242237
By default, isolation locks expire after the command is finished. Or, if the command is interrupted and unable to finish, the lock will expire after one hour. However, you may adjust the lock expiration time by defining a `isolationLockExpiresAt` method on your command:
243238

244239
```php
240+
use DateTimeInterface;
241+
use DateInterval;
242+
245243
/**
246244
* Determine when an isolation lock expires for the command.
247-
*
248-
* @return \DateTimeInterface|\DateInterval
249245
*/
250-
public function isolationLockExpiresAt()
246+
public function isolationLockExpiresAt(): DateTimeInterface|DateInterval
251247
{
252248
return now()->addMinutes(5);
253249
}
@@ -385,14 +381,10 @@ While your command is executing, you will likely need to access the values for t
385381

386382
/**
387383
* Execute the console command.
388-
*
389-
* @return int
390384
*/
391-
public function handle()
385+
public function handle(): void
392386
{
393387
$userId = $this->argument('user');
394-
395-
//
396388
}
397389

398390
If you need to retrieve all of the arguments as an `array`, call the `arguments` method:
@@ -414,12 +406,12 @@ In addition to displaying output, you may also ask the user to provide input dur
414406

415407
/**
416408
* Execute the console command.
417-
*
418-
* @return mixed
419409
*/
420-
public function handle()
410+
public function handle(): void
421411
{
422412
$name = $this->ask('What is your name?');
413+
414+
// ...
423415
}
424416

425417
The `secret` method is similar to `ask`, but the user's input will not be visible to them as they type in the console. This method is useful when asking for sensitive information such as passwords:
@@ -432,13 +424,13 @@ The `secret` method is similar to `ask`, but the user's input will not be visibl
432424
If you need to ask the user for a simple "yes or no" confirmation, you may use the `confirm` method. By default, this method will return `false`. However, if the user enters `y` or `yes` in response to the prompt, the method will return `true`.
433425

434426
if ($this->confirm('Do you wish to continue?')) {
435-
//
427+
// ...
436428
}
437429

438430
If necessary, you may specify that the confirmation prompt should return `true` by default by passing `true` as the second argument to the `confirm` method:
439431

440432
if ($this->confirm('Do you wish to continue?', true)) {
441-
//
433+
// ...
442434
}
443435

444436
<a name="auto-completion"></a>
@@ -450,7 +442,7 @@ The `anticipate` method can be used to provide auto-completion for possible choi
450442

451443
Alternatively, you may pass a closure as the second argument to the `anticipate` method. The closure will be called each time the user types an input character. The closure should accept a string parameter containing the user's input so far, and return an array of options for auto-completion:
452444

453-
$name = $this->anticipate('What is your address?', function ($input) {
445+
$name = $this->anticipate('What is your address?', function (string $input) {
454446
// Return auto-completion options...
455447
});
456448

@@ -482,10 +474,8 @@ To send output to the console, you may use the `line`, `info`, `comment`, `quest
482474

483475
/**
484476
* Execute the console command.
485-
*
486-
* @return mixed
487477
*/
488-
public function handle()
478+
public function handle(): void
489479
{
490480
// ...
491481

@@ -528,7 +518,7 @@ For long running tasks, it can be helpful to show a progress bar that informs us
528518

529519
use App\Models\User;
530520

531-
$users = $this->withProgressBar(User::all(), function ($user) {
521+
$users = $this->withProgressBar(User::all(), function (User $user) {
532522
$this->performTask($user);
533523
});
534524

@@ -558,10 +548,8 @@ All of your console commands are registered within your application's `App\Conso
558548

559549
/**
560550
* Register the commands for the application.
561-
*
562-
* @return void
563551
*/
564-
protected function commands()
552+
protected function commands(): void
565553
{
566554
$this->load(__DIR__.'/Commands');
567555
$this->load(__DIR__.'/../Domain/Orders/Commands');
@@ -582,12 +570,12 @@ Sometimes you may wish to execute an Artisan command outside of the CLI. For exa
582570

583571
use Illuminate\Support\Facades\Artisan;
584572

585-
Route::post('/user/{user}/mail', function ($user) {
573+
Route::post('/user/{user}/mail', function (string $user) {
586574
$exitCode = Artisan::call('mail:send', [
587575
'user' => $user, '--queue' => 'default'
588576
]);
589577

590-
//
578+
// ...
591579
});
592580

593581
Alternatively, you may pass the entire Artisan command to the `call` method as a string:
@@ -623,12 +611,12 @@ Using the `queue` method on the `Artisan` facade, you may even queue Artisan com
623611

624612
use Illuminate\Support\Facades\Artisan;
625613

626-
Route::post('/user/{user}/mail', function ($user) {
614+
Route::post('/user/{user}/mail', function (string $user) {
627615
Artisan::queue('mail:send', [
628616
'user' => $user, '--queue' => 'default'
629617
]);
630618

631-
//
619+
// ...
632620
});
633621

634622
Using the `onConnection` and `onQueue` methods, you may specify the connection or queue the Artisan command should be dispatched to:
@@ -644,16 +632,14 @@ Sometimes you may wish to call other commands from an existing Artisan command.
644632

645633
/**
646634
* Execute the console command.
647-
*
648-
* @return mixed
649635
*/
650-
public function handle()
636+
public function handle(): void
651637
{
652638
$this->call('mail:send', [
653639
'user' => 1, '--queue' => 'default'
654640
]);
655641

656-
//
642+
// ...
657643
}
658644

659645
If you would like to call another console command and suppress all of its output, you may use the `callSilently` method. The `callSilently` method has the same signature as the `call` method:
@@ -669,10 +655,8 @@ As you may know, operating systems allow signals to be sent to running processes
669655

670656
/**
671657
* Execute the console command.
672-
*
673-
* @return mixed
674658
*/
675-
public function handle()
659+
public function handle(): void
676660
{
677661
$this->trap(SIGTERM, fn () => $this->shouldKeepRunning = false);
678662

@@ -683,7 +667,7 @@ As you may know, operating systems allow signals to be sent to running processes
683667

684668
To listen for multiple signals at once, you may provide an array of signals to the `trap` method:
685669

686-
$this->trap([SIGTERM, SIGQUIT], function ($signal) {
670+
$this->trap([SIGTERM, SIGQUIT], function (int $signal) {
687671
$this->shouldKeepRunning = false;
688672

689673
dump($signal); // SIGTERM / SIGQUIT

0 commit comments

Comments
 (0)