Completed
Push — master ( 872df9...4bf172 )
by Jean-Christophe
04:11
created

HtmlTD   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 111
Duplicated Lines 11.71 %

Coupling/Cohesion

Components 3
Dependencies 5

Importance

Changes 0
Metric Value
wmc 23
lcom 3
cbo 5
dl 13
loc 111
rs 10
c 0
b 0
f 0

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