Completed
Push — master ( f99fed...57dceb )
by Jean-Christophe
03:29
created

HtmlTD   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 103
Duplicated Lines 5.83 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 0
Metric Value
wmc 21
lcom 3
cbo 4
dl 6
loc 103
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setContainer() 0 5 1
A setValue() 0 4 1
A addValue() 0 4 1
A setRowspan() 3 8 2
A mergeRow() 0 7 2
A mergeCol() 0 7 2
A setColspan() 3 8 2
A getColspan() 0 6 2
A getRowspan() 0 6 2
A conditionalCellFormat() 0 6 2
A apply() 0 4 1
A setSelectable() 0 6 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
3
namespace Ajax\semantic\html\content\table;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\base\traits\TextAlignmentTrait;
7
use Ajax\semantic\html\base\constants\Variation;
8
use Ajax\semantic\html\base\constants\State;
9
use Ajax\semantic\html\base\traits\TableElementTrait;
10
use Ajax\semantic\html\elements\html5\HtmlLink;
11
12
class HtmlTD extends HtmlSemDoubleElement {
13
	use TextAlignmentTrait,TableElementTrait;
14
	private $_container;
15
	private $_row;
16
	private $_col;
17
	private $_colMerged=false;
18
	private $_rowMerged=false;
19
20
	/**
21
	 *
22
	 * @param string $identifier
23
	 * @param mixed $content
24
	 * @param string $tagName
25
	 */
26
	public function __construct($identifier, $content=NULL, $tagName="td") {
27
		parent::__construct($identifier, $tagName, "", $content);
28
		$this->_variations=[ Variation::COLLAPSING ];
29
		$this->_states=[ State::ACTIVE,State::POSITIVE,State::NEGATIVE,State::WARNING,State::ERROR,State::DISABLED ];
30
	}
31
32
	public function setContainer($container, $row, $col) {
33
		$this->_container=$container;
34
		$this->_row=$row;
35
		$this->_col=$col;
36
	}
37
38
	public function setValue($value) {
39
		$this->content=$value;
40
		return $this;
41
	}
42
43
	public function addValue($value) {
44
		$this->addContent($value);
45
		return $this;
46
	}
47
48
	public function setRowspan($rowspan) {
49
		$to=min($this->_container->count(), $this->_row + $rowspan - 1);
50 View Code Duplication
		for($i=$to; $i > $this->_row; $i--) {
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...
51
			$this->_container->delete($i, $this->_col);
52
		}
53
		$this->setProperty("rowspan", $rowspan);
54
		return $this->_container;
55
	}
56
57
	public function mergeRow() {
58
		if(!$this->_rowMerged){
59
			$this->_rowMerged=true;
60
			return $this->setRowspan($this->_container->count());
61
		}
62
		return $this->_container;
63
	}
64
65
	public function mergeCol() {
66
		if(!$this->_colMerged){
67
			$this->_colMerged=true;
68
			return $this->setColspan($this->_container->getRow($this->_row)->count());
69
		}
70
		return $this->_container;
71
	}
72
73
	public function setColspan($colspan) {
74
		$to=min($this->_container->getRow($this->_row)->count(), $this->_col + $colspan - 1);
75 View Code Duplication
		for($i=$to; $i > $this->_col; $i--) {
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...
76
			$this->_container->delete($this->_row, $this->_col + 1);
77
		}
78
		$this->setProperty("colspan", $colspan);
79
		return $this->_container;
80
	}
81
82
	public function getColspan() {
83
		$colspan=1;
84
		if (\array_key_exists("colspan", $this->properties))
85
			$colspan=$this->getProperty("colspan");
86
		return $colspan;
87
	}
88
89
	public function getRowspan() {
90
		$rowspan=1;
91
		if (\array_key_exists("rowspan", $this->properties))
92
			$rowspan=$this->getProperty("rowspan");
93
		return $rowspan;
94
	}
95
96
	public function conditionalCellFormat($callback, $format) {
97
		if ($callback($this)) {
98
			$this->addToProperty("class", $format);
99
		}
100
		return $this;
101
	}
102
103
	public function apply($callback) {
104
		$callback($this);
105
		return $this;
106
	}
107
108
	public function setSelectable($href="#") {
109
		if (\is_string($this->content)) {
110
			$this->content=new HtmlLink("", $href, $this->content);
111
		}
112
		return $this->addToProperty("class", "selectable");
113
	}
114
}