Skip to content

Commit

Permalink
Static fix
Browse files Browse the repository at this point in the history
  • Loading branch information
guanguans committed Jun 20, 2023
1 parent 12bc272 commit 781bbb4
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 23 deletions.
25 changes: 18 additions & 7 deletions app/Commands/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,17 @@ public function __destruct()

/**
* @psalm-suppress UndefinedInterfaceMethod
* @noinspection ReturnTypeCanBeDeclaredInspection
* @noinspection PhpMissingReturnTypeInspection
* @noinspection PhpMissingParentCallCommonInspection
*/
public function isEnabled()
{
return ! $this->laravel->isProduction();
}

/**
* {@inheritDoc}
* @throws \JsonException
*/
public function handle(): void
{
Expand All @@ -107,8 +110,10 @@ public function run(InputInterface $input, OutputInterface $output): int

/**
* Builds the application into a single file.
*
* @throws \JsonException
*/
private function build(string $name): BuildCommand
private function build(string $name): self
{
/*
* We prepare the application for a build, moving it to production. Then,
Expand All @@ -130,8 +135,9 @@ private function build(string $name): BuildCommand
* @psalm-suppress UndefinedInterfaceMethod
*
* @noinspection PhpVoidFunctionResultUsedInspection
* @noinspection DisconnectedForeachInstructionInspection
*/
private function compile(string $name): BuildCommand
private function compile(string $name): self
{
if (! File::exists($this->app->buildsPath())) {
File::makeDirectory($this->app->buildsPath());
Expand Down Expand Up @@ -161,7 +167,7 @@ private function compile(string $name): BuildCommand
$progressBar->advance();

if ($this->option('verbose')) {
$process::OUT === $type ? $this->info("$data") : $this->error("$data");
$process::OUT === $type ? $this->info((string) $data) : $this->error((string) $data);
}
}

Expand All @@ -180,8 +186,11 @@ private function compile(string $name): BuildCommand

/**
* @noinspection DebugFunctionUsageInspection
* @noinspection UsingInclusionReturnValueInspection
*
* @throws \JsonException
*/
private function prepare(): BuildCommand
private function prepare(): self
{
$configFile = $this->app->configPath('app.php');
self::$config = File::get($configFile);
Expand Down Expand Up @@ -219,7 +228,7 @@ static function () use ($configFile, $config): void {
return $this;
}

private function clear(): BuildCommand
private function clear(): self
{
File::put($this->app->configPath('app.php'), self::$config);

Expand Down Expand Up @@ -259,13 +268,15 @@ private function getTimeout(): ?float

/**
* Enable and listen to async signals for the process.
*
* @noinspection PhpComposerExtensionStubsInspection
*/
private function listenForSignals(): void
{
pcntl_async_signals(true);

pcntl_signal(SIGINT, function (): void {
if (null !== static::$config) {
if (null !== self::$config) {
$this->clear();
}

Expand Down
6 changes: 4 additions & 2 deletions app/Commands/CommitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace App\Commands;

use App\ConfigManager;
use App\Exceptions\TaskException;
use App\GeneratorManager;
use App\Support\JsonFixer;
Expand Down Expand Up @@ -97,7 +98,7 @@ function ($attempts) use ($stagedDiff): string {
$this->newLine();
}, 'generating...');

$this->task('2. Choosing commit message', function () use ($messages, &$message): void {
$this->task('2. Choosing commit message', function () use (&$message, $messages): void {
$message = collect(json_decode($messages, true, 512, JSON_THROW_ON_ERROR))
->tap(function (Collection $messages): void {
$this->newLine(2);
Expand Down Expand Up @@ -130,6 +131,7 @@ function ($attempts) use ($stagedDiff): string {
}

/**
* @codeCoverageIgnore
* @psalm-suppress InvalidArgument
*/
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
Expand All @@ -156,7 +158,7 @@ public function schedule(Schedule $schedule): void
protected function configure(): void
{
$this->setDefinition([
new InputArgument('path', InputArgument::OPTIONAL, 'The working directory', $this->configManager::localPath('')),
new InputArgument('path', InputArgument::OPTIONAL, 'The working directory', ConfigManager::localPath('')),
new InputOption('commit-options', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Append options for the `git commit` command', $this->configManager->get('commit_options', [])),
new InputOption('diff-options', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Append options for the `git diff` command', $this->configManager->get('diff_options', [])),
new InputOption('generator', 'g', InputOption::VALUE_REQUIRED, 'Specify generator name', $this->configManager->get('generator')),
Expand Down
18 changes: 13 additions & 5 deletions app/Commands/ConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class ConfigCommand extends Command
/**
* @var string[]
*/
protected $editors = ['editor', 'vim', 'vi', 'nano', 'pico', 'ed'];
protected const EDITORS = ['editor', 'vim', 'vi', 'nano', 'pico', 'ed'];

/**
* @var string
Expand All @@ -59,6 +59,9 @@ public function __construct()
parent::__construct();
}

/**
* @throws \JsonException
*/
public function handle(): int
{
$file = value(function () {
Expand Down Expand Up @@ -123,7 +126,7 @@ public function handle(): int
return 'notepad';
}

foreach ($this->editors as $editor) {
foreach (self::EDITORS as $editor) {
if (exec("which $editor")) {
return $editor;
}
Expand All @@ -145,6 +148,7 @@ public function handle(): int
}

/**
* @codeCoverageIgnore
* @psalm-suppress InvalidArgument
*/
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
Expand All @@ -162,7 +166,7 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
}

if ($input->mustSuggestOptionValuesFor('editor')) {
$suggestions->suggestValues($this->editors);
$suggestions->suggestValues(self::EDITORS);
}
}

Expand All @@ -186,15 +190,18 @@ protected function configure(): void
]);
}

/**
* @throws \JsonException
*/
protected function initialize(InputInterface $input, OutputInterface $output): void
{
if (! file_exists($this->configManager::globalPath())) {
if (! file_exists(ConfigManager::globalPath())) {
$this->configManager->putGlobal();
}
}

/**
* @return mixed
* @throws \JsonException
*/
protected function argToValue(string $arg)
{
Expand Down Expand Up @@ -225,6 +232,7 @@ protected function argToValue(string $arg)
* @param mixed $value
*
* @noinspection DebugFunctionUsageInspection
* @noinspection JsonEncodingApiUsageInspection
*/
protected function valueToArg($value): string
{
Expand Down
15 changes: 14 additions & 1 deletion app/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public static function load(): void
resolve('config')->set('ai-commit', self::create());
}

/**
* @throws \JsonException
*/
public static function create(?array $items = null): self
{
if (\is_array($items)) {
Expand All @@ -62,6 +65,9 @@ public static function create(?array $items = null): self
);
}

/**
* @throws \JsonException
*/
public static function createFrom(...$files): self
{
return new self(self::readFrom(...$files));
Expand All @@ -71,7 +77,7 @@ public static function globalPath(string $path = self::NAME): string
{
$path = $path ? \DIRECTORY_SEPARATOR.$path : $path;
if (windows_os()) {
return sprintf('C:\\Users\\%s', get_current_user()).$path;
return sprintf('C:\\Users\\%s', get_current_user()).$path; // @codeCoverageIgnore
}

return exec('cd ~; pwd').$path;
Expand Down Expand Up @@ -133,6 +139,9 @@ public function putFile(string $file, int $options = JSON_PRETTY_PRINT | JSON_UN
return file_put_contents($file, $this->toJson($options));
}

/**
* @throws \JsonException
*/
public function replaceFrom(string $file): void
{
$this->replace(self::readFrom($file));
Expand Down Expand Up @@ -193,12 +202,16 @@ public function toArray(): array
* {@inheritDoc}
*
* @throws \JsonException
* @noinspection JsonEncodingApiUsageInspection
*/
public function toJson($options = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE): string
{
return json_encode($this->jsonSerialize(), $options);
}

/**
* @throws \JsonException
*/
public static function readFrom(...$files): array
{
$configurations = array_reduce($files, static function (array $configurations, string $file): array {
Expand Down
3 changes: 3 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function renderForConsole($output, \Throwable $e): void

/**
* {@inheritDoc}
*
* @noinspection ReturnTypeCanBeDeclaredInspection
* @noinspection PhpMissingReturnTypeInspection
*/
protected function shouldntReport(\Throwable $e)
{
Expand Down
5 changes: 4 additions & 1 deletion app/Generators/OpenAIGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ protected static function extractCompletion($response): string
return Arr::get($response, 'choices.0.text', '');
}

/**
* @noinspection JsonEncodingApiUsageInspection
*/
protected function getWriter(?string &$messages): \Closure
{
return function (string $data) use (&$messages): void {
Expand All @@ -95,7 +98,7 @@ protected function getWriter(?string &$messages): \Closure
}

// (正常|错误|流)响应
$rowResponse = (array) json_decode($this->openAI::sanitizeData($data), true);
$rowResponse = (array) json_decode(OpenAI::sanitizeData($data), true);
$messages .= $text = static::extractCompletion($rowResponse);
$this->output->write($text);
};
Expand Down
1 change: 1 addition & 0 deletions app/Macros/StrMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class StrMacro
{
/**
* @psalm-suppress UnusedFunctionCall
* @noinspection BadExceptionsProcessingInspection
*/
public static function isJson(): \Closure
{
Expand Down
3 changes: 3 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use App\Macros\StringableMacro;
use App\Macros\StrMacro;
use Illuminate\Console\OutputStyle;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
Expand Down Expand Up @@ -50,6 +51,8 @@ public function boot(): void

/**
* {@inheritDoc}
*
* @throws BindingResolutionException
*/
public function register(): void
{
Expand Down
12 changes: 6 additions & 6 deletions app/Support/FoundationSDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,24 @@ public function __construct(array $config)
/**
* @psalm-suppress UnusedClosureParam
*/
public function ddRequestData()
public function ddRequestData(): self
{
return $this->tapDefaultPendingRequest(static function (PendingRequest $pendingRequest): void {
$pendingRequest->beforeSending(static function (Request $request, array $options): void {
VarDumper::dump($options['laravel_data']);
exit(1);
VarDumper::dump($options['laravel_data']); // @codeCoverageIgnore
exit(1); // @codeCoverageIgnore
});
});
}

/**
* @psalm-suppress UnusedClosureParam
*/
public function dumpRequestData()
public function dumpRequestData(): self
{
return $this->tapDefaultPendingRequest(static function (PendingRequest $pendingRequest): void {
$pendingRequest->beforeSending(static function (Request $request, array $options): void {
VarDumper::dump($options['laravel_data']);
VarDumper::dump($options['laravel_data']); // @codeCoverageIgnore
});
});
}
Expand All @@ -88,7 +88,7 @@ public function buildLogMiddleware(?LoggerInterface $logger = null, ?MessageForm
return Middleware::log($logger, $formatter, $logLevel);
}

public function tapDefaultPendingRequest(callable $callback)
public function tapDefaultPendingRequest(callable $callback): self
{
$this->defaultPendingRequest = tap($this->defaultPendingRequest, $callback);

Expand Down
3 changes: 3 additions & 0 deletions app/Support/JsonFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ private function trim(string $json): array
return $match;
}

/**
* @noinspection JsonEncodingApiUsageInspection
*/
private function isValid(string $json): bool
{
/** @psalm-suppress UnusedFunctionCall */
Expand Down
Loading

0 comments on commit 781bbb4

Please sign in to comment.