TableRenderer   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 10.23 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 14
c 3
b 0
f 0
lcom 1
cbo 6
dl 9
loc 88
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaders() 0 3 1
A handles() 0 3 1
A headElements() 6 12 4
A __construct() 0 4 1
A renderHeaders() 0 7 2
A renderRows() 3 12 3
A render() 0 11 2

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
namespace rtens\domin\delivery\web\renderers\tables;
3
4
use rtens\domin\delivery\RendererRegistry;
5
use rtens\domin\delivery\web\Element;
6
use rtens\domin\delivery\web\renderers\link\LinkPrinter;
7
use rtens\domin\delivery\web\WebRenderer;
8
use watoki\reflect\Property;
9
10
class TableRenderer implements WebRenderer {
11
12
    /** @var RendererRegistry */
13
    private $renderers;
14
15
    /** @var LinkPrinter */
16
    private $printer;
17
18
    public function __construct(RendererRegistry $renderers, LinkPrinter $printer) {
19
        $this->renderers = $renderers;
20
        $this->printer = $printer;
21
    }
22
23
    /**
24
     * @param mixed $value
25
     * @return bool
26
     */
27
    public function handles($value) {
28
        return $value instanceof Table;
29
    }
30
31
    /**
32
     * @param Table $value
33
     * @return mixed
34
     */
35
    public function render($value) {
36
        $rows = $this->renderRows($value);
37
38
        if (!$rows) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rows of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
39
            return null;
40
        };
41
42
        return (string)new Element('table', ['class' => 'table table-striped'], array_merge([
43
            new Element('thead', [], [new Element('tr', [], $this->renderHeaders($value))])
44
        ], $rows));
45
    }
46
47
    private function renderHeaders($table) {
48
        $headers = [new Element('th', ['width' => '1'])];
49
        foreach ($this->getHeaders($table) as $caption) {
50
            $headers[] = new Element('th', [], [$caption]);
51
        }
52
        return $headers;
53
    }
54
55
    /**
56
     * @param Table $table
57
     * @return array
58
     * @throws \Exception
59
     */
60
    private function renderRows($table) {
61
        $rows = [];
62
        foreach ($table->getItems() as $item) {
63
            $row = [new Element('td', [], $this->printer->createDropDown($item))];
64 View Code Duplication
            foreach ($table->getCells($item) as $cell) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
65
                $row[] = new Element('td', [], [$this->renderers->getRenderer($cell)->render($cell)]);
66
            }
67
68
            $rows[] = new Element('tr', [], $row);
69
        }
70
        return $rows;
71
    }
72
73
    /**
74
     * @param Table $value
75
     * @return array|Element[]
76
     */
77
    public function headElements($value) {
78
        $elements = [];
79
        foreach ($value->getItems() as $item) {
80 View Code Duplication
            foreach ($value->getCells($item) as $cell) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
81
                $renderer = $this->renderers->getRenderer($cell);
82
                if ($renderer instanceof WebRenderer) {
83
                    $elements = array_merge($elements, $renderer->headElements($cell));
84
                }
85
            }
86
        }
87
        return $elements;
88
    }
89
90
    /**
91
     * @param Table $table
92
     * @return mixed
93
     */
94
    protected function getHeaders($table) {
95
        return $table->getHeaders();
96
    }
97
}