Skip to content

Commit fde8776

Browse files
committed
refactor(app): use custom exceptions and update driver creation
- Replace generic InvalidArgumentException with custom one - Update GeneratorManager to prioritize method over class for driver creation
1 parent 0cb665a commit fde8776

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

app/Commands/BuildCommand.php

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

1313
namespace App\Commands;
1414

15+
use App\Exceptions\InvalidArgumentException;
1516
use Illuminate\Console\Application as Artisan;
1617
use Illuminate\Support\Facades\File;
1718
use LaravelZero\Framework\Commands\Command;
@@ -258,7 +259,7 @@ private function getBinary(): string
258259
private function getTimeout(): ?float
259260
{
260261
if (! is_numeric($this->option('timeout'))) {
261-
throw new \InvalidArgumentException('The timeout value must be a number.');
262+
throw new InvalidArgumentException('The timeout value must be a number.');
262263
}
263264

264265
$timeout = (float) $this->option('timeout');

app/GeneratorManager.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace App;
1414

15+
use App\Exceptions\InvalidArgumentException;
1516
use App\Generators\BitoGenerator;
1617
use App\Generators\OpenAIChatGenerator;
1718
use App\Generators\OpenAIGenerator;
@@ -45,17 +46,17 @@ protected function createDriver($driver)
4546
}
4647

4748
$config = $this->config->get("ai-commit.generators.$driver");
48-
$class = sprintf('App\Generators\%sGenerator', Str::studly($driver));
49-
if (class_exists($class)) {
50-
return new $class($config);
51-
}
49+
$studlyName = Str::studly($driver);
5250

53-
$method = 'create'.Str::studly($driver).'Driver';
54-
if (method_exists($this, $method)) {
51+
if (method_exists($this, $method = "create{$studlyName}Driver")) {
5552
return $this->{$method}($config);
5653
}
5754

58-
throw new \InvalidArgumentException("Driver [$driver] not supported.");
55+
if (class_exists($class = "App\\Generators\\{$studlyName}Generator")) {
56+
return new $class($config);
57+
}
58+
59+
throw new InvalidArgumentException("Driver [$driver] not supported.");
5960
}
6061

6162
private function createOpenAIDriver(array $config): OpenAIGenerator

app/Support/JsonFixer.php

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

1313
namespace App\Support;
1414

15+
use App\Exceptions\RuntimeException;
16+
1517
/**
1618
* This file is modified from https://github.com/adhocore/php-json-fixer.
1719
*/
@@ -276,7 +278,7 @@ private function fixOrFail(string $json): string
276278
return $json;
277279
}
278280

279-
throw new \RuntimeException(sprintf('Could not fix JSON (tried padding `%s`)', substr($tmpJson, $length)));
281+
throw new RuntimeException(sprintf('Could not fix JSON (tried padding `%s`)', substr($tmpJson, $length)));
280282
}
281283

282284
private function padLiteral(string $tmpJson): string

0 commit comments

Comments
 (0)