Passed
Push — main ( ed779b...d01439 )
by Nils
05:39 queued 02:41
created

ExplainCommand::doExecute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 24
rs 9.9
1
<?php
2
3
namespace Startwind\Forrest\CliCommand\Ai;
4
5
use Startwind\Forrest\CliCommand\Command\CommandCommand;
6
use Startwind\Forrest\Util\OutputHelper;
7
use Symfony\Component\Console\Command\Command as SymfonyCommand;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Startwind\Forrest\Command\Answer\Answer;
13
14
class ExplainCommand extends CommandCommand
15
{
16
    protected static $defaultName = 'ai:explain';
17
    protected static $defaultDescription = 'Suggest an answer to a question.';
18
19
    protected function configure(): void
20
    {
21
        $this->addArgument('prompt', InputArgument::IS_ARRAY, 'The prompt you want to have explained.');
22
        $this->setAliases(['explain']);
23
24
        parent::configure();
25
    }
26
27
    protected function doExecute(InputInterface $input, OutputInterface $output): int
28
    {
29
        $this->enrichRepositories();
30
31
        \Startwind\Forrest\Output\OutputHelper::renderHeader($output);
32
33
        $prompt = implode(' ', $input->getArgument('prompt'));
34
35
        OutputHelper::writeInfoBox($output, [
36
            'Explanation of: "' . $prompt. '"'
37
        ]);
38
39
        $answers = $this->getRepositoryCollection()->explain($prompt);
40
41
        foreach ($answers as $repositoryName => $repoAnswers) {
42
            foreach ($repoAnswers as $answer) {
43
                /** @var Answer $answer */
44
                $output->writeln(OutputHelper::indentText($this->formatCliText($answer->getAnswer())));
45
            }
46
        }
47
48
        $output->writeln(['', '']);
49
50
        return SymfonyCommand::SUCCESS;
51
    }
52
53
    private function formatCliText(string $text): string
54
    {
55
        preg_match_all('#```shell((.|\n)*?)```#', $text, $matches);
56
57
        if (count($matches[1]) == 1) {
58
            $shell = $matches[1][0];
59
60
            $shellNew = implode("\n", OutputHelper::indentText(trim($shell), 2, 100, ' | '));
61
62
            $text = str_replace($matches[0][0], $shellNew, $text);
63
        }
64
65
        preg_match_all('#`(.*)`#', $text, $matches);
66
67
        if (count($matches[1]) > 0) {
68
69
            foreach ($matches[0] as $key => $match) {
70
                $text = str_replace($match, '<options=bold>' . $matches[1][$key] . '</>', $text);
71
            }
72
        }
73
74
        return $text;
75
    }
76
}
77