Passed
Pull Request — master (#205)
by Sergei
03:36 queued 49s
created

CommandCollector::fetchOutput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector\Console;
6
7
use RuntimeException;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Event\ConsoleCommandEvent;
10
use Symfony\Component\Console\Event\ConsoleErrorEvent;
11
use Symfony\Component\Console\Event\ConsoleEvent;
12
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Yiisoft\Yii\Console\Output\ConsoleBufferedOutput;
15
use Yiisoft\Yii\Debug\Collector\CollectorInterface;
16
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
17
use Yiisoft\Yii\Debug\Collector\IndexCollectorInterface;
18
19
use function is_object;
20
21
final class CommandCollector implements CollectorInterface, IndexCollectorInterface
22
{
23
    use CollectorTrait;
24
25
    /**
26
     * Let -1 mean that it was not set during the process.
27
     */
28
    private const UNDEFINED_EXIT_CODE = -1;
29
    private array $commands = [];
30
31
    public function getCollected(): array
32
    {
33
        return $this->commands;
34
    }
35
36
    public function collect(ConsoleEvent|ConsoleErrorEvent|ConsoleTerminateEvent $event): void
37
    {
38
        if (!is_object($event) || !$this->isActive()) {
39
            return;
40
        }
41
42
        $command = $event->getCommand();
43
44
        if ($event instanceof ConsoleErrorEvent) {
45
            $this->commands[$event::class] = [
46
                'name' => $event->getInput()->getFirstArgument() ?? '',
47
                'command' => $command,
48
                'input' => $event->getInput()->__toString(),
0 ignored issues
show
Bug introduced by
The method __toString() does not exist on Symfony\Component\Console\Input\InputInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Consol...treamableInputInterface or Symfony\Component\Console\Input\Input or Symfony\Component\Console\Input\Input. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
                'input' => $event->getInput()->/** @scrutinizer ignore-call */ __toString(),
Loading history...
49
                'output' => $this->fetchOutput($event->getOutput()),
50
                'error' => $event->getError()->getMessage(),
51
                'exitCode' => $event->getExitCode(),
52
            ];
53
54
            return;
55
        }
56
57
        if ($event instanceof ConsoleTerminateEvent) {
58
            $this->commands[$event::class] = [
59
                'name' => $command->getName(),
60
                'command' => $command,
61
                'input' => $event->getInput()->__toString(),
62
                'output' => $this->fetchOutput($event->getOutput()),
63
                'exitCode' => $event->getExitCode(),
64
            ];
65
            return;
66
        }
67
68
        if ($event instanceof ConsoleEvent) {
0 ignored issues
show
introduced by
$event is always a sub-type of Symfony\Component\Console\Event\ConsoleEvent.
Loading history...
69
            $this->commands[$event::class] = [
70
                'name' => $command->getName(),
71
                'command' => $command,
72
                'input' => $event->getInput()->__toString(),
73
                'output' => $this->fetchOutput($event->getOutput()),
74
                'arguments' => $command->getDefinition()->getArguments(),
75
                'options' => $command->getDefinition()->getOptions(),
76
            ];
77
        }
78
    }
79
80
    public function getIndexData(): array
81
    {
82
        $eventTypes = [
83
            ConsoleErrorEvent::class,
84
            ConsoleTerminateEvent::class,
85
            ConsoleCommandEvent::class,
86
        ];
87
88
        $commandEvent = null;
89
        foreach ($eventTypes as $eventType) {
90
            if (!array_key_exists($eventType, $this->commands)) {
91
                continue;
92
            }
93
94
            $commandEvent = $this->commands[$eventType];
95
            break;
96
        }
97
98
        if ($commandEvent === null) {
99
            $types = array_keys($this->commands);
100
            throw new RuntimeException(
101
                sprintf(
102
                    'Unsupported event type encountered among "%s". Supported only "%s"',
103
                    implode('", "', $types),
104
                    implode('", "', $eventTypes),
105
                )
106
            );
107
        }
108
109
        return [
110
            'command' => [
111
                'name' => $commandEvent['name'],
112
                'class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null,
113
                'input' => $commandEvent['input'],
114
                'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE,
115
            ],
116
        ];
117
    }
118
119
    private function reset(): void
0 ignored issues
show
Unused Code introduced by
The method reset() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
120
    {
121
        $this->commands = [];
122
    }
123
124
    private function fetchOutput(OutputInterface $output): ?string
125
    {
126
        return $output instanceof ConsoleBufferedOutput ? $output->fetch() : null;
127
    }
128
}
129