Completed
Push — master ( b569b1...d88325 )
by Jean-Christophe
03:46
created

HtmlTableContent   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 93
Duplicated Lines 12.9 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
c 1
b 0
f 0
lcom 1
cbo 2
dl 12
loc 93
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 3
A setRowCount() 0 12 3
A createItem() 0 6 1
A getCell() 0 7 2
A getRow() 0 3 1
A setCellValue() 0 7 2
A setValues() 12 12 3
A getRowCount() 0 3 1
A getColCount() 0 6 2
A delete() 0 11 3

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
7
class HtmlTableContent extends HtmlSemCollection{
8
9
	protected $_tdTagNames=["thead"=>"th","tbody"=>"td","tfoot"=>"th"];
10
11
	public function __construct( $identifier,$tagName="tbody",$rowCount=NULL,$colCount=NULL){
12
		parent::__construct( $identifier, $tagName, "");
13
		if(isset($rowCount) && isset($colCount))
14
			$this->setRowCount($rowCount, $colCount);
15
	}
16
17
	public function setRowCount($rowCount,$colCount){
18
		$count=$this->count();
19
		for($i=$count;$i<$rowCount;$i++){
20
			$this->addItem($colCount);
21
		}
22
		for($i=0;$i<$rowCount;$i++){
23
			$item=$this->content[$i];
24
			$item->setTdTagName($this->_tdTagNames[$this->tagName]);
25
			$this->content[$i]->setColCount($colCount);
26
		}
27
		return $this;
28
	}
29
30
	protected function createItem($value){
31
		$count=$this->count();
32
		$tr= new HtmlTR("", $value);
33
		$tr->setContainer($this, $count);
34
		return $tr;
35
	}
36
37
	/**
38
	 * Returns the cell (HtmlTD) at position $row,$col
39
	 * @param int $row
40
	 * @param int $col
41
	 * @return \Ajax\semantic\html\content\HtmlTD
42
	 */
43
	public function getCell($row,$col){
44
		$row=$this->getItem($row);
45
		if(isset($row)){
46
			$col=$row->getItem($col);
47
		}
48
		return $col;
49
	}
50
51
	public function getRow($index){
52
		return $this->getItem($index);
53
	}
54
55
	public function setCellValue($row,$col,$value=""){
56
		$cell=$this->getCell($row, $col);
57
		if(isset($cell)){
58
			$cell->setValue($value);
59
		}
60
		return $this;
61
	}
62
63 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...
64
		$count=$this->count();
65
		if(\is_array($values)===false){
66
			$values=\array_fill(0, $count, $values);
67
		}
68
		$count=\min(\sizeof($values),$count);
69
70
		for ($i=0;$i<$count;$i++){
71
			$row=$this->content[$i];
72
			$row->setValues($values[$i]);
73
		}
74
	}
75
76
	public function getRowCount(){
77
		return $this->count();
78
	}
79
80
	public function getColCount(){
81
		$result=0;
82
		if($this->count()>0)
83
			$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...
84
		return $result;
85
	}
86
87
	public function delete($rowIndex,$colIndex=NULL){
88
		if(isset($colIndex)){
89
			$row=$this->getItem($rowIndex);
90
			if(isset($row)===true){
91
				$row->delete($colIndex);
92
			}
93
		}else{
94
			$this->removeItem($rowIndex);
95
		}
96
		return $this;
97
	}
98
99
}