Test Failed
Pull Request — master (#31)
by Josh
04:18
created

TableRow   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 79
Duplicated Lines 10.13 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 13
c 4
b 0
f 0
lcom 2
cbo 2
dl 8
loc 79
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getTable() 0 4 1
A setTable() 0 10 2
A getCells() 0 4 1
A addCell() 0 10 2
A removeCell() 0 11 3
A getCell() 0 4 2
A insertCells() 8 8 2

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 Caxy\HtmlDiff\Table;
4
5
class TableRow extends AbstractTableElement
6
{
7
    /**
8
     * @var Table
9
     */
10
    protected $table;
11
12
    /**
13
     * @var array
14
     */
15
    protected $cells = array();
16
    
17
    public function getTable()
18
    {
19
        return $this->table;
20
    }
21
22
    public function setTable(Table $table = null)
23
    {
24
        $this->table = $table;
25
26
        if (!in_array($this, $table->getRows())) {
27
            $table->addRow($this);
0 ignored issues
show
Bug introduced by
It seems like $table is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
28
        }
29
30
        return $this;
31
    }
32
33
    public function getCells()
34
    {
35
        return $this->cells;
36
    }
37
38
    public function addCell(TableCell $cell)
39
    {
40
        $this->cells[] = $cell;
41
42
        if (!$cell->getRow()) {
43
            $cell->setRow($this);
44
        }
45
46
        return $this;
47
    }
48
49
    public function removeCell(TableCell $cell)
50
    {
51
        $key = array_search($cell, $this->cells, true);
52
53
        if ($key !== false) {
54
            unset($this->cells[$key]);
55
            if ($cell->getRow()) {
56
                $cell->setRow(null);
57
            }
58
        }
59
    }
60
61
    /**
62
     * @param $index
63
     *
64
     * @return TableCell|null
65
     */
66
    public function getCell($index)
67
    {
68
        return isset($this->cells[$index]) ? $this->cells[$index] : null;
69
    }
70
71
    /**
72
     * @param array $cells
73
     * @param null  $position
74
     */
75 View Code Duplication
    public function insertCells($cells, $position = null)
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...
76
    {
77
        if ($position === null) {
78
            $this->cells = array_merge($this->cells, $cells);
79
        } else {
80
            array_splice($this->cells, $position, 0, $cells);
81
        }
82
    }
83
}
84