Completed
Push — master ( f662ec...5059ab )
by Jean-Christophe
02:58
created

HtmlTR   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 173
Duplicated Lines 2.31 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 35
lcom 2
cbo 6
dl 4
loc 173
rs 9
c 0
b 0
f 0

19 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
C getColPosition() 0 23 8
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
A toDelete() 0 4 1

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(HtmlTD &$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(HtmlTD &$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
		if($this->_container->_isMerged()!==true)
124
			return $colIndex;
125
		$pos=0;
126
		$rows=$this->_container->getContent();
127
		for($i=0; $i < $this->_row; $i++) {
128
			$max=\min($colIndex, $rows[$i]->count());
129
			for($j=0; $j < $max; $j++) {
130
				$rowspan=$rows[$i]->getItem($j)->getRowspan();
131
				if ($rowspan + $i > $this->_row)
132
					$pos++;
133
			}
134
		}
135
		if ($pos > $colIndex)
136
			return NULL;
137
		$count=$this->count();
138
		for($i=0; $i < $count; $i++) {
139
			$pos+=$this->content[$i]->getColspan();
140
			if ($pos >= $colIndex + 1)
141
				return $i;
142
		}
143
		return null;
144
	}
145
146
	public function conditionalCellFormat($callback, $format) {
147
		$cells=$this->content;
148
		foreach ( $cells as $cell ) {
149
			$cell->conditionalCellFormat($callback, $format);
150
		}
151
		return $this;
152
	}
153
154
	public function conditionalRowFormat($callback, $format) {
155
		if ($callback($this)) {
156
			$this->addToProperty("class", $format);
157
		}
158
		return $this;
159
	}
160
161
	public function containsStr($needle) {
162
		$cells=$this->content;
163
		foreach ( $cells as $cell ) {
164
			if (\strpos($cell->getContent(), $needle) !== false)
165
				return true;
166
		}
167
		return false;
168
	}
169
170
	public function apply($callback) {
171
		$callback($this);
172
		return $this;
173
	}
174
175
	public function applyCells($callback) {
176
		$cells=$this->content;
177
		foreach ( $cells as $cell ) {
178
			$cell->apply($callback);
179
		}
180
		return $this;
181
	}
182
183
	public function toDelete($colIndex){
184
		$this->getItem($colIndex)->toDelete();
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 toDelete() 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...
185
		return $this;
186
	}
187
}
188