Completed
Push — master ( 5f7aee...f5f495 )
by Jean-Christophe
03:30
created

HtmlTR   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 166
Duplicated Lines 2.41 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 6
dl 4
loc 166
rs 9.3999
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setColCount() 0 8 2
A getColCount() 0 3 1
A createItem() 0 6 1
A setTdTagName() 0 3 1
A setContainer() 0 4 1
A setValues() 0 3 1
A addValues() 0 3 1
A _addOrSetValues() 4 17 4
A delete() 0 4 1
A mergeCol() 0 3 1
A mergeRow() 0 3 1
B getColPosition() 0 21 7
A conditionalCellFormat() 0 7 2
A conditionalRowFormat() 0 6 2
A containsStr() 0 8 3
A apply() 0 4 1
A applyCells() 0 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\HtmlSemCollection;
6
use Ajax\semantic\html\base\constants\State;
7
use Ajax\semantic\html\base\traits\TableElementTrait;
8
use Ajax\service\JArray;
9
10
/**
11
 *
12
 * @author jc
13
 *
14
 */
15
class HtmlTR extends HtmlSemCollection {
16
	use TableElementTrait;
17
	private $_tdTagName;
18
	private $_container;
19
	private $_row;
20
21
	public function __construct($identifier) {
22
		parent::__construct($identifier, "tr", "");
23
		$this->_states=[ State::ACTIVE,State::POSITIVE,State::NEGATIVE,State::WARNING,State::ERROR,State::DISABLED ];
24
	}
25
26
	public function setColCount($colCount) {
27
		$count=$this->count();
28
		for($i=$count; $i < $colCount; $i++) {
29
			$item=$this->addItem(NULL);
30
			$item->setTagName($this->_tdTagName);
31
		}
32
		return $this;
33
	}
34
35
	public function getColCount(){
36
		return $this->count();
37
	}
38
39
	/**
40
	 *
41
	 * {@inheritDoc}
42
	 *
43
	 * @see \Ajax\common\html\HtmlCollection::createItem()
44
	 */
45
	protected function createItem($value) {
46
		$count=$this->count();
47
		$td=new HtmlTD("", $value, $this->_tdTagName);
48
		$td->setContainer($this->_container, $this->_row, $count);
49
		return $td;
50
	}
51
52
	public function setTdTagName($tagName="td") {
53
		$this->_tdTagName=$tagName;
54
	}
55
56
	/**
57
	 * Define the container (HtmlTableContent) and the num of the row
58
	 * @param HtmlTableContent $container
59
	 * @param int $row
60
	 */
61
	public function setContainer($container, $row) {
62
		$this->_container=$container;
63
		$this->_row=$row;
64
	}
65
66
	/**
67
	 * Sets values to the row cols
68
	 * @param mixed $values
69
	 */
70
	public function setValues($values=array()) {
71
		return $this->_addOrSetValues($values, function(&$cell,$value){$cell->setValue($value);});
72
	}
73
74
	/**
75
	 * Adds values to the row cols
76
	 * @param mixed $values
77
	 */
78
	public function addValues($values=array()) {
79
		return $this->_addOrSetValues($values, function(&$cell,$value){$cell->addValue($value);});
80
	}
81
82
	/**
83
	 * Sets or adds values to the row cols
84
	 * @param mixed $values
85
	 */
86
	protected function _addOrSetValues($values,$callback) {
87
		$count=$this->count();
88
		if (!\is_array($values)) {
89
			$values=\array_fill(0, $count, $values);
90
		} else {
91
			if (JArray::isAssociative($values) === true) {
92
				$values=\array_values($values);
93
			}
94
		}
95
		$count=\min(\sizeof($values), $count);
96
97 View Code Duplication
		for($i=0; $i < $count; $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...
98
			$cell=$this->content[$i];
99
			$callback($cell,$values[$i]);
100
		}
101
		return $this;
102
	}
103
104
	/**
105
	 * Removes the col at $index
106
	 * @param int $index the index of the col to remove
107
	 * @return \Ajax\semantic\html\content\table\HtmlTR
108
	 */
109
	public function delete($index) {
110
		$this->removeItem($index);
111
		return $this;
112
	}
113
114
	public function mergeCol($colIndex=0) {
115
		return $this->getItem($colIndex)->mergeCol();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Ajax\common\html\HtmlDoubleElement as the method mergeCol() does only exist in the following sub-classes of Ajax\common\html\HtmlDoubleElement: Ajax\semantic\html\content\table\HtmlTD, Ajax\semantic\html\content\table\HtmlTR, Ajax\semantic\html\content\table\HtmlTableContent. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
116
	}
117
118
	public function mergeRow($colIndex=0) {
119
		return $this->getItem($colIndex)->mergeRow();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Ajax\common\html\HtmlDoubleElement as the method mergeRow() does only exist in the following sub-classes of Ajax\common\html\HtmlDoubleElement: Ajax\semantic\html\content\table\HtmlTD, Ajax\semantic\html\content\table\HtmlTR, Ajax\semantic\html\content\table\HtmlTableContent. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
120
	}
121
122
	public function getColPosition($colIndex) {
123
		$pos=0;
124
		$rows=$this->_container->getContent();
125
		for($i=0; $i < $this->_row; $i++) {
126
			$max=\min($colIndex, $rows[$i]->count());
127
			for($j=0; $j < $max; $j++) {
128
				$rowspan=$rows[$i]->getItem($j)->getRowspan();
129
				if ($rowspan + $i > $this->_row)
130
					$pos++;
131
			}
132
		}
133
		if ($pos > $colIndex)
134
			return NULL;
135
		$count=$this->count();
136
		for($i=0; $i < $count; $i++) {
137
			$pos+=$this->content[$i]->getColspan();
138
			if ($pos >= $colIndex + 1)
139
				return $i;
140
		}
141
		return null;
142
	}
143
144
	public function conditionalCellFormat($callback, $format) {
145
		$cells=$this->content;
146
		foreach ( $cells as $cell ) {
147
			$cell->conditionalCellFormat($callback, $format);
148
		}
149
		return $this;
150
	}
151
152
	public function conditionalRowFormat($callback, $format) {
153
		if ($callback($this)) {
154
			$this->addToProperty("class", $format);
155
		}
156
		return $this;
157
	}
158
159
	public function containsStr($needle) {
160
		$cells=$this->content;
161
		foreach ( $cells as $cell ) {
162
			if (\strpos($cell->getContent(), $needle) !== false)
163
				return true;
164
		}
165
		return false;
166
	}
167
168
	public function apply($callback) {
169
		$callback($this);
170
		return $this;
171
	}
172
173
	public function applyCells($callback) {
174
		$cells=$this->content;
175
		foreach ( $cells as $cell ) {
176
			$cell->apply($callback);
177
		}
178
		return $this;
179
	}
180
}