Skip to content

Commit 6aed947

Browse files
author
ityaozm@gmail.com
committed
refactor(ConfigCommand): Simplify handle method and extract editor logic
- Remove the ExecutableFinder dependency from the handle method signature. - Update config file output message format for clarity. - Inline editor determination logic into a private method to enhance readability. - Use a closure for setting TTY support in the Process instance to streamline the code.
1 parent 5a3cfc0 commit 6aed947

12 files changed

Lines changed: 43 additions & 251 deletions

app/Commands/CommitCommand.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use App\ConfigManager;
1919
use App\Exceptions\RuntimeException;
2020
use App\GeneratorManager;
21-
use Illuminate\Console\Scheduling\Schedule;
2221
use Illuminate\Support\Collection;
2322
use Illuminate\Support\Stringable;
2423
use LaravelZero\Framework\Commands\Command;
@@ -115,14 +114,6 @@ function (int $attempts) use ($cachedDiff, $type): string {
115114
});
116115
}
117116

118-
/**
119-
* @noinspection PhpMissingParentCallCommonInspection
120-
*/
121-
public function schedule(Schedule $schedule): void
122-
{
123-
// $schedule->command(static::class)->everyMinute();
124-
}
125-
126117
/**
127118
* @codeCoverageIgnore
128119
*

app/Commands/ConfigCommand.php

Lines changed: 40 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use App\ConfigManager;
1717
use App\Exceptions\RuntimeException;
1818
use App\Exceptions\UnsupportedConfigActionException;
19-
use Illuminate\Console\Scheduling\Schedule;
2019
use Illuminate\Support\Arr;
2120
use LaravelZero\Framework\Commands\Command;
2221
use Symfony\Component\Console\Completion\CompletionInput;
@@ -53,12 +52,14 @@ public function __construct()
5352
* @throws \Psr\Container\ContainerExceptionInterface
5453
* @throws \Psr\Container\NotFoundExceptionInterface
5554
*/
56-
public function handle(ExecutableFinder $executableFinder): int
55+
public function handle(): int
5756
{
5857
$file = $this->configFile();
59-
$this->output->note("The config file($file) is being operated.");
58+
59+
$this->output->note("The config file [$file] is being operated.");
6060
file_exists($file) or $this->configManager->putFile($file);
6161
$this->configManager->replaceFrom($file);
62+
6263
$action = $this->argument('action');
6364
$key = $this->argument('key');
6465

@@ -97,25 +98,10 @@ public function handle(ExecutableFinder $executableFinder): int
9798

9899
break;
99100
case 'edit':
100-
$editor = value(function () use ($executableFinder) {
101-
if ($editor = $this->option('editor')) {
102-
return $editor;
103-
}
104-
105-
$editors = windows_os() ? self::WINDOWS_EDITORS : self::UNIX_EDITORS;
106-
107-
foreach ($editors as $editor) {
108-
if ($executableFinder->find($editor)) {
109-
return $editor;
110-
}
111-
}
112-
113-
throw new RuntimeException('Unable to find a default editor or specify the editor.');
114-
});
115-
116-
tap(new Process([$editor, $file]), static function (Process $process): void {
117-
Process::isTtySupported() and $process->setTty(true);
118-
})->setTimeout(null)->mustRun();
101+
tap(
102+
new Process([$this->editor(), $file]),
103+
static fn (Process $process): bool => Process::isTtySupported() and $process->setTty(true)
104+
)->setTimeout(null)->mustRun();
119105

120106
break;
121107
default:
@@ -152,14 +138,6 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
152138
}
153139
}
154140

155-
/**
156-
* @noinspection PhpMissingParentCallCommonInspection
157-
*/
158-
public function schedule(Schedule $schedule): void
159-
{
160-
// $schedule->command(static::class)->everyMinute();
161-
}
162-
163141
public static function hydratedActions(): string
164142
{
165143
return implode(', ', self::ACTIONS);
@@ -196,50 +174,6 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
196174
}
197175
}
198176

199-
/**
200-
* @throws \JsonException
201-
*/
202-
private function argToValue(string $arg): mixed
203-
{
204-
// if (0 === strncasecmp($arg, 'null', 4)) {
205-
// return;
206-
// }
207-
//
208-
// if (0 === strncasecmp($arg, 'true', 4)) {
209-
// return true;
210-
// }
211-
//
212-
// if (0 === strncasecmp($arg, 'false', 5)) {
213-
// return false;
214-
// }
215-
//
216-
// if (is_numeric($arg)) {
217-
// return str_contains($arg, '.') ? (float) $arg : (int) $arg;
218-
// }
219-
220-
if (str($arg)->isJson()) {
221-
return json_decode($arg, true, 512, \JSON_THROW_ON_ERROR);
222-
}
223-
224-
return $arg;
225-
}
226-
227-
/**
228-
* @noinspection JsonEncodingApiUsageInspection
229-
*/
230-
private function valueToArg(mixed $value): string
231-
{
232-
// if (null === $value) {
233-
// return 'null';
234-
// }
235-
//
236-
// if (\is_scalar($value)) {
237-
// return var_export($value, true);
238-
// }
239-
240-
return (string) json_encode($value, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE);
241-
}
242-
243177
private function configFile(): string
244178
{
245179
if ($file = $this->option('file')) {
@@ -252,4 +186,36 @@ private function configFile(): string
252186

253187
return ConfigManager::localPath();
254188
}
189+
190+
private function editor(): string
191+
{
192+
if ($editor = $this->option('editor')) {
193+
return $editor;
194+
}
195+
196+
$editors = windows_os() ? self::WINDOWS_EDITORS : self::UNIX_EDITORS;
197+
198+
foreach ($editors as $editor) {
199+
if (resolve(ExecutableFinder::class)->find($editor)) {
200+
return $editor;
201+
}
202+
}
203+
204+
throw new RuntimeException('Unable to find a default editor or specify the editor.');
205+
}
206+
207+
/**
208+
* @throws \JsonException
209+
*/
210+
private function argToValue(string $arg): mixed
211+
{
212+
return str($arg)->isJson() ? json_decode($arg, true, 512, \JSON_THROW_ON_ERROR) : $arg;
213+
}
214+
215+
private function valueToArg(mixed $value): string
216+
{
217+
return \is_string($value)
218+
? $value
219+
: json_encode($value, ConfigManager::JSON_OPTIONS);
220+
}
255221
}

app/Commands/ThanksCommand.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace App\Commands;
1515

16-
use Illuminate\Console\Scheduling\Schedule;
1716
use LaravelZero\Framework\Commands\Command;
1817

1918
final class ThanksCommand extends Command
@@ -42,12 +41,4 @@ public function handle(): void
4241
' <options=bold>https://github.com/guanguans/ai-commit</>',
4342
]);
4443
}
45-
46-
/**
47-
* @noinspection PhpMissingParentCallCommonInspection
48-
*/
49-
public function schedule(Schedule $schedule): void
50-
{
51-
// $schedule->command(static::class)->everyMinute();
52-
}
5344
}

baselines/complexity.functionLike.neon

Lines changed: 0 additions & 8 deletions
This file was deleted.

baselines/loader.neon

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# total 4 errors
1+
# total 1 error
22
includes:
3-
- complexity.functionLike.neon
43
- method.notFound.neon
5-
- missing-identifier.neon

baselines/missing-identifier.neon

Lines changed: 0 additions & 8 deletions
This file was deleted.

composer-dependency-analyser.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
// __DIR__.'/src/Support/Rectors',
3131
])
3232
->ignoreUnknownClasses([
33-
'LaravelZero\Framework\Components\Logo\FigletString',
3433
])
3534
/** @see \ShipMonk\ComposerDependencyAnalyser\Analyser::CORE_EXTENSIONS */
3635
->ignoreErrorsOnExtensions(

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@
348348
"roave-no-leaks": "@php vendor/bin/roave-no-leaks",
349349
"sk": "@php vendor/bin/swiss-knife --ansi -v",
350350
"sk-alice-yaml-fixtures-to-php": "@sk alice-yaml-fixtures-to-php --help",
351-
"sk-check-commented-code": "@sk check-commented-code app/ bootstrap/ config/ resources/ tests/ --line-limit=20",
351+
"sk-check-commented-code": "@sk check-commented-code app/ bootstrap/ config/ resources/ tests/ --line-limit=6 --skip-file=bootstrap/providers.php",
352352
"sk-check-conflicts": "@sk check-conflicts app/ bootstrap/ config/ resources/ tests/",
353353
"sk-dump-editorconfig": "@sk dump-editorconfig",
354354
"sk-finalize-classes": "@sk finalize-classes app/",

config/app.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,6 @@
104104
*/
105105

106106
'providers' => [
107-
// Laravel Framework Service Providers...
108-
// Illuminate\Cache\CacheServiceProvider::class,
109-
// Illuminate\Filesystem\FilesystemServiceProvider::class,
110-
// Illuminate\Hashing\HashServiceProvider::class,
111-
// Illuminate\Pipeline\PipelineServiceProvider::class,
112-
// Illuminate\Translation\TranslationServiceProvider::class,
113-
// Illuminate\Validation\ValidationServiceProvider::class,
114-
// Illuminate\View\ViewServiceProvider::class,
115-
116-
// Application Service Providers...
117107
// App\Providers\AppServiceProvider::class,
118108
],
119109
];

config/commands.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
*/
5454

5555
'add' => [
56-
// ..
5756
Illuminate\Foundation\Console\VendorPublishCommand::class,
5857
],
5958

@@ -91,6 +90,6 @@
9190
*/
9291

9392
'remove' => [
94-
// ..
93+
// ...
9594
],
9695
];

0 commit comments

Comments
 (0)