AsciiRenderer   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 128
Duplicated Lines 48.44 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 1
cbo 2
dl 62
loc 128
ccs 69
cts 71
cp 0.9718
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 12 2
A writeLine() 0 4 1
B writeHeader() 31 31 6
C writeBody() 31 31 7
A getMaxColumnWidth() 0 18 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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