Completed
Push — master ( 66af5c...1d23a1 )
by Jean-Christophe
03:38
created

HtmlTableContent::setColValues()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 3
eloc 8
nc 4
nop 2
1
<?php
2
3
namespace Ajax\semantic\html\content\table;
4
5
use Ajax\semantic\html\base\HtmlSemCollection;
6
7
/**
8
 * a table content (thead, tbody or tfoot)
9
 * @author jc
10
 *
11
 */
12
class HtmlTableContent extends HtmlSemCollection{
13
14
	protected $_tdTagNames=["thead"=>"th","tbody"=>"td","tfoot"=>"th"];
15
16
	/**
17
	 * @param string $identifier
18
	 * @param string $tagName
19
	 * @param int $rowCount
20
	 * @param int $colCount
21
	 */
22
	public function __construct( $identifier,$tagName="tbody",$rowCount=NULL,$colCount=NULL){
23
		parent::__construct( $identifier, $tagName, "");
24
		if(isset($rowCount) && isset($colCount))
25
			$this->setRowCount($rowCount, $colCount);
26
	}
27
28
	/**
29
	 * @param int $rowCount
30
	 * @param int $colCount
31
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
32
	 */
33
	public function setRowCount($rowCount,$colCount){
34
		$count=$this->count();
35
		for($i=$count;$i<$rowCount;$i++){
36
			$this->addItem($colCount);
37
		}
38
		for($i=0;$i<$rowCount;$i++){
39
			$item=$this->content[$i];
40
			$item->setTdTagName($this->_tdTagNames[$this->tagName]);
41
			$this->content[$i]->setColCount($colCount);
42
		}
43
		return $this;
44
	}
45
46
	/**
47
	 * {@inheritDoc}
48
	 * @see \Ajax\common\html\HtmlCollection::createItem()
49
	 */
50
	protected function createItem($value){
51
		$count=$this->count();
52
		$tr= new HtmlTR("", $value);
53
		$tr->setContainer($this, $count);
54
		return $tr;
55
	}
56
57
	/**
58
	 * Returns the cell (HtmlTD) at position $row,$col
59
	 * @param int $row
60
	 * @param int $col
61
	 * @return \Ajax\semantic\html\content\HtmlTD
62
	 */
63
	public function getCell($row,$col){
64
		$row=$this->getItem($row);
65
		if(isset($row)){
66
			$col=$row->getItem($col);
67
		}
68
		return $col;
69
	}
70
71
	/**
72
	 * @param int $index
73
	 * @return \Ajax\semantic\html\content\HtmlTR
74
	 */
75
	public function getRow($index){
76
		return $this->getItem($index);
77
	}
78
79
	/**
80
	 * @param int $row
81
	 * @param int $col
82
	 * @param mixed $value
83
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
84
	 */
85
	public function setCellValue($row,$col,$value=""){
86
		$cell=$this->getCell($row, $col);
87
		if(isset($cell)===true){
88
			$cell->setValue($value);
89
		}
90
		return $this;
91
	}
92
93
	/**
94
	 * Sets the cells values
95
	 * @param mixed $values
96
	 */
97 View Code Duplication
	public function setValues($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...
98
		$count=$this->count();
99
		if(\is_array($values)===false){
100
			$values=\array_fill(0, $count, $values);
101
		}
102
		$count=\min(\sizeof($values),$count);
103
104
		for ($i=0;$i<$count;$i++){
105
			$row=$this->content[$i];
106
			$row->setValues($values[$i]);
107
		}
108
		return $this;
109
	}
110
111 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...
112
		$count=$this->count();
113
		if(\is_array($values)===false){
114
			$values=\array_fill(0, $count, $values);
115
		}
116
		$count=\min(\sizeof($values),$count);
117
		for ($i=0;$i<$count;$i++){
118
			$this->getCell($i, $colIndex)->setValue($values[$i]);
119
		}
120
		return $this;
121
	}
122
123
	public function colCenter($colIndex){
124
		$count=$this->count();
125
		for ($i=0;$i<$count;$i++){
126
			$this->getCell($i, $colIndex)->textCenterAligned();
127
		}
128
		return $this;
129
	}
130
131
	/**
132
	 * Returns the number of rows (TR)
133
	 * @return int
134
	 */
135
	public function getRowCount(){
136
		return $this->count();
137
	}
138
139
	/**
140
	 * Returns the number of columns (TD)
141
	 * @return int
142
	 */
143
	public function getColCount(){
144
		$result=0;
145
		if($this->count()>0)
146
			$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...
147
		return $result;
148
	}
149
150
	/**
151
	 * Removes the cell at position $rowIndex,$colIndex
152
	 * @param int $rowIndex
153
	 * @param int $colIndex
154
	 * @return \Ajax\semantic\html\content\table\HtmlTableContent
155
	 */
156
	public function delete($rowIndex,$colIndex=NULL){
157
		if(isset($colIndex)){
158
			$row=$this->getItem($rowIndex);
159
			if(isset($row)===true){
160
				$row->delete($colIndex);
161
			}
162
		}else{
163
			$this->removeItem($rowIndex);
164
		}
165
		return $this;
166
	}
167
168
	public function mergeCol($rowIndex=0,$colIndex=0){
169
		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...
170
	}
171
172
	public function mergeRow($rowIndex=0,$colIndex=0){
173
		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...
174
	}
175
176
177
178
}