Skip to content

Commit 5a3cfc0

Browse files
author
ityaozm@gmail.com
committed
perf(thanks-command): Simplify OS-specific URL opening logic
- Refactor the URL opening logic to use a match expression for clarity. - Ensure compatibility across macOS, Windows, and Linux platforms. - Improve code maintainability by reducing conditional statements.
1 parent 853b422 commit 5a3cfc0

3 files changed

Lines changed: 30 additions & 26 deletions

File tree

app/Commands/ConfigCommand.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,9 @@
3030

3131
final class ConfigCommand extends Command
3232
{
33-
/** @var list<string> */
34-
public const ACTIONS = ['set', 'get', 'unset', 'reset', 'list', 'edit'];
35-
36-
/** @var list<string> */
37-
protected const WINDOWS_EDITORS = ['notepad'];
38-
39-
/** @var list<string> */
40-
protected const UNIX_EDITORS = ['editor', 'vim', 'vi', 'nano', 'pico', 'ed'];
33+
private const ACTIONS = ['set', 'get', 'unset', 'reset', 'list', 'edit'];
34+
private const UNIX_EDITORS = ['editor', 'vim', 'vi', 'nano', 'pico', 'ed'];
35+
private const WINDOWS_EDITORS = ['notepad'];
4136

4237
/** @noinspection ClassOverridesFieldOfSuperClassInspection */
4338
protected $signature = 'config';
@@ -53,25 +48,14 @@ public function __construct()
5348
}
5449

5550
/**
51+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
5652
* @throws \JsonException
5753
* @throws \Psr\Container\ContainerExceptionInterface
5854
* @throws \Psr\Container\NotFoundExceptionInterface
5955
*/
6056
public function handle(ExecutableFinder $executableFinder): int
6157
{
62-
/** @var string $file */
63-
$file = value(function () {
64-
if ($file = $this->option('file')) {
65-
return $file;
66-
}
67-
68-
if ($this->option('global')) {
69-
return ConfigManager::globalPath();
70-
}
71-
72-
return ConfigManager::localPath();
73-
});
74-
58+
$file = $this->configFile();
7559
$this->output->note("The config file($file) is being operated.");
7660
file_exists($file) or $this->configManager->putFile($file);
7761
$this->configManager->replaceFrom($file);
@@ -176,6 +160,11 @@ public function schedule(Schedule $schedule): void
176160
// $schedule->command(static::class)->everyMinute();
177161
}
178162

163+
public static function hydratedActions(): string
164+
{
165+
return implode(', ', self::ACTIONS);
166+
}
167+
179168
/**
180169
* {@inheritDoc}
181170
*
@@ -185,7 +174,7 @@ public function schedule(Schedule $schedule): void
185174
protected function configure(): void
186175
{
187176
$this->setDefinition([
188-
new InputArgument('action', InputArgument::REQUIRED, \sprintf('The action(<comment>[%s]</comment>) name', implode(', ', self::ACTIONS))),
177+
new InputArgument('action', InputArgument::REQUIRED, \sprintf('The action(<comment>[%s]</comment>) name', self::hydratedActions())),
189178
new InputArgument('key', InputArgument::OPTIONAL, 'The key of config options'),
190179
new InputArgument('value', InputArgument::OPTIONAL, 'The value of config options'),
191180
new InputOption('global', 'g', InputOption::VALUE_NONE, 'Apply to the global config file'),
@@ -250,4 +239,17 @@ private function valueToArg(mixed $value): string
250239

251240
return (string) json_encode($value, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE);
252241
}
242+
243+
private function configFile(): string
244+
{
245+
if ($file = $this->option('file')) {
246+
return $file;
247+
}
248+
249+
if ($this->option('global')) {
250+
return ConfigManager::globalPath();
251+
}
252+
253+
return ConfigManager::localPath();
254+
}
253255
}

app/Commands/ThanksCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ public function handle(): void
2929
$wantsToSupport = $this->ask('Can you quickly <options=bold>star our GitHub repository</>? 🙏🏻', 'yes');
3030

3131
if (str($wantsToSupport)->trim()->is(['yes', 'y'])) {
32-
\PHP_OS_FAMILY === 'Darwin' and exec('open https://github.com/guanguans/ai-commit');
33-
\PHP_OS_FAMILY === 'Windows' and exec('start https://github.com/guanguans/ai-commit');
34-
\PHP_OS_FAMILY === 'Linux' and exec('xdg-open https://github.com/guanguans/ai-commit');
32+
exec(match (\PHP_OS_FAMILY) {
33+
'Darwin' => 'open https://github.com/guanguans/ai-commit',
34+
'Windows' => 'start https://github.com/guanguans/ai-commit',
35+
default => 'xdg-open https://github.com/guanguans/ai-commit',
36+
});
3537
}
3638

3739
$this->output->writeln([

app/Exceptions/UnsupportedConfigActionException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class UnsupportedConfigActionException extends InvalidArgumentException
2020
public static function make(string $action, int $code = 0, ?\Throwable $previous = null): self
2121
{
2222
return new self(
23-
\sprintf("The action [$action] is not supported, that must be one of [%s].", implode(', ', ConfigCommand::ACTIONS)),
23+
\sprintf("The action [$action] is not supported, that must be one of [%s].", ConfigCommand::hydratedActions()),
2424
$code,
2525
$previous
2626
);

0 commit comments

Comments
 (0)