Table   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 16
c 3
b 0
f 1
lcom 1
cbo 2
dl 0
loc 192
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A addCell() 0 15 2
A constructCell() 0 7 1
A createRow() 0 7 1
A addRow() 0 21 4
A getRows() 0 4 1
A getHeaderRows() 0 4 1
A getFooterRows() 0 4 1
A getCurrentRow() 0 9 2
A setCurrentRowAttributes() 0 6 1
A getAttributes() 0 4 1
A setAttributes() 0 6 1
1
<?php
2
/**
3
 * @package    Fuel\Common
4
 * @version    2.0
5
 * @author     Fuel Development Team
6
 * @license    MIT License
7
 * @copyright  2010 - 2015 Fuel Development Team
8
 * @link       http://fuelphp.com
9
 */
10
11
namespace Fuel\Common;
12
13
use Fuel\Common\Table\Row;
14
use Fuel\Common\Table\Cell;
15
use Fuel\Common\Table\EnumRowType;
16
17
/**
18
 * Deals with constructing table structures.
19
 *
20
 * @package Fuel\Common
21
 *
22
 * @since 2.0
23
 */
24
class Table
25
{
26
	/**
27
	 * @var array
28
	 */
29
	protected $rows = [];
30
31
	/**
32
	 * @var array
33
	 */
34
	protected $headerRows = [];
35
36
	/**
37
	 * @var array
38
	 */
39
	protected $footerRows = [];
40
41
	/**
42
	 * @var Row The row that new cells will be added to
43
	 */
44
	protected $currentRow = null;
45
46
	/**
47
	 * @var array
48
	 */
49
	protected $attributes = [];
50
51
	/**
52
	 * Adds a Cell to the current Row
53
	 *
54
	 * @param mixed $content    Anything that is not a Cell will be added as content to a new Cell
55
	 * @param array $attributes The array of attributes to assign the new Cell
56
	 *
57
	 * @return $this
58
	 */
59
	public function addCell($content, $attributes = array())
60
	{
61
		$currentRow = $this->getCurrentRow();
62
		//If we have been given a Cell then just add it, else create a new cell
63
		if ( $content instanceof Cell )
64
		{
65
			$currentRow[] = $content;
66
		}
67
		else
68
		{
69
			$currentRow[] = $this->constructCell($content, $attributes);
70
		}
71
72
		return $this;
73
	}
74
75
	/**
76
	 * Creates a new Cell with the given content
77
	 *
78
	 * @param mixed $content    The content for the new Cell
79
	 * @param array $attributes The attributes for the Cell
80
	 *
81
	 * @return Cell
82
	 */
83
	protected function constructCell($content = null, $attributes = array())
84
	{
85
		$cell = new Cell($content);
86
		$cell->setAttributes($attributes);
87
88
		return $cell;
89
	}
90
91
	/**
92
	 * Creates a new Row object and assigns it as the currently active row
93
	 *
94
	 * @param EnumRowType $type The type of the new row, uses Body by default
95
	 *
96
	 * @return $this
97
	 */
98
	public function createRow($type = EnumRowType::Body)
99
	{
100
		$this->currentRow = new Row;
101
		$this->currentRow->setType($type);
0 ignored issues
show
Documentation introduced by
$type is of type object<Fuel\Common\Table\EnumRowType>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
103
		return $this;
104
	}
105
106
	/**
107
	 * Adds the Row that's currently being constructed to the list of finished Rows
108
	 *
109
	 * @return $this
110
	 */
111
	public function addRow()
112
	{
113
		switch ( $this->currentRow->getType() )
114
		{
115
			case EnumRowType::Body:
116
				$this->rows[] = $this->currentRow;
117
				break;
118
			case EnumRowType::Header:
119
				$this->headerRows[] = $this->currentRow;
120
				break;
121
			case EnumRowType::Footer:
122
				$this->footerRows[] = $this->currentRow;
123
				break;
124
			default:
125
				throw new \InvalidArgumentException('Unknown row type');
126
		}
127
128
		$this->currentRow = null;
129
130
		return $this;
131
	}
132
133
	/**
134
	 * Returns a list of all currently consructed Rows
135
	 *
136
	 * @return array
137
	 */
138
	public function getRows()
139
	{
140
		return $this->rows;
141
	}
142
143
	/**
144
	 * Returns a list of currently constructed header rows
145
	 *
146
	 * @return array
147
	 */
148
	public function getHeaderRows()
149
	{
150
		return $this->headerRows;
151
	}
152
153
	/**
154
	 * Returns a list of currently constructed footer rows
155
	 *
156
	 * @return array
157
	 */
158
	public function getFooterRows()
159
	{
160
		return $this->footerRows;
161
	}
162
163
	/**
164
	 * Gets the currently active row. The row will not be added until addRow() is called
165
	 *
166
	 * @return Row
167
	 */
168
	public function getCurrentRow()
169
	{
170
		if ( is_null($this->currentRow) )
171
		{
172
			$this->createRow();
173
		}
174
175
		return $this->currentRow;
176
	}
177
178
	/**
179
	 * Sets the attributes for the currently active Row
180
	 *
181
	 * @param array $attributes
182
	 *
183
	 * @return $this
184
	 */
185
	public function setCurrentRowAttributes(array $attributes)
186
	{
187
		$this->getCurrentRow()->setAttributes($attributes);
188
189
		return $this;
190
	}
191
192
	/**
193
	 * Returns the attributes of this Table
194
	 *
195
	 * @return array
196
	 */
197
	public function getAttributes()
198
	{
199
		return $this->attributes;
200
	}
201
202
	/**
203
	 * Sets the atributes of the Table
204
	 *
205
	 * @param array $newAttributes
206
	 *
207
	 * @return $this
208
	 */
209
	public function setAttributes(array $newAttributes)
210
	{
211
		$this->attributes = $newAttributes;
212
213
		return $this;
214
	}
215
}
216