Skip to content

Commit b1b30a2

Browse files
author
ityaozm@gmail.com
committed
refactor: apply phpstan
1 parent 1a7158f commit b1b30a2

39 files changed

Lines changed: 58 additions & 374 deletions

app/Clients/AbstractClient.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public function pendingRequest(?callable $callback = null, bool $clone = false):
112112
function (PendingRequest $pendingRequest): void {
113113
/** @see \Illuminate\Http\Client\Factory::createPendingRequest() */
114114
$pendingRequest
115+
/** @phpstan-ignore-next-line */
115116
->stub((fn (): Collection => $this->stubCallbacks)->call(Http::getFacadeRoot()))
116117
->preventStrayRequests(Http::preventingStrayRequests());
117118
}
@@ -284,8 +285,8 @@ private function getUserAgent(): string
284285
->implode(' ');
285286
}
286287

287-
private function makeLoggerMiddleware(?string $logger = null): callable
288-
{
289-
return Middleware::log(Log::channel($logger), new MessageFormatter(MessageFormatter::DEBUG));
290-
}
288+
// private function makeLoggerMiddleware(?string $logger = null): callable
289+
// {
290+
// return Middleware::log(Log::channel($logger), new MessageFormatter(MessageFormatter::DEBUG));
291+
// }
291292
}

app/Clients/Ernie.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,10 @@ private function completion(
131131
static function (PendingRequest $pendingRequest) use (&$rowData, $writer): PendingRequest {
132132
return $pendingRequest->withOptions([
133133
'curl' => [
134-
\CURLOPT_WRITEFUNCTION => static function ($ch, string $data) use (&$rowData, $writer): int {
134+
\CURLOPT_WRITEFUNCTION => static function (\CurlHandle $ch, string $data) use (&$rowData, $writer): int {
135135
$rowData = self::sanitizeData($data);
136+
137+
/** @var callable $writer */
136138
$writer($rowData, $ch);
137139

138140
return \strlen($data);
@@ -163,7 +165,7 @@ static function (PendingRequest $pendingRequest) use (&$rowData, $writer): Pendi
163165
}
164166

165167
return tap($response->throw(), static function (Response $response): void {
166-
if (isset($response['error_code'])) {
168+
if ($response->offsetExists('error_code')) {
167169
throw new RequestException($response);
168170
}
169171
});

app/Clients/Moonshot.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ public function chatCompletions(array $parameters, ?callable $writer = null): Re
6868
static function (PendingRequest $pendingRequest) use (&$rowData, $writer): PendingRequest {
6969
return $pendingRequest->withOptions([
7070
'curl' => [
71-
\CURLOPT_WRITEFUNCTION => static function ($ch, string $data) use (&$rowData, $writer): int {
71+
\CURLOPT_WRITEFUNCTION => static function (\CurlHandle $ch, string $data) use (&$rowData, $writer): int {
7272
// $sanitizeData = self::sanitizeData($data);
7373
// if (! str($data)->startsWith('data: [DONE]')) {
7474
// $rowData = $sanitizeData;
7575
// }
7676

7777
$rowData .= $data;
7878

79+
/** @var callable $writer */
7980
$writer($data, $ch);
8081

8182
return \strlen($data);

app/Clients/OpenAI.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,15 @@ private function completion(string $url, array $parameters, array $rules, ?calla
200200
static function (PendingRequest $pendingRequest) use (&$rowData, $writer): PendingRequest {
201201
return $pendingRequest->withOptions([
202202
'curl' => [
203-
\CURLOPT_WRITEFUNCTION => static function ($ch, string $data) use (&$rowData, $writer): int {
203+
\CURLOPT_WRITEFUNCTION => static function (\CurlHandle $ch, string $data) use (&$rowData, $writer): int {
204204
// $sanitizeData = self::sanitizeData($data);
205205
// if (! str($data)->startsWith('data: [DONE]')) {
206206
// $rowData = $sanitizeData;
207207
// }
208208

209209
$rowData .= $data;
210210

211+
/** @var callable $writer */
211212
$writer($data, $ch);
212213

213214
return \strlen($data);

app/Commands/CommitCommand.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function handle(): void
6666
->tap(function () use (&$message, $cachedDiff, $type): void {
6767
$message = retry(
6868
$this->option('retry-times'),
69-
function ($attempts) use ($cachedDiff, $type): string {
69+
function (int $attempts) use ($cachedDiff, $type): string {
7070
if (1 < $attempts) {
7171
$this->output->note('retrying...');
7272
}
@@ -226,8 +226,8 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
226226
]);
227227

228228
collect($options)
229-
->reject(static fn ($value): bool => null === $value)
230-
->each(function ($value, $name): void {
229+
->reject(static fn (mixed $value): bool => null === $value)
230+
->each(function (mixed $value, string $name): void {
231231
$this->input->setOption((string) str($name)->replace(['.', '_'], '-'), $value);
232232
});
233233
}
@@ -314,14 +314,4 @@ private function shouldntVerify(): bool
314314
{
315315
return $this->option('no-verify') || $this->configManager->get('no_verify');
316316
}
317-
318-
/**
319-
* @codeCoverageIgnore
320-
*
321-
* @noinspection PhpUnusedPrivateMethodInspection
322-
*/
323-
private function shouldVerify(): bool
324-
{
325-
return !$this->shouldntVerify();
326-
}
327317
}

app/Commands/ConfigCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function handle(ExecutableFinder $executableFinder): int
100100

101101
break;
102102
case 'list':
103-
collect($this->configManager->toDotArray())->each(function ($value, $key): void {
103+
collect($this->configManager->toDotArray())->each(function (mixed $value, string $key): void {
104104
$this->line("[<comment>$key</comment>] <info>{$this->valueToArg($value)}</info>");
105105
});
106106

app/ConfigManager.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static function create(?array $items = null): self
7272
/**
7373
* @throws \JsonException
7474
*/
75-
public static function createFrom(...$files): self
75+
public static function createFrom(string ...$files): self
7676
{
7777
return new self(self::readFrom(...$files));
7878
}
@@ -121,7 +121,7 @@ public function putLocal(int $options = \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNI
121121
public function putFile(string $file, int $options = \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE): bool|int
122122
{
123123
collect($this->toDotArray())
124-
->filter(static fn ($val): bool => !\is_scalar($val) && null !== $val)
124+
->filter(static fn (mixed $val): bool => !\is_scalar($val) && null !== $val)
125125
->keys()
126126
->push(
127127
'generators.openai.parameters.prompt',
@@ -169,7 +169,7 @@ public function forget(array|string $keys): void
169169
*/
170170
public function jsonSerialize(): array
171171
{
172-
return array_map(static function ($value) {
172+
return array_map(static function (mixed $value) {
173173
if ($value instanceof \JsonSerializable) {
174174
return $value->jsonSerialize();
175175
}
@@ -193,7 +193,7 @@ public function toDotArray(): array
193193

194194
public function toArray(): array
195195
{
196-
return array_map(static fn ($value) => $value instanceof Arrayable ? $value->toArray() : $value, $this->all());
196+
return array_map(static fn (mixed $value) => $value instanceof Arrayable ? $value->toArray() : $value, $this->all());
197197
}
198198

199199
/**
@@ -204,15 +204,15 @@ public function toArray(): array
204204
*
205205
* @throws \JsonException
206206
*/
207-
public function toJson($options = \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE): string
207+
public function toJson(mixed $options = \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE): string
208208
{
209209
return json_encode($this->jsonSerialize(), $options);
210210
}
211211

212212
/**
213213
* @throws \JsonException
214214
*/
215-
public static function readFrom(...$files): array
215+
public static function readFrom(string ...$files): array
216216
{
217217
$configurations = array_reduce($files, static function (array $configurations, string $file): array {
218218
$ext = str(pathinfo($file, \PATHINFO_EXTENSION));

app/GeneratorManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ public function getDefaultDriver(): string
4343
* @noinspection MissingParentCallInspection
4444
* @noinspection PhpMissingParentCallCommonInspection
4545
*/
46-
protected function createDriver($driver): GeneratorContract
46+
protected function createDriver(mixed $driver): GeneratorContract
4747
{
4848
if (isset($this->customCreators[$driver])) {
4949
return $this->callCustomCreator($driver);
5050
}
5151

52+
/** @var array $config */
5253
$config = $this->config->get("ai-commit.generators.$driver");
5354

5455
$studlyName = Str::studly($config['driver'] ?? $driver);
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
use Symfony\Component\Console\Exception\InvalidArgumentException;
2020
use Symfony\Component\Console\Helper\HelperInterface;
2121
use Symfony\Component\Console\Helper\HelperSet;
22+
use Symfony\Component\Console\Helper\ProcessHelper;
2223
use Symfony\Component\Console\Output\OutputInterface;
2324
use Symfony\Component\Process\Exception\ProcessFailedException;
2425
use Symfony\Component\Process\Process;
2526

26-
abstract class Generator implements GeneratorContract
27+
abstract class AbstractGenerator implements GeneratorContract
2728
{
2829
protected OutputStyle $output;
2930
protected HelperSet $helperSet;
@@ -76,6 +77,8 @@ protected function runProcess(
7677

7778
/**
7879
* @throws InvalidArgumentException if the helper is not defined
80+
*
81+
* @return ProcessHelper
7982
*/
8083
protected function getHelper(string $name): HelperInterface
8184
{
@@ -100,9 +103,9 @@ protected function ensureWithOptions(array $command): array
100103
protected function hydratedOptions(): array
101104
{
102105
return collect($this->config['options'] ?? [])
103-
->map(static fn ($value): string => (string) str(urldecode(http_build_query([$option = 'option' => $value])))->after("$option="))
106+
->map(static fn (mixed $value): string => (string) str(urldecode(http_build_query([$option = 'option' => $value])))->after("$option="))
104107
->filter()
105-
->map(static fn ($value, string $option): array => [$option, $value])
108+
->map(static fn (mixed $value, string $option): array => [$option, $value])
106109
->flatten()
107110
->all();
108111
}

app/Generators/BitoCliGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use Symfony\Component\Process\Process;
1717

18-
final class BitoCliGenerator extends Generator
18+
final class BitoCliGenerator extends AbstractGenerator
1919
{
2020
public function generate(string $prompt): string
2121
{

0 commit comments

Comments
 (0)