AsciiRenderer::writeBody()   C
last analyzed

Complexity

Conditions 7
Paths 15

Size

Total Lines 31
Code Lines 19

Duplication

Lines 31
Ratio 100 %

Code Coverage

Tests 24
CRAP Score 7.0031

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 31
loc 31
ccs 24
cts 25
cp 0.96
rs 6.7272
cc 7
eloc 19
nc 15
nop 1
crap 7.0031
1
<?php
2
3
namespace CL\Table\Renderer;
4
5
use CL\Table\Column;
6
use CL\Table\Table;
7
8
class AsciiRenderer implements RendererInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    private $buffer;
14
15
    /**
16
     * @var array
17
     */
18
    private $maxWidths = [];
19
20
    /**
21
     * @inheritdoc
22
     */
23 6
    public function render(Table $table, $withHeaders = true)
24
    {
25 6
        $this->maxWidths = [];
26
27 6
        if ($withHeaders) {
28 4
            $this->writeHeader($table);
29 4
        }
30
31 6
        $this->writeBody($table);
32
33 6
        return $this->buffer;
34
    }
35
36
    /**
37
     * @param string $ascii
38
     */
39 6
    protected function writeLine($ascii)
40
    {
41 6
        $this->buffer .= $ascii."\n";
42 6
    }
43
44
    /**
45
     * @param Table $table
46
     */
47 4 View Code Duplication
    private function writeHeader(Table $table)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49 4
        $line = '';
50
51 4
        foreach ($table->getColumns() as $column) {
52 4
            if (!$line) {
53 4
                $line .= '|';
54 4
            }
55
56 4
            $columnWidth = $this->getMaxColumnWidth($column);
57 4
            $value = $column->getLabel();
58 4
            $suffix = '';
59 4
            $prefix = '';
60
61 4
            if ($value) {
62 4
                $prefix = ' ';
63 4
                $missingSpaces = $columnWidth - mb_strlen($value) + mb_strlen($prefix);
64 4
                if ($missingSpaces > 0) {
65 4
                    $suffix = str_repeat(' ', $missingSpaces);
66 4
                } else {
67
                    $suffix = ' ';
68
                }
69 4
            }
70
71 4
            $line .= sprintf('%s%s%s|', $prefix, $value, $suffix);
72 4
        }
73
74 4
        if ($line) {
75 4
            $this->writeLine($line);
76 4
        }
77 4
    }
78
79
    /**
80
     * @param Table $table
81
     */
82 6 View Code Duplication
    private function writeBody(Table $table)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84 6
        foreach ($table->toArray() as $row) {
85 6
            $line = '';
86 6
            foreach ($row as $column => $value) {
87 6
                if (!$line) {
88 6
                    $line .= '|';
89 6
                }
90
91 6
                $columnWidth = $this->getMaxColumnWidth($table->getColumn($column));
92 6
                $suffix = '';
93 6
                $prefix = '';
94
95 6
                if ($value) {
96 6
                    $prefix = ' ';
97 6
                    $missingSpaces = $columnWidth - mb_strlen($value) + mb_strlen($prefix);
98 6
                    if ($missingSpaces > 0) {
99 6
                        $suffix = str_repeat(' ', $missingSpaces);
100 6
                    } else {
101
                        $suffix = ' ';
102
                    }
103 6
                }
104
105 6
                $line .= sprintf('%s%s%s|', $prefix, $value, $suffix);
106 6
            }
107
108 6
            if ($line) {
109 6
                $this->writeLine($line);
110 6
            }
111 6
        }
112 6
    }
113
114
    /**
115
     * @param Column $column
116
     */
117 6
    private function getMaxColumnWidth(Column $column)
118
    {
119 6
        if (!array_key_exists($column->getName(), $this->maxWidths)) {
120 6
            $maxWidth = mb_strlen($column->getLabel());
121
122 6
            foreach ($column->getValues() as $value) {
123 6
                $width = mb_strlen($value);
124
125 6
                if ($width > $maxWidth) {
126 2
                    $maxWidth = $width;
127 2
                }
128 6
            }
129
130 6
            $this->maxWidths[$column->getName()] = $maxWidth;
131 6
        }
132
133 6
        return $this->maxWidths[$column->getName()];
134
    }
135
}
136