TableRowBox   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 1
Metric Value
wmc 29
eloc 106
dl 0
loc 254
rs 10
c 8
b 0
f 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setRowSpan() 0 5 1
A setRowSpanUp() 0 5 1
A getRowSpanUp() 0 3 1
A appendTableWrapperBox() 0 2 1
A getRowSpan() 0 3 1
A appendInlineBox() 0 2 1
A appendInlineBlockBox() 0 2 1
A createColumnBox() 0 14 1
D spanColumns() 0 49 16
A appendBlockBox() 0 2 1
A appendTableCellBox() 0 57 4
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * TableRowBox class.
6
 *
7
 * @package   YetiForcePDF\Layout
8
 *
9
 * @copyright YetiForce Sp. z o.o
10
 * @license   MIT
11
 * @author    Rafal Pospiech <[email protected]>
12
 */
13
14
namespace YetiForcePDF\Layout;
15
16
use YetiForcePDF\Html\Element;
17
use YetiForcePDF\Math;
18
use YetiForcePDF\Style\Style;
19
20
/**
21
 * Class TableRowBox.
22
 */
23
class TableRowBox extends BlockBox
24
{
25
	/**
26
	 * @var int
27
	 */
28
	protected $rowSpan = 1;
29
	/**
30
	 * @var int
31
	 */
32
	protected $rowSpanUp = 0;
33
34
	/**
35
	 * Set row span.
36
	 *
37
	 * @param int $rowSpan
38
	 *
39
	 * @return $this
40
	 */
41
	public function setRowSpan(int $rowSpan)
42
	{
43
		$this->rowSpan = $rowSpan;
44
45
		return $this;
46
	}
47
48
	/**
49
	 * Get row span.
50
	 *
51
	 * @return int
52
	 */
53
	public function getRowSpan()
54
	{
55
		return $this->rowSpan;
56
	}
57
58
	/**
59
	 * Set row span up.
60
	 *
61
	 * @param int $rowSpan
62
	 * @param int $rowSpanUp
63
	 *
64
	 * @return $this
65
	 */
66
	public function setRowSpanUp(int $rowSpanUp)
67
	{
68
		$this->rowSpanUp = $rowSpanUp;
69
70
		return $this;
71
	}
72
73
	/**
74
	 * Get row span up.
75
	 *
76
	 * @return int
77
	 */
78
	public function getRowSpanUp()
79
	{
80
		return $this->rowSpanUp;
81
	}
82
83
	/**
84
	 * We shouldn't append block box here.
85
	 *
86
	 * @param mixed $childDomElement
87
	 * @param mixed $element
88
	 * @param mixed $style
89
	 * @param mixed $parentBlock
90
	 */
91
	public function appendBlockBox($childDomElement, $element, $style, $parentBlock)
92
	{
93
	}
94
95
	/**
96
	 * We shouldn't append table wrapper here.
97
	 *
98
	 * @param mixed $childDomElement
99
	 * @param mixed $element
100
	 * @param mixed $style
101
	 * @param mixed $parentBlock
102
	 */
103
	public function appendTableWrapperBox($childDomElement, $element, $style, $parentBlock)
104
	{
105
	}
106
107
	/**
108
	 * We shouldn't append inline block box here.
109
	 *
110
	 * @param mixed $childDomElement
111
	 * @param mixed $element
112
	 * @param mixed $style
113
	 * @param mixed $parentBlock
114
	 */
115
	public function appendInlineBlockBox($childDomElement, $element, $style, $parentBlock)
116
	{
117
	}
118
119
	/**
120
	 * We shouldn't append inline box here.
121
	 *
122
	 * @param mixed $childDomElement
123
	 * @param mixed $element
124
	 * @param mixed $style
125
	 * @param mixed $parentBlock
126
	 */
127
	public function appendInlineBox($childDomElement, $element, $style, $parentBlock)
128
	{
129
	}
130
131
	/**
132
	 * Create column box.
133
	 *
134
	 * @throws \InvalidArgumentException
135
	 *
136
	 * @return $this
137
	 */
138
	public function createColumnBox()
139
	{
140
		$style = (new \YetiForcePDF\Style\Style())
141
			->setDocument($this->document)
142
			->parseInline();
143
		$box = (new TableColumnBox())
144
			->setDocument($this->document)
145
			->setParent($this)
146
			->setStyle($style)
147
			->init();
148
		$this->appendChild($box);
149
		$box->getStyle()->init();
150
151
		return $box;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $box returns the type YetiForcePDF\Layout\TableColumnBox which is incompatible with the documented return type YetiForcePDF\Layout\TableRowBox.
Loading history...
152
	}
153
154
	/**
155
	 * Span columns.
156
	 *
157
	 * @return $this
158
	 */
159
	public function spanColumns()
160
	{
161
		$colSpans = [];
162
		foreach ($this->getChildren() as $column) {
163
			if ($column->getColSpan() > 1) {
0 ignored issues
show
Bug introduced by
The method getColSpan() does not exist on YetiForcePDF\Layout\Box. It seems like you code against a sub-type of YetiForcePDF\Layout\Box such as YetiForcePDF\Layout\TableColumnBox. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
			if ($column->/** @scrutinizer ignore-call */ getColSpan() > 1) {
Loading history...
164
				$spanCount = $column->getColSpan() - 1;
165
				$spans = [$column];
166
				$currentColumn = $column;
167
				for ($i = 0; $i < $spanCount; ++$i) {
168
					$currentColumn = $currentColumn->getNext();
169
					$spans[] = $currentColumn;
170
				}
171
				$colSpans[] = $spans;
172
			}
173
		}
174
		$colSpansCount = \count($colSpans);
175
		foreach ($colSpans as $index => $columns) {
176
			$source = array_shift($columns);
177
			$spannedWidth = '0';
178
			foreach ($columns as $column) {
179
				if (isset($column) && null !== $column) {
180
					$spannedWidth = Math::add($spannedWidth, $column->getDimensions()->getWidth());
181
				}
182
			}
183
			$tableAuto = 'auto' === $this->getParent()->getParent()->getParent()->getStyle()->getRules('width');
184
			if (null !== $column) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $column does not seem to be defined for all execution paths leading up to this point.
Loading history...
185
				$separate = 'separate' === $column->getStyle()->getRules('border-collapse');
186
				if ($separate && $tableAuto) {
187
					$spannedWidth = Math::add($spannedWidth, Math::mul((string) (\count($columns)), $column->getStyle()->getRules('border-spacing')));
188
				}
189
				if ($separate && null === $column->getNext() && !$tableAuto) {
190
					$spannedWidth = Math::sub($spannedWidth, $column->getStyle()->getRules('border-spacing'));
191
				}
192
				foreach ($columns as $column) {
193
					$column->setColSpanned($colSpansCount - $index)->setRenderable(false);
194
				}
195
				if (null === $source->getNext()) {
196
					$cell = $source->getFirstChild();
197
					$cellStyle = $cell->getStyle();
198
					$cellStyle->setRule('border-right-width', $cellStyle->getRules('border-left-width'));
199
				}
200
				$sourceDmns = $source->getDimensions();
201
				$sourceDmns->setWidth(Math::add($sourceDmns->getWidth(), $spannedWidth));
202
				$cell = $source->getFirstChild();
203
				$cell->getDimensions()->setWidth($sourceDmns->getInnerWidth());
204
			}
205
		}
206
207
		return $this;
208
	}
209
210
	/**
211
	 * Append table cell box element.
212
	 *
213
	 * @param \DOMElement                   $childDomElement
214
	 * @param Element                       $element
215
	 * @param Style                         $style
216
	 * @param \YetiForcePDF\Layout\BlockBox $parentBlock
217
	 *
218
	 * @return $this
219
	 */
220
	public function appendTableCellBox($childDomElement, $element, $style, $parentBlock)
0 ignored issues
show
Unused Code introduced by
The parameter $parentBlock is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

220
	public function appendTableCellBox($childDomElement, $element, $style, /** @scrutinizer ignore-unused */ $parentBlock)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
221
	{
222
		$colSpan = 1;
223
		$style->setRule('display', 'block');
224
		$attributeColSpan = $childDomElement->getAttribute('colspan');
225
		if ($attributeColSpan) {
226
			$colSpan = (int) $attributeColSpan;
227
		}
228
		$rowSpan = 1;
229
		$attributeRowSpan = $childDomElement->getAttribute('rowspan');
230
		if ($attributeRowSpan) {
231
			$rowSpan = (int) $attributeRowSpan;
232
		}
233
		$clearStyle = (new \YetiForcePDF\Style\Style())
234
			->setDocument($this->document)
235
			->parseInline();
236
		$column = (new TableColumnBox())
237
			->setDocument($this->document)
238
			->setParent($this)
239
			->setStyle($clearStyle)
240
			->init();
241
		$column->setColSpan($colSpan)->setRowSpan($rowSpan);
242
		$this->appendChild($column);
243
		$column->getStyle()->init()->setRule('display', 'block');
244
		$box = (new TableCellBox())
245
			->setDocument($this->document)
246
			->setParent($column)
247
			->setElement($element)
248
			->setStyle($style)
249
			->init();
250
		$column->appendChild($box);
251
		$box->getStyle()->init();
252
		--$colSpan;
253
		for ($i = 0; $i < $colSpan; ++$i) {
254
			$clearStyle = (new \YetiForcePDF\Style\Style())
255
				->setDocument($this->document)
256
				->parseInline();
257
			$column = (new TableColumnBox())
258
				->setDocument($this->document)
259
				->setParent($this)
260
				->setStyle($clearStyle)
261
				->init();
262
			$column->setColSpan(-1);
263
			$this->appendChild($column);
264
			$column->getStyle()->init()->setRule('display', 'block');
265
			$spanBox = (new TableCellBox())
266
				->setDocument($this->document)
267
				->setParent($column)
268
				->setStyle(clone $style)
269
				->setElement(clone $element)
270
				->setSpanned(true)
271
				->init();
272
			$column->appendChild($spanBox);
273
		}
274
		$box->buildTree($box);
275
276
		return $box;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $box returns the type YetiForcePDF\Layout\TableCellBox which is incompatible with the documented return type YetiForcePDF\Layout\TableRowBox.
Loading history...
277
	}
278
}
279