Skip to content

Conversation

@shaedrich
Copy link
Contributor

@shaedrich shaedrich commented Dec 5, 2025

and input validation

like, for example, with \Illuminate\Support\Facades\Config (#50140) and \Illuminate\Support\Arr (#55567)

Enhancements

  • return value is typed
  • optionally provide a default value, otherwise, an exception is thrown if not found
  • when value has unexpected type, an exception is thrown

Example

<?php

namespace App\Console\Commands;

use App\Models\User;
use App\Support\DripEmailer;
use Illuminate\Console\Command;
 
class SendEmails extends Command
{
    protected $signature = 'mail:send {user} {--flag} {--subject=} {--cc=*}';
    protected $description = 'Send a marketing email to a user';
 
    /**
     * Execute the console command.
     */
    public function handle(DripEmailer $drip): void
    {
        $user = User::find($this->integer('user')); // argument typed as int, alternatively, you can use $this->int() or $this->number()
        $flag = $this->boolean('flag'); // flag typed as bool, there's also the alias $this->bool()
        $subject = $this->string('subject', 'Default subject'); // option typed as string with default value
        $cc = User::whereIn('id', $this->array('cc'))->get(); // option typed as array

        if ($flag) {
            // do something
        }

        $drip->send($user, $cc, $subject);
    }
}

and input validation

like, for example, with `\Illuminate\Support\Facades\Config`
*/
public function string(string $key, ?string $default = null): string
{
$value = match (true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block is called multiple times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block is called multiple times, and your code duplicates the logic for getting the value (checking argument, checking option, etc.) in several different parts of the codebase.

        $value = match (true) {
            $this->hasArgument($key) => $this->getArgument($key),
            $this->hasOption($key) => $this->getOption($key),
            default => $default ??  throw new InvalidArgumentException(sprintf('"%s" is neither an option nor an argument of this command', $key)),
        };

Copy link
Contributor Author

@shaedrich shaedrich Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you mean, I could move that to its own method? Sure, I could, but I intend to first see if Taylor has an appetite for that

@shaedrich
Copy link
Contributor Author

StyleCI can be fixed automatically upon merge as I understand it

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants