ListenersRenderer::render()   B
last analyzed

Complexity

Conditions 10
Paths 11

Size

Total Lines 47
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 47
rs 7.6666
cc 10
nc 11
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Renderer\ConsoleRenderer\Renderer;
6
7
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter;
8
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer;
9
10
class ListenersRenderer implements Renderer
11
{
12
    private int $property;
13
    private string $title;
14
15
    public function __construct(int $property, string $title)
16
    {
17
        $this->property = $property;
18
        $this->title = $title;
19
    }
20
21
    public function render(Formatter $formatter, array $schema, string $role): ?string
22
    {
23
        if (! isset($schema[$this->property])) {
24
            return null;
25
        }
26
27
        $listeners = (array)($schema[$this->property] ?? []);
28
29
        if ($listeners === []) {
30
            return null;
31
        }
32
33
        $rows = [
34
            \sprintf('%s:', $formatter->title($this->title)),
35
        ];
36
37
        foreach ($listeners as $definition) {
38
            $data = (array)$definition;
39
40
            // Listener class
41
            $class = \is_string($data[0] ?? null)
42
                ? $formatter->typecast($data[0])
43
                : $formatter->error('undefined');
44
            $rows[] = \sprintf('%s%s', $formatter->title(' '), $class);
45
46
            // Listener arguments
47
            $args = $data[1] ?? null;
48
            if ($args === null) {
49
                continue;
50
            }
51
            if (\is_array($args)) {
52
                foreach ($args as $key => $value) {
53
                    $row = $formatter->title(' ') . ' - ';
54
                    if (\is_string($key)) {
55
                        $row .= $formatter->property($key) . ' : ';
56
                    }
57
                    $row .= $formatter->info($this->printValue($value, $formatter));
58
                    $rows[] = $row;
59
                }
60
            } elseif (is_string($args)) {
61
                $rows[] = $formatter->title(' ') . ' - ' . $formatter->info($this->printValue($args, $formatter));
62
            } else {
63
                $rows[] = $formatter->typecast($this->printValue($data, $formatter));
64
            }
65
        }
66
67
        return \implode($formatter::LINE_SEPARATOR, $rows);
68
    }
69
70
    /**
71
     * @param mixed $value
72
     */
73
    private function printValue($value, Formatter $formatter): string
74
    {
75
        $data = \trim(\var_export($value, true), '\'');
76
        $data = \array_map(
77
            static fn (string $row): string => $formatter->title(' ') . $row,
78
            \explode("\n", $data)
79
        );
80
81
        return \ltrim(
82
            \implode("\n", $data)
83
        );
84
    }
85
}
86