Completed
Push — master ( ea2a7d...e9bdcb )
by Jean-Christophe
03:40
created

HtmlTableContent::getRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Ajax\semantic\html\content\table;
4
5
use Ajax\semantic\html\base\HtmlSemCollection;
6
use Ajax\service\JArray;
7
8
/**
9
 * a table content (thead, tbody or tfoot)
10
 * @author jc
11
 *
12
 */
13
class HtmlTableContent extends HtmlSemCollection {
14
	protected $_tdTagNames=[ "thead" => "th","tbody" => "td","tfoot" => "th" ];
15
16
	/**
17
	 *
18
	 * @param string $identifier
19
	 * @param string $tagName
20
	 * @param int $rowCount
21
	 * @param int $colCount
22
	 */
23
	public function __construct($identifier, $tagName="tbody", $rowCount=NULL, $colCount=NULL) {
24
		parent::__construct($identifier, $tagName, "");
25
		if (isset($rowCount) && isset($colCount))
26
			$this->setRowCount($rowCount, $colCount);
27
	}
28
29
	/**
30
	 *
31
	 * @param int $rowCount
32
	 * @param int $colCount
33
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
34
	 */
35
	public function setRowCount($rowCount, $colCount) {
36
		$count=$this->count();
37
		for($i=$count; $i < $rowCount; $i++) {
38
			$this->addItem(NULL);
39
		}
40
		for($i=0; $i < $rowCount; $i++) {
41
			$item=$this->content[$i];
42
			$item->setTdTagName($this->_tdTagNames[$this->tagName]);
43
			$this->content[$i]->setColCount($colCount);
44
		}
45
		return $this;
46
	}
47
48
	public function getTdTagName($tagName) {
0 ignored issues
show
Unused Code introduced by
The parameter $tagName is not used and could be removed.

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

Loading history...
49
		return $this->_tdTagNames[$this->tagName];
50
	}
51
52
	/**
53
	 *
54
	 * {@inheritDoc}
55
	 *
56
	 * @see \Ajax\common\html\HtmlCollection::createItem()
57
	 */
58
	protected function createItem($value) {
59
		$count=$this->count();
60
		$tr=new HtmlTR("", $value);
0 ignored issues
show
Unused Code introduced by
The call to HtmlTR::__construct() has too many arguments starting with $value.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
61
		$tr->setContainer($this, $count);
62
		$tr->setTdTagName($this->_tdTagNames[$this->tagName]);
63
		if (isset($value) === true) {
64
			$tr->setColCount($value);
65
		}
66
		return $tr;
67
	}
68
69
	public function addRow($colCount) {
70
		return $this->addItem($colCount);
71
	}
72
73
	/**
74
	 * Returns the cell (HtmlTD) at position $row,$col
75
	 * @param int $row
76
	 * @param int $col
77
	 * @return \Ajax\semantic\html\content\HtmlTD
78
	 */
79
	public function getCell($row, $col) {
80
		$row=$this->getItem($row);
81
		if (isset($row)) {
82
			$col=$row->getItem($col);
83
		}
84
		return $col;
85
	}
86
87
	/**
88
	 *
89
	 * @param int $index
90
	 * @return \Ajax\semantic\html\content\HtmlTR
91
	 */
92
	public function getRow($index) {
93
		return $this->getItem($index);
94
	}
95
96
	/**
97
	 *
98
	 * @param int $row
99
	 * @param int $col
100
	 * @param mixed $value
101
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
102
	 */
103
	public function setCellValue($row, $col, $value="") {
104
		$cell=$this->getCell($row, $col);
105
		if (isset($cell) === true) {
106
			$cell->setValue($value);
107
		}
108
		return $this;
109
	}
110
111
	/**
112
	 * Sets the cells values
113
	 * @param mixed $values
114
	 */
115
	public function setValues($values=array()) {
116
		$count=$this->count();
117
		$isArray=true;
118
		if (\is_array($values) === false) {
119
			$values=\array_fill(0, $count, $values);
120
			$isArray=false;
121
		}
122
		if (JArray::dimension($values) == 1 && $isArray)
123
			$values=[ $values ];
124
		
125
		$count=\min(\sizeof($values), $count);
126
		
127
		for($i=0; $i < $count; $i++) {
128
			$row=$this->content[$i];
129
			$row->setValues($values[$i]);
130
		}
131
		return $this;
132
	}
133
134 View Code Duplication
	public function setColValues($colIndex, $values=array()) {
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...
135
		$count=$this->count();
136
		if (\is_array($values) === false) {
137
			$values=\array_fill(0, $count, $values);
138
		}
139
		$count=\min(\sizeof($values), $count);
140
		for($i=0; $i < $count; $i++) {
141
			$this->getCell($i, $colIndex)->setValue($values[$i]);
142
		}
143
		return $this;
144
	}
145
146
	public function addColVariations($colIndex, $variations=array()) {
147
		$count=$this->count();
148
		for($i=0; $i < $count; $i++) {
149
			$this->getCell($i, $colIndex)->addVariations($variations);
150
		}
151
		return $this;
152
	}
153
154
	public function setRowValues($rowIndex, $values=array()) {
155
		$count=$this->count();
156
		if (\is_array($values) === false) {
157
			$values=\array_fill(0, $count, $values);
158
		}
159
		$this->getItem($rowIndex)->setValues($values);
0 ignored issues
show
Bug introduced by
The method setValues() does not exist on Ajax\common\html\HtmlDoubleElement. Did you maybe mean setValue()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
160
		return $this;
161
	}
162
163
	public function colCenter($colIndex) {
164
		$count=$this->count();
165
		for($i=0; $i < $count; $i++) {
166
			$this->getCell($i, $colIndex)->textCenterAligned();
167
		}
168
		return $this;
169
	}
170
171
	public function colRight($colIndex) {
172
		$count=$this->count();
173
		for($i=0; $i < $count; $i++) {
174
			$this->getCell($i, $colIndex)->textRightAligned();
175
		}
176
		return $this;
177
	}
178
179
	/**
180
	 * Returns the number of rows (TR)
181
	 * @return int
182
	 */
183
	public function getRowCount() {
184
		return $this->count();
185
	}
186
187
	/**
188
	 * Returns the number of columns (TD)
189
	 * @return int
190
	 */
191
	public function getColCount() {
192
		$result=0;
193
		if ($this->count() > 0)
194
			$result=$this->getItem(0)->getColCount();
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 getColCount() does only exist in the following sub-classes of Ajax\common\html\HtmlDoubleElement: 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...
195
		return $result;
196
	}
197
198
	/**
199
	 * Removes the cell at position $rowIndex,$colIndex
200
	 * @param int $rowIndex
201
	 * @param int $colIndex
202
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
203
	 */
204
	public function delete($rowIndex, $colIndex=NULL) {
205
		if (isset($colIndex)) {
206
			$row=$this->getItem($rowIndex);
207
			if (isset($row) === true) {
208
				$row->delete($colIndex);
209
			}
210
		} else {
211
			$this->removeItem($rowIndex);
212
		}
213
		return $this;
214
	}
215
216
	public function mergeCol($rowIndex=0, $colIndex=0) {
217
		return $this->getItem($rowIndex)->mergeCol($colIndex);
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...
218
	}
219
220
	public function mergeRow($rowIndex=0, $colIndex=0) {
221
		return $this->getItem($rowIndex)->mergeRow($colIndex);
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...
222
	}
223
224
	public function setFullWidth() {
225
		return $this->addToProperty("class", "full-width");
226
	}
227
}