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

AskCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 75
rs 10
c 1
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A formatCliText() 0 22 4
A doExecute() 0 37 5
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
use Symfony\Component\Console\Question\Question;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Question\Question 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...
14
15
class AskCommand extends CommandCommand
16
{
17
    protected static $defaultName = 'ai:ask';
18
    protected static $defaultDescription = 'Suggest an answer to a question.';
19
20
    protected function configure(): void
21
    {
22
        $this->addArgument('question', InputArgument::IS_ARRAY, 'The question you want to have answered.', []);
23
        $this->setAliases(['ask']);
24
        $this->addOption('force', null, InputOption::VALUE_NONE, 'Run the command without asking for permission.');
25
26
        parent::configure();
27
    }
28
29
    protected function doExecute(InputInterface $input, OutputInterface $output): int
30
    {
31
        $this->enrichRepositories();
32
33
        \Startwind\Forrest\Output\OutputHelper::renderHeader($output);
34
35
        $aiQuestion = implode(' ', $input->getArgument('question'));
36
37
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $questionHelper */
38
        $questionHelper = $this->getHelper('question');
39
40
        if (!$aiQuestion) {
41
            OutputHelper::writeInfoBox($output, ["Hi, I am Forrest, your AI command line helper. How can I help you?"]);
42
            $output->writeln('');
43
            $aiQuestion = $questionHelper->ask($input, $output, new Question('  Your question: '));
44
            $output->writeln(['', '']);
45
        } else {
46
            OutputHelper::writeInfoBox($output, [
47
                ucfirst($aiQuestion)
48
            ]);
49
        }
50
51
        $answers = $this->getRepositoryCollection()->ask($aiQuestion);
52
53
        foreach ($answers as $repositoryName => $repoAnswers) {
54
            foreach ($repoAnswers as $answer) {
55
                /** @var Answer $answer */
56
                $output->writeln(OutputHelper::indentText($this->formatCliText($answer->getAnswer())));
57
                $command = $answer->getCommand();
58
59
                if ($command->getPrompt()) {
60
                    return $this->runCommand($answer->getCommand(), []);
61
                }
62
            }
63
        }
64
65
        return SymfonyCommand::SUCCESS;
66
    }
67
68
    private function formatCliText(string $text): string
69
    {
70
        preg_match_all('#```shell((.|\n)*?)```#', $text, $matches);
71
72
        if (count($matches[1]) == 1) {
73
            $shell = $matches[1][0];
74
75
            $shellNew = implode("\n", OutputHelper::indentText(trim($shell), 2, 100, ' | '));
76
77
            $text = str_replace($matches[0][0], $shellNew, $text);
78
        }
79
80
        preg_match_all('#`(.*)`#', $text, $matches);
81
82
        if (count($matches[1]) > 0) {
83
84
            foreach ($matches[0] as $key => $match) {
85
                $text = str_replace($match, '<options=bold>' . $matches[1][$key] . '</>', $text);
86
            }
87
        }
88
89
        return $text;
90
    }
91
}
92