Passed
Branch master (6713c8)
by Alexander
02:29
created

CommandCollector   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 92.68%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 42
c 2
b 0
f 0
dl 0
loc 79
ccs 38
cts 41
cp 0.9268
rs 10
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 3 1
A getCollected() 0 3 1
A getIndexData() 0 26 5
B collect() 0 32 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
use Symfony\Component\Console\Event\ConsoleCommandEvent;
8
use Symfony\Component\Console\Event\ConsoleErrorEvent;
9
use Symfony\Component\Console\Event\ConsoleEvent;
10
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
11
use function get_class;
12
use function is_object;
13
14
final class CommandCollector implements CollectorInterface, IndexCollectorInterface
15
{
16
    use CollectorTrait;
17
18
    private array $commands = [];
19
20 2
    public function getCollected(): array
21
    {
22 2
        return $this->commands;
23
    }
24
25 2
    public function collect(object $event): void
26
    {
27 2
        if (!is_object($event) || !$this->isActive()) {
28 1
            return;
29
        }
30
31 1
        if ($event instanceof ConsoleErrorEvent) {
32 1
            $this->commands[get_class($event)] = [
33 1
                'command' => $event->getCommand(),
34 1
                '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

34
                'input' => $event->getInput()->/** @scrutinizer ignore-call */ __toString(),
Loading history...
35 1
                'output' => $event->getOutput()->fetch(),
0 ignored issues
show
Bug introduced by
The method fetch() does not exist on Symfony\Component\Console\Output\OutputInterface. It seems like you code against a sub-type of Symfony\Component\Console\Output\OutputInterface such as Symfony\Component\Console\Output\BufferedOutput or Symfony\Component\Consol...put\TrimmedBufferOutput or Yiisoft\Yii\Console\Output\ConsoleBufferedOutput or Yiisoft\Yii\Console\Output\ConsoleBufferedOutput. ( Ignorable by Annotation )

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

35
                'output' => $event->getOutput()->/** @scrutinizer ignore-call */ fetch(),
Loading history...
36 1
                'error' => $event->getError()->getMessage(),
37 1
                'exitCode' => $event->getExitCode(),
38
            ];
39 1
            return;
40
        }
41
42 1
        if ($event instanceof ConsoleTerminateEvent) {
43 1
            $this->commands[get_class($event)] = [
44 1
                'command' => $event->getCommand(),
45 1
                'input' => $event->getInput()->__toString(),
46 1
                'output' => $event->getOutput()->fetch(),
47 1
                'exitCode' => $event->getExitCode(),
48
            ];
49 1
            return;
50
        }
51
52 1
        if ($event instanceof ConsoleEvent) {
53 1
            $this->commands[get_class($event)] = [
54 1
                'command' => $event->getCommand(),
55 1
                'input' => $event->getInput()->__toString(),
56 1
                'output' => $event->getOutput()->fetch(),
57
            ];
58
        }
59 1
    }
60
61 1
    public function getIndexData(): array
62
    {
63
        $eventTypes = [
64 1
            ConsoleErrorEvent::class,
65
            ConsoleTerminateEvent::class,
66
            ConsoleCommandEvent::class,
67
        ];
68
69 1
        $command = null;
70 1
        foreach ($eventTypes as $eventType) {
71 1
            if (!array_key_exists($eventType, $this->commands)) {
72
                continue;
73
            }
74
75 1
            $command = $this->commands[$eventType];
76 1
            break;
77
        }
78
79 1
        if ($command === null) {
80
            $types = array_keys($this->commands);
81
            throw new \RuntimeException('Unsupported event type encountered among "' . implode('", "', $types) . '".');
82
        }
83
84
        return [
85 1
            'command' => $command['input'],
86 1
            'commandClass' => $command['command'] !== null ? get_class($command['command']) : null,
87
        ];
88
    }
89
90 1
    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...
91
    {
92 1
        $this->commands = [];
93 1
    }
94
}
95