Component   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 34
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 152
ccs 103
cts 103
cp 1
rs 9.2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A open() 0 8 2
A cell() 0 12 3
A close() 0 11 1
A values() 0 15 4
A head() 0 8 2
A foot() 0 8 2
A row() 0 8 2
C wrapUp() 0 60 17
1
<?php
2
3
namespace BootPress\Table;
4
5
use BootPress\Page\Component as Page;
6
7
class Component
8
{
9
    private $page;
10
    private $head = false; // or true
11
    private $foot = false; // or true
12
    private $body = false; // or true
13
    private $cell = ''; // or a closing </th> or </td> tag
14
    private $vars = array(); // to include in every cell
15
16 2
    public function __construct()
17
    {
18 2
        $this->page = Page::html();
19 2
    }
20
21 2
    public function open($vars = '', $caption = '')
22
    {
23 2
        if (!empty($caption)) {
24 1
            $caption = '<caption>'.$caption.'</caption>';
25 1
        }
26
27 2
        return "\n".$this->page->tag('table', $this->values($vars)).$caption;
28
    }
29
30 1
    public function head($vars = '', $cells = '')
31
    {
32 1
        $html = $this->wrapUp('head')."\n\t";
33 1
        $this->head = true;
34 1
        $this->vars = (!empty($cells)) ? $this->values($cells) : array();
35
36 1
        return $html.$this->page->tag('tr', $this->values($vars));
37
    }
38
39 1
    public function foot($vars = '', $cells = '')
40
    {
41 1
        $html = $this->wrapUp('foot')."\n\t";
42 1
        $this->foot = true;
43 1
        $this->vars = (!empty($cells)) ? $this->values($cells) : array();
44
45 1
        return $html.$this->page->tag('tr', $this->values($vars));
46
    }
47
48 1
    public function row($vars = '', $cells = '')
49
    {
50 1
        $html = $this->wrapUp('row')."\n\t";
51 1
        $this->body = true;
52 1
        $this->vars = (!empty($cells)) ? $this->values($cells) : array();
53
54 1
        return $html.$this->page->tag('tr', $this->values($vars));
55
    }
56
57 1
    public function cell($vars = '', $content = '')
58
    {
59 1
        $html = $this->wrapUp('cell');
60 1
        $tag = ($this->head) ? 'th' : 'td';
61 1
        $this->cell = '</'.$tag.'>';
62 1
        $vars = $this->values($vars);
63 1
        if (!empty($this->vars)) {
64 1
            $vars = array_merge($this->vars, $vars);
65 1
        }
66
67 1
        return $html.$this->page->tag($tag, $vars).$content;
68
    }
69
70 1
    public function close()
71
    {
72 1
        $html = $this->wrapUp('table')."\n";
73 1
        $this->head = false;
74 1
        $this->foot = false;
75 1
        $this->body = false;
76 1
        $this->cell = '';
77 1
        $this->vars = '';
0 ignored issues
show
Documentation Bug introduced by
It seems like '' of type string is incompatible with the declared type array of property $vars.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
78
79 1
        return $html.'</table>';
80
    }
81
82 2
    protected function values($vars)
83
    {
84 2
        if (is_array($vars)) {
85 2
            return $vars;
86
        }
87 2
        $attributes = array();
88 2
        foreach (explode('|', $vars) as $value) {
89 2
            if (strpos($value, '=')) {
90 2
                list($key, $value) = explode('=', $value);
91 2
                $attributes[$key] = $value;
92 2
            }
93 2
        }
94
95 2
        return $attributes;
96
    }
97
98 1
    private function wrapUp($section)
99
    {
100 1
        $html = $this->cell;
101 1
        $this->cell = '';
102
        switch ($section) {
103 1
            case 'head':
104 1
                if ($this->head) {
105 1
                    $html .= '</tr>';
106 1
                } else {
107 1
                    if ($this->foot) {
108 1
                        $html .= '</tr></tfoot>';
109 1
                        $this->foot = false;
110 1
                    } elseif ($this->body) {
111 1
                        $html .= '</tr></tbody>';
112 1
                        $this->body = false;
113 1
                    }
114 1
                    $html .= '<thead>';
115
                }
116 1
                break;
117 1
            case 'foot':
118 1
                if ($this->foot) {
119 1
                    $html .= '</tr>';
120 1
                } else {
121 1
                    if ($this->head) {
122 1
                        $html .= '</tr></thead>';
123 1
                        $this->head = false;
124 1
                    } elseif ($this->body) {
125 1
                        $html .= '</tr></tbody>';
126 1
                        $this->body = false;
127 1
                    }
128 1
                    $html .= '<tfoot>';
129
                }
130 1
                break;
131 1
            case 'row':
132 1
                if ($this->body) {
133 1
                    $html .= '</tr>';
134 1
                } else {
135 1
                    if ($this->head) {
136 1
                        $html .= '</tr></thead>';
137 1
                        $this->head = false;
138 1
                    } elseif ($this->foot) {
139 1
                        $html .= '</tr></tfoot>';
140 1
                        $this->foot = false;
141 1
                    }
142 1
                    $html .= '<tbody>';
143
                }
144 1
                break;
145 1
            case 'table':
146 1
                if ($this->head) {
147 1
                    $html .= '</tr></thead>';
148 1
                } elseif ($this->foot) {
149 1
                    $html .= '</tr></tfoot>';
150 1
                } elseif ($this->body) {
151 1
                    $html .= '</tr></tbody>';
152 1
                }
153 1
                break;
154
        }
155
156 1
        return $html;
157
    }
158
}
159