Skip to content

Commit f69bb2d

Browse files
committed
refactor(commands): update soar option handling and improve user prompts
- Simplify and streamline the `WithSoarOptions` trait by removing unnecessary method definitions. - Use `resolve` instead of `app` for dependency resolution in `soar()`. - Rename `normalizedSoarOptions` to `normalizedOptions` for consistency. - Improve handling of `verbose` mode with `isDebug()` in `debugSoar()`. - Adjust `ScoreCommand` to handle empty query input with more efficient and streamlined logic. - Remove redundant comments and clarify inline explanations for reading queries from STDIN.
1 parent ea48b52 commit f69bb2d

File tree

6 files changed

+18
-30
lines changed

6 files changed

+18
-30
lines changed

phpstan.neon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,24 @@ parameters:
8080
function: 'env_explode()'
8181
message: 'use config() instead'
8282
ignoreErrors:
83-
- '#^Call to method label\(\) on an unknown class Spatie\\.*\\Ray\.$#'
84-
- '#^Function clock not found\.$#'
8583
# - identifier: cast.string
84+
# - identifier: encapsedStringPart.nonString
8685
# - identifier: logicalAnd.resultUnused
8786
# - identifier: return.void
8887
# - identifier: symplify.noDynamicName
8988
# - identifier: typePerfect.noMixedMethodCaller
9089
- identifier: argument.templateType
9190
- identifier: argument.type
9291
- identifier: binaryOp.invalid
93-
- identifier: encapsedStringPart.nonString
9492
- identifier: method.nonObject
9593
- identifier: missingType.generics
9694
- identifier: missingType.iterableValue
9795
- identifier: return.type
9896
- identifier: shipmonk.deadMethod
9997
- identifier: symplify.explicitTraitSuffixName
10098
- identifier: symplify.requireExceptionNamespace
99+
- '#^Call to method label\(\) on an unknown class Spatie\\.*\\Ray\.$#'
100+
- '#^Function clock not found\.$#'
101101
-
102102
identifier: symplify.explicitInterfaceSuffixName
103103
path: src/Contracts/

src/Commands/Concerns/WithSoarOptions.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,23 @@ trait WithSoarOptions
2626
{
2727
protected function configure(): void
2828
{
29-
$this->setDefinition($this->definition());
30-
}
31-
32-
/**
33-
* @return list<\Symfony\Component\Console\Input\InputArgument|\Symfony\Component\Console\Input\InputOption>
34-
*/
35-
protected function definition(): array
36-
{
37-
return [
29+
$this->setDefinition([
3830
new InputOption(
3931
'option',
40-
'o',
32+
null,
4133
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
42-
'The option to be passed to Soar(e.g. `--option=-report-type=markdown` or `--option report-type=markdown` or `-o report-type=markdown`)',
34+
'The option to be passed to Soar(e.g. `--option=-report-type=markdown` or `--option report-type=markdown`)',
4335
),
44-
];
36+
]);
4537
}
4638

4739
protected function debugSoar(): Soar
4840
{
4941
$soar = $this->soar();
5042

51-
if ($this->option('verbose')) {
43+
if ($this->output->isVerbose()) {
5244
$soar->dump();
45+
5346
$this->output->newLine();
5447
}
5548

@@ -58,10 +51,10 @@ protected function debugSoar(): Soar
5851

5952
protected function soar(): Soar
6053
{
61-
return app(Soar::class)->withOptions($this->normalizedSoarOptions());
54+
return resolve(Soar::class)->withOptions($this->normalizedOptions());
6255
}
6356

64-
protected function normalizedSoarOptions(): array
57+
protected function normalizedOptions(): array
6558
{
6659
return collect($this->option('option'))
6760
->mapWithKeys(static function (string $option): array {

src/Commands/ScoreCommand.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,16 @@ class ScoreCommand extends Command
3333
*/
3434
public function handle(): void
3535
{
36-
$soar = $this->soar();
37-
38-
$query = $soar->getOption('-query');
36+
$query = $this->soar()->getOption('-query');
3937

38+
// If the query is not passed in, read from STDIN
4039
if (($fstat = fstat(\STDIN)) && 0 < $fstat['size']) {
4140
$query = trim(stream_get_contents(\STDIN));
4241
fclose(\STDIN);
4342
}
4443

45-
while (true) {
46-
$query = $query ?: $this->ask('Please input the SQL statements');
47-
48-
if ($query) {
44+
while (blank($query)) {
45+
if (filled($query = $this->ask('Please input the SQL statements'))) {
4946
break;
5047
}
5148
}

tests/Commands/ClearCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Guanguans\LaravelSoar\Facades\Soar;
2323
use function Pest\Laravel\artisan;
2424

25-
it('can clear the soar log file', function (): void {
25+
it('can clear the Soar log file', function (): void {
2626
Soar::onlyVerbose()->scores('select * from foo;');
2727

2828
expect($logFile = ClearCommand::soarLogFile())->toBeFile();

tests/Commands/RunCommandTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020

2121
use Guanguans\LaravelSoar\Commands\RunCommand;
22-
use Symfony\Component\Console\Command\Command;
2322
use function Pest\Laravel\artisan;
2423

2524
it('can run Soar with the given options', function (): void {
@@ -32,5 +31,5 @@
3231
],
3332
'--verbose' => true,
3433
]
35-
)->assertExitCode(Command::SUCCESS);
34+
)->assertOk();
3635
})->group(__DIR__, __FILE__);

tests/Commands/ScoreCommandTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@
1919
*/
2020

2121
use Guanguans\LaravelSoar\Commands\ScoreCommand;
22-
use Symfony\Component\Console\Command\Command;
2322
use function Pest\Laravel\artisan;
2423

2524
it('can get the Soar scores of the given SQL statements', function (): void {
2625
artisan(ScoreCommand::class)
2726
->expectsQuestion('Please input the SQL statements', 'select * from foo; select * from bar;')
28-
->assertExitCode(Command::SUCCESS);
27+
->assertOk();
2928
})->group(__DIR__, __FILE__);

0 commit comments

Comments
 (0)