Skip to content

Commit

Permalink
style(app/Commands): refactor CommitCommand
Browse files Browse the repository at this point in the history
- Move the `diffCommand` method to be above the `commitCommandFor` method.
- Remove redundant comments and condition checks.
- Reorder methods in a more logical   order.
- Refactor the `createProcess` method to improve readability and organization.
  • Loading branch information
[email protected] committed Oct 17, 2024
1 parent 3e5539b commit a0a77ae
Showing 1 changed file with 45 additions and 45 deletions.
90 changes: 45 additions & 45 deletions app/Commands/CommitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ function ($attempts) use ($cachedDiff, $type): string {
$this->output->success('Successfully generated and committed message.');
}

public function schedule(Schedule $schedule): void
{
// $schedule->command(static::class)->everyMinute();
}

/**
* @codeCoverageIgnore
* @psalm-suppress InvalidArgument
Expand All @@ -161,11 +166,6 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
}
}

public function schedule(Schedule $schedule): void
{
// $schedule->command(static::class)->everyMinute();
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -274,32 +274,28 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
}
}

private function diffCommand(): array
{
return array_merge(['git', 'diff', '--cached'], $this->option('diff-options'));
}

/**
* @param null|mixed $input
* @psalm-suppress RedundantCondition
*
* @noinspection CallableParameterUseCaseInTypeContextInspection
*/
private function createProcess(
array $command,
?string $cwd = null,
?array $env = null,
$input = null,
?float $timeout = 60
): Process {
if (null === $cwd) {
$cwd = $this->argument('path');
}

return tap(new Process($command, $cwd, $env, $input, $timeout), function (Process $process): void {
if ($this->option('verbose')) {
$this->output->note($process->getCommandLine());
}
});
}

private function diffCommand(): array
private function commitCommandFor(Collection $message): array
{
return array_merge(['git', 'diff', '--cached'], $this->option('diff-options'));
$options = collect($this->option('commit-options'))
->when($this->shouldntEdit(), static function (Collection $collection): Collection {
return $collection->add('--no-edit');
})
->when($this->shouldntVerify(), static function (Collection $collection): Collection {
return $collection->add('--no-verify');
})
->all();

return array_merge(['git', 'commit', '--message', $this->hydrateMessage($message)], $options);
}

private function promptFor(string $cachedDiff, string $type): string
Expand Down Expand Up @@ -367,25 +363,6 @@ private function tryFixMessage(string $message): string
);
}

/**
* @psalm-suppress RedundantCondition
*
* @noinspection CallableParameterUseCaseInTypeContextInspection
*/
private function commitCommandFor(Collection $message): array
{
$options = collect($this->option('commit-options'))
->when($this->shouldntEdit(), static function (Collection $collection): Collection {
return $collection->add('--no-edit');
})
->when($this->shouldntVerify(), static function (Collection $collection): Collection {
return $collection->add('--no-verify');
})
->all();

return array_merge(['git', 'commit', '--message', $this->hydrateMessage($message)], $options);
}

private function hydrateMessage(Collection $message): string
{
return $message
Expand All @@ -398,6 +375,29 @@ private function hydrateMessage(Collection $message): string
->implode(str_repeat(PHP_EOL, 2));
}

/**
* @param null|mixed $input
*
* @noinspection CallableParameterUseCaseInTypeContextInspection
*/
private function createProcess(
array $command,
?string $cwd = null,
?array $env = null,
$input = null,
?float $timeout = 60
): Process {
if (null === $cwd) {
$cwd = $this->argument('path');
}

return tap(new Process($command, $cwd, $env, $input, $timeout), function (Process $process): void {
if ($this->option('verbose')) {
$this->output->note($process->getCommandLine());
}
});
}

private function shouldntEdit(): bool
{
return ! Process::isTtySupported() || $this->option('no-edit') || $this->configManager->get('no_edit');
Expand Down

0 comments on commit a0a77ae

Please sign in to comment.