Skip to content

Commit 4a9f348

Browse files
committed
refactor: Apply rector
1 parent 2e58f51 commit 4a9f348

23 files changed

+97
-103
lines changed

.php-cs-fixer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
* This source file is subject to the MIT license that is bundled.
1111
*/
1212

13+
use PhpCsFixer\Config;
14+
use PhpCsFixer\Finder;
15+
1316
$header = <<<'header'
1417
This file is part of the guanguans/ai-commit.
1518
@@ -19,7 +22,7 @@
1922
header;
2023

2124
/** @noinspection PhpParamsInspection */
22-
$finder = PhpCsFixer\Finder::create()
25+
$finder = Finder::create()
2326
->in([
2427
__DIR__.'/app',
2528
__DIR__.'/bootstrap',
@@ -60,7 +63,7 @@
6063

6164
// dd(json_encode($header, JSON_UNESCAPED_SLASHES));
6265

63-
return (new PhpCsFixer\Config())
66+
return (new Config())
6467
->setRules([
6568
'@DoctrineAnnotation' => true,
6669
// '@PHP80Migration:risky' => true,

app/Commands/CommitCommand.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ protected function initialize(InputInterface $input, OutputInterface $output)
7272
$config = $this->option('config') and $this->configManager->replaceFrom($config);
7373
}
7474

75-
public function handle()
75+
public function handle(): int
7676
{
77-
$this->task('1. Checking run environment', function () use (&$stagedDiff) {
78-
$isInsideWorkTree = $this->createProcess('git rev-parse --is-inside-work-tree')
77+
$this->task('1. Checking run environment', function () use (&$stagedDiff): void {
78+
$output = $this->createProcess('git rev-parse --is-inside-work-tree')
7979
->mustRun()
8080
->getOutput();
81-
if (! \str($isInsideWorkTree)->rtrim()->is('true')) {
81+
if (! \str($output)->rtrim()->is('true')) {
8282
$message = <<<'message'
8383
It looks like you are not in a git repository.
8484
Please run this command from the root of a git repository, or initialize one using `git init`.
@@ -93,7 +93,7 @@ public function handle()
9393
}
9494
}, 'checking...');
9595

96-
$this->task('2. Generating commit messages', function () use (&$messages, $stagedDiff) {
96+
$this->task('2. Generating commit messages', function () use (&$messages, $stagedDiff): void {
9797
$generator = $this->laravel->get(GeneratorManager::class)->driver($this->option('generator'));
9898
$messages = $generator->generate($this->getPromptOfAI($stagedDiff));
9999
if (\str($messages)->isEmpty()) {
@@ -109,15 +109,15 @@ public function handle()
109109
$this->line('');
110110
}, 'generating...');
111111

112-
$this->task('3. Choosing commit message', function () use ($messages, &$message) {
112+
$this->task('3. Choosing commit message', function () use ($messages, &$message): void {
113113
$messages = collect(json_decode($messages, true));
114114
$chosenSubject = $this->choice('Please choice a commit message', $messages->pluck('subject', 'id')->all());
115-
$message = $messages->first(function ($message) use ($chosenSubject) {
115+
$message = $messages->first(static function ($message) use ($chosenSubject): bool {
116116
return $message['subject'] === $chosenSubject;
117117
});
118118
}, 'choosing...');
119119

120-
$this->task('4. Committing message', function () use ($message) {
120+
$this->task('4. Committing message', function () use ($message): void {
121121
$this->createProcess($this->getCommitCommand($message))
122122
->setTty(true)
123123
->setTimeout(null)
@@ -153,7 +153,7 @@ protected function getPromptOfAI(string $stagedDiff): string
153153
[$this->configManager->get('diff_mark'), $this->configManager->get('num_mark')],
154154
[$stagedDiff, $this->option('num')]
155155
)
156-
->when($this->option('verbose'), function (Stringable $diff) {
156+
->when($this->option('verbose'), function (Stringable $diff): void {
157157
$this->line('');
158158
$this->comment('============================ start prompt ============================');
159159

@@ -194,10 +194,10 @@ protected function tryFixMessages(string $messages): string
194194
protected function getCommitCommand(array $message): array
195195
{
196196
return collect($message)
197-
->filter(function ($val) {
197+
->filter(static function ($val): bool {
198198
return $val && is_string($val);
199199
})
200-
->map(function (string $val) {
200+
->map(static function (string $val): string {
201201
return trim($val, " \t\n\r\x0B");
202202
})
203203
->pipe(function (Collection $message): array {
@@ -206,7 +206,7 @@ protected function getCommitCommand(array $message): array
206206
->pipe(function (Collection $options): Collection {
207207
$noEdit = $this->option('no-edit') ?: ! $this->configManager->get('edit');
208208
if ($noEdit) {
209-
return $options->filter(function (string $option): bool {
209+
return $options->filter(static function (string $option): bool {
210210
return '--edit' !== $option;
211211
});
212212
}
@@ -221,10 +221,8 @@ protected function getCommitCommand(array $message): array
221221

222222
/**
223223
* Define the command's schedule.
224-
*
225-
* @return void
226224
*/
227-
public function schedule(Schedule $schedule)
225+
public function schedule(Schedule $schedule): void
228226
{
229227
// $schedule->command(static::class)->everyMinute();
230228
}

app/Commands/ConfigCommand.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use App\ConfigManager;
1616
use Illuminate\Console\Scheduling\Schedule;
1717
use LaravelZero\Framework\Commands\Command;
18+
use RuntimeException;
1819
use Symfony\Component\Console\Input\InputArgument;
1920
use Symfony\Component\Console\Input\InputInterface;
2021
use Symfony\Component\Console\Input\InputOption;
@@ -23,6 +24,9 @@
2324

2425
class ConfigCommand extends Command
2526
{
27+
/**
28+
* @var string[]
29+
*/
2630
public const ACTIONS = ['set', 'get', 'unset', 'list', 'edit'];
2731

2832
/**
@@ -62,7 +66,7 @@ protected function configure()
6266
]);
6367
}
6468

65-
public function initialize(InputInterface $input, OutputInterface $output)
69+
protected function initialize(InputInterface $input, OutputInterface $output)
6670
{
6771
if (! file_exists($this->configManager::globalPath())) {
6872
$this->configManager->toGlobal();
@@ -71,10 +75,8 @@ public function initialize(InputInterface $input, OutputInterface $output)
7175

7276
/**
7377
* Execute the console command.
74-
*
75-
* @return mixed
7678
*/
77-
public function handle()
79+
public function handle(): int
7880
{
7981
$file = value(function () {
8082
if ($file = $this->option('file')) {
@@ -108,15 +110,13 @@ public function handle()
108110
break;
109111
case 'get':
110112
$value = null === $key ? $this->configManager->toJson() : $this->configManager->get($key);
111-
$value = transform($value, $transform = function ($value) {
113+
$value = transform($value, $transform = static function ($value) {
112114
if (is_string($value)) {
113115
return $value;
114116
}
115-
116117
if (null === $value) {
117118
return 'null';
118119
}
119-
120120
if (is_scalar($value)) {
121121
return (string) \str(json_encode([$value], JSON_UNESCAPED_UNICODE))->replaceFirst('[', '')->replaceLast(']', '');
122122
}
@@ -168,14 +168,14 @@ public function handle()
168168
}
169169
}
170170

171-
throw new \RuntimeException('No editor found or specified.');
171+
throw new RuntimeException('No editor found or specified.');
172172
});
173173

174174
Process::fromShellCommandline("$editor $file")->setTimeout(null)->setTty(true)->mustRun();
175175

176176
break;
177177
default:
178-
throw new \RuntimeException(sprintf('The action(%s) must be one of [set, get, unset, list, edit].', implode(', ', self::ACTIONS)));
178+
throw new RuntimeException(sprintf('The action(%s) must be one of [set, get, unset, list, edit].', implode(', ', self::ACTIONS)));
179179
}
180180

181181
return self::SUCCESS;

app/ConfigManager.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@
1717
use Illuminate\Contracts\Support\Arrayable;
1818
use Illuminate\Contracts\Support\Jsonable;
1919
use Illuminate\Support\Arr;
20+
use InvalidArgumentException;
21+
use JsonSerializable;
2022

2123
/**
2224
* @template TKey of array-key
2325
* @template TValue
2426
*
2527
* @see https://github.com/hassankhan/config
2628
*/
27-
class ConfigManager extends Repository implements Arrayable, Jsonable, \JsonSerializable
29+
class ConfigManager extends Repository implements Arrayable, Jsonable, JsonSerializable
2830
{
31+
/**
32+
* @var string
33+
*/
2934
public const NAME = '.ai-commit.json';
3035

3136
final public static function load(): void
@@ -46,14 +51,13 @@ public static function create(?array $items = null): self
4651

4752
public static function createFrom(...$files): self
4853
{
49-
$config = array_reduce($files, function (array $items, string $file): array {
54+
$config = array_reduce($files, static function (array $items, string $file): array {
5055
$ext = str(pathinfo($file, PATHINFO_EXTENSION));
5156
if ($ext->is('php')) {
5257
$items[] = require $file;
5358

5459
return $items;
5560
}
56-
5761
if ($ext->is('json')) {
5862
$config = json_decode(file_get_contents($file), true);
5963
if (JSON_ERROR_NONE !== json_last_error()) {
@@ -65,7 +69,7 @@ public static function createFrom(...$files): self
6569
return $items;
6670
}
6771

68-
throw new \InvalidArgumentException("Invalid argument type: `$ext`.");
72+
throw new InvalidArgumentException("Invalid argument type: `$ext`.");
6973
}, []);
7074

7175
return new self(array_replace_recursive(...$config));
@@ -91,12 +95,12 @@ public static function localPath(string $path = self::NAME): string
9195
return $cwd.($path ? DIRECTORY_SEPARATOR.$path : $path);
9296
}
9397

94-
public function toGlobal(int $options = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
98+
public function toGlobal(int $options = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE): void
9599
{
96100
$this->toFile(self::globalPath(), $options);
97101
}
98102

99-
public function toLocal(int $options = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
103+
public function toLocal(int $options = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE): void
100104
{
101105
$this->toFile(self::localPath(), $options);
102106
}
@@ -121,7 +125,7 @@ public function replaceFrom(string $file): void
121125
}
122126

123127
if (! isset($items)) {
124-
throw new \InvalidArgumentException('Unsupported config type');
128+
throw new InvalidArgumentException('Unsupported config type');
125129
}
126130

127131
$this->replace($items);
@@ -147,15 +151,13 @@ public function forget($keys): void
147151
*/
148152
public function jsonSerialize(): array
149153
{
150-
return array_map(function ($value) {
151-
if ($value instanceof \JsonSerializable) {
154+
return array_map(static function ($value) {
155+
if ($value instanceof JsonSerializable) {
152156
return $value->jsonSerialize();
153157
}
154-
155158
if ($value instanceof Jsonable) {
156159
return json_decode($value->toJson(), true);
157160
}
158-
159161
if ($value instanceof Arrayable) {
160162
return $value->toArray();
161163
}
@@ -164,12 +166,9 @@ public function jsonSerialize(): array
164166
}, $this->all());
165167
}
166168

167-
/**
168-
* @return array
169-
*/
170-
public function toArray()
169+
public function toArray(): array
171170
{
172-
return array_map(function ($value) {
171+
return array_map(static function ($value) {
173172
return $value instanceof Arrayable ? $value->toArray() : $value;
174173
}, $this->all());
175174
}

app/Contracts/ThrowableContract.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace App\Contracts;
1414

15-
interface ThrowableContract extends \Throwable
15+
use Throwable;
16+
17+
interface ThrowableContract extends Throwable
1618
{
1719
}

app/Exceptions/Handler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace App\Exceptions;
1414

1515
use Illuminate\Validation\ValidationException;
16+
use Throwable;
1617

1718
class Handler extends \Illuminate\Foundation\Exceptions\Handler
1819
{
@@ -26,7 +27,7 @@ class Handler extends \Illuminate\Foundation\Exceptions\Handler
2627
/**
2728
* {@inheritdoc}
2829
*/
29-
public function renderForConsole($output, \Throwable $e)
30+
public function renderForConsole($output, Throwable $e): void
3031
{
3132
if ($e instanceof ValidationException) {
3233
$e = new InvalidConfigException(
@@ -39,7 +40,7 @@ public function renderForConsole($output, \Throwable $e)
3940
parent::renderForConsole($output, $e);
4041
}
4142

42-
public function shouldntReport(\Throwable $e)
43+
protected function shouldntReport(Throwable $e)
4344
{
4445
if ($this->container->isProduction()) {
4546
return true;

app/Exceptions/InvalidConfigException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
namespace App\Exceptions;
1414

1515
use App\Contracts\ThrowableContract;
16+
use Exception;
1617

17-
class InvalidConfigException extends \Exception implements ThrowableContract
18+
class InvalidConfigException extends Exception implements ThrowableContract
1819
{
1920
}

app/Exceptions/InvalidJsonFileException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
namespace App\Exceptions;
1414

1515
use App\Contracts\ThrowableContract;
16+
use RuntimeException;
1617

17-
class InvalidJsonFileException extends \RuntimeException implements ThrowableContract
18+
class InvalidJsonFileException extends RuntimeException implements ThrowableContract
1819
{
1920
public static function make(string $file): self
2021
{

app/Exceptions/TaskException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
namespace App\Exceptions;
1414

1515
use App\Contracts\ThrowableContract;
16+
use Exception;
1617

17-
class TaskException extends \Exception implements ThrowableContract
18+
class TaskException extends Exception implements ThrowableContract
1819
{
1920
}

app/GeneratorManager.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,11 @@
1515
use App\Generators\OpenAIGenerator;
1616
use Illuminate\Support\Manager;
1717

18+
/**
19+
* @method \App\Contracts\GeneratorContract driver(?string $driver = null)
20+
*/
1821
class GeneratorManager extends Manager
1922
{
20-
/**
21-
* @param string|null $driver
22-
*
23-
* @return \App\Contracts\GeneratorContract
24-
*/
25-
public function driver($driver = null)
26-
{
27-
return parent::driver($driver);
28-
}
29-
3023
public function getDefaultDriver()
3124
{
3225
return $this->config->get('ai-commit.generator');

0 commit comments

Comments
 (0)