Passed
Pull Request — master (#144)
by Dmitriy
02:38
created

CommandCollector::getIndexData()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0987

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 22
c 3
b 0
f 0
dl 0
loc 34
ccs 11
cts 17
cp 0.6471
rs 9.2568
cc 5
nc 9
nop 0
crap 6.0987
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
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
14
use function get_class;
15
use function is_object;
16
17
final class CommandCollector implements CollectorInterface, IndexCollectorInterface
18
{
19
    use CollectorTrait;
20
21
    private array $commands = [];
22
23 2
    public function getCollected(): array
24
    {
25 2
        return $this->commands;
26
    }
27
28 2
    public function collect(object $event): void
29
    {
30 2
        if (!is_object($event) || !$this->isActive()) {
31 1
            return;
32
        }
33
34 1
        if ($event instanceof ConsoleErrorEvent) {
35 1
            $this->commands[get_class($event)] = [
36 1
                'name' => $event->getInput()->getFirstArgument() ?? '',
37 1
                'command' => $event->getCommand(),
38 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

38
                'input' => $event->getInput()->/** @scrutinizer ignore-call */ __toString(),
Loading history...
39 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

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