Skip to content

Commit

Permalink
refactor(score): Improve Soar score command signature and options han…
Browse files Browse the repository at this point in the history
…dling

- Update the signature of the `soar:score` command to include options
- Improve handling of options passed to Soar in the `ScoreCommand` class
- Refactor the `getNormalizeOptions` method to properly handle and normalize options
  • Loading branch information
guanguans committed Jun 7, 2024
1 parent 2972af9 commit 0339e22
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/Commands/ScoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@

use Guanguans\LaravelSoar\Soar;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

class ScoreCommand extends Command
{
protected $signature = 'soar:score';
protected $signature = 'soar:score
{--o|option=* : The option to be passed to Soar(e.g. --option=-report-type=markdown)}
';

protected $description = 'Get the scores of the given SQL statements';

/**
* @noinspection ForgottenDebugOutputInspection
* @noinspection DebugFunctionUsageInspection
* @noinspection MethodShouldBeFinalInspection
*
* @throws \Guanguans\SoarPHP\Exceptions\InvalidOptionException
Expand All @@ -37,6 +38,23 @@ public function handle(Soar $soar): void
}
}

collect($soar->arrayScores($sqls))->each(static fn (array $score) => dump($score));
echo tap($soar, function (Soar $soar): void {
$soar->mergeOptions($this->getNormalizeOptions());

if ($this->option('verbose')) {
$soar->dump();
}
})->scores($sqls);
}

private function getNormalizeOptions(): array
{
return collect($this->option('option'))
->mapWithKeys(static function (string $option): array {
[$key, $value] = Str::of($option)->explode('=', 2)->pad(2, null)->all();

return [Str::start($key, '-') => $value];
})
->all();
}
}

0 comments on commit 0339e22

Please sign in to comment.