Completed
Push — master ( 1d23a1...916f6f )
by Jean-Christophe
03:30
created

HtmlTableContent::setRowValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
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($colCount);
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
	/**
49
	 *
50
	 * {@inheritDoc}
51
	 *
52
	 * @see \Ajax\common\html\HtmlCollection::createItem()
53
	 */
54
	protected function createItem($value) {
55
		$count=$this->count();
56
		$tr=new HtmlTR("", $value);
57
		$tr->setContainer($this, $count);
58
		return $tr;
59
	}
60
61
	/**
62
	 * Returns the cell (HtmlTD) at position $row,$col
63
	 * @param int $row
64
	 * @param int $col
65
	 * @return \Ajax\semantic\html\content\HtmlTD
66
	 */
67
	public function getCell($row, $col) {
68
		$row=$this->getItem($row);
69
		if (isset($row)) {
70
			$col=$row->getItem($col);
71
		}
72
		return $col;
73
	}
74
75
	/**
76
	 *
77
	 * @param int $index
78
	 * @return \Ajax\semantic\html\content\HtmlTR
79
	 */
80
	public function getRow($index) {
81
		return $this->getItem($index);
82
	}
83
84
	/**
85
	 *
86
	 * @param int $row
87
	 * @param int $col
88
	 * @param mixed $value
89
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
90
	 */
91
	public function setCellValue($row, $col, $value="") {
92
		$cell=$this->getCell($row, $col);
93
		if (isset($cell)===true) {
94
			$cell->setValue($value);
95
		}
96
		return $this;
97
	}
98
99
	/**
100
	 * Sets the cells values
101
	 * @param mixed $values
102
	 */
103
	public function setValues($values=array()) {
104
		$count=$this->count();
105
		$isArray=true;
106
		if (\is_array($values)===false) {
107
			$values=\array_fill(0, $count, $values);
108
			$isArray=false;
109
		}
110
		if (JArray::dimension($values)==1&&$isArray)
111
			$values=[ $values ];
112
		
113
		$count=\min(\sizeof($values), $count);
114
		
115
		for($i=0; $i<$count; $i++) {
116
			$row=$this->content[$i];
117
			$row->setValues($values[$i]);
118
		}
119
		return $this;
120
	}
121
122 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...
123
		$count=$this->count();
124
		if (\is_array($values)===false) {
125
			$values=\array_fill(0, $count, $values);
126
		}
127
		$count=\min(\sizeof($values), $count);
128
		for($i=0; $i<$count; $i++) {
129
			$this->getCell($i, $colIndex)->setValue($values[$i]);
130
		}
131
		return $this;
132
	}
133
134
	public function setRowValues($rowIndex, $values=array()) {
135
		$count=$this->count();
136
		if (\is_array($values)===false) {
137
			$values=\array_fill(0, $count, $values);
138
		}
139
		$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...
140
		return $this;
141
	}
142
143
	public function colCenter($colIndex) {
144
		$count=$this->count();
145
		for($i=0; $i<$count; $i++) {
146
			$this->getCell($i, $colIndex)->textCenterAligned();
147
		}
148
		return $this;
149
	}
150
151
	public function colRight($colIndex) {
152
		$count=$this->count();
153
		for($i=0; $i<$count; $i++) {
154
			$this->getCell($i, $colIndex)->textRightAligned();
155
		}
156
		return $this;
157
	}
158
159
	/**
160
	 * Returns the number of rows (TR)
161
	 * @return int
162
	 */
163
	public function getRowCount() {
164
		return $this->count();
165
	}
166
167
	/**
168
	 * Returns the number of columns (TD)
169
	 * @return int
170
	 */
171
	public function getColCount() {
172
		$result=0;
173
		if ($this->count()>0)
174
			$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...
175
		return $result;
176
	}
177
178
	/**
179
	 * Removes the cell at position $rowIndex,$colIndex
180
	 * @param int $rowIndex
181
	 * @param int $colIndex
182
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
183
	 */
184
	public function delete($rowIndex, $colIndex=NULL) {
185
		if (isset($colIndex)) {
186
			$row=$this->getItem($rowIndex);
187
			if (isset($row)===true) {
188
				$row->delete($colIndex);
189
			}
190
		} else {
191
			$this->removeItem($rowIndex);
192
		}
193
		return $this;
194
	}
195
196
	public function mergeCol($rowIndex=0, $colIndex=0) {
197
		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...
198
	}
199
200
	public function mergeRow($rowIndex=0, $colIndex=0) {
201
		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...
202
	}
203
}