Nip_Form_Renderer_Table::renderRows()   B
last analyzed

Complexity

Conditions 11
Paths 18

Size

Total Lines 45
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 45
ccs 0
cts 32
cp 0
rs 7.3166
cc 11
nc 18
nop 0
crap 132

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
use Nip\Form\Renderer\AbstractRenderer;
4
5
class Nip_Form_Renderer_Table extends AbstractRenderer
6
{
7
    protected $_table = [];
8
    protected $_tbody = [];
9
    protected $_data = [];
10
    protected $_rows = [];
11
    protected $_cols = [];
12
13 8
    public function __construct()
14
    {
15 8
        parent::__construct();
16 8
        $this->setTableAttrib('class', 'form horizontal');
17 8
        $this->setTableAttrib('cellspacing', '0');
18 8
        $this->setTableAttrib('cellpadding', '0');
19 8
    }
20
21 8
    public function setTableAttrib($type, $value)
22
    {
23 8
        $this->_table[$type] = $value;
24 8
        return $this;
25
    }
26
27
    public function setTBodyAttrib($type, $value)
28
    {
29
        $this->_tbody[$type] = $value;
30
        return $this;
31
    }
32
33
    public function setRowAttrib($idRow, $type, $value)
34
    {
35
        $this->_rows[$idRow][$type] = $value;
36
        return $this;
37
    }
38
39
    public function addClassName($name)
40
    {
41
        $this->_table['class'] .= ' ' . $name;
42
        return $this;
43
    }
44
45
    public function addCell($idRow, $idCol, $element, $type = 'text')
46
    {
47
        $this->_data[$idRow][$idCol]['element'] = $element;
48
        $this->_data[$idRow][$idCol]['type'] = $type;
49
        if (!in_array($idCol, $this->_cols)) {
50
            $this->_cols[] = $idCol;
51
        }
52
    }
53
54
    public function setCols()
55
    {
56
        $this->_cols = func_get_args();
57
    }
58
59
60
    public function renderElements()
61
    {
62
        $return = '<table';
63
        foreach ($this->_table as $attrib => $value) {
64
            $return .= ' ' . $attrib . '="' . $value . '"';
65
        }
66
        $return .= '>';
67
        $renderRows = $this->renderRows();
68
        $return .= '<tbody';
69
        foreach ($this->_tbody as $attrib => $value) {
70
            $return .= ' ' . $attrib . '="' . $value . '"';
71
        }
72
        $return .= '>';
73
        if ($renderRows) {
74
            $return .= $renderRows;
75
        }
76
        $return .= '</tbody>';
77
        $return .= '</table>';
78
        return $return;
79
    }
80
81
    public function renderRows()
82
    {
83
        $return = '';
84
        foreach ($this->_data as $idRow=>$row) {
85
            $cell = reset($row);
86
            $element = $cell['element'];
87
88
            if (!$element->isRendered()) {
89
                $return .= '<tr';
90
                if ($this->_rows[$idRow]) {
91
                    foreach ($this->_rows[$idRow] as $attrib => $value) {
92
                        $return .= ' ' . $attrib . '="' . $value . '"';
93
                    }
94
                }
95
96
                $return .= '>';
97
                foreach ($this->_cols as $idCol) {
98
                    $cell = $row[$idCol];
99
                    switch ($cell['type']) {
100
                        case 'label':
101
                            $return .= '<td class="label' . ($element->isError() ? ' error' : '') . '">';
102
                            $return .= $element->getLabel();
103
                            if ($element->isRequired()) {
104
                                $return .= '<span class="required">*</span>';
105
                            }
106
                            $return .= ":";
107
                            break;
108
109
                        case 'value':
110
                            $return .= '<td class="value">';
111
                            $return .= $this->renderElement($element);
112
                            $return .= '</td>';
113
                            break;
114
115
                        case 'text':
116
                        default:
117
                            $return .= '<td>';
118
                            $return .= $cell['element'];
119
                            $return .= '</td>';
120
                    }
121
                }
122
                $return .= '</tr>';
123
            }
124
        }
125
        return $return;
126
    }
127
}
128