Passed
Pull Request — master (#31)
by Josh
03:22
created

TableRow   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 8.16 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 13
c 5
b 0
f 1
lcom 2
cbo 2
dl 8
loc 98
ccs 0
cts 33
cp 0
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
/**
6
 * Class TableRow
7
 * @package Caxy\HtmlDiff\Table
8
 */
9
class TableRow extends AbstractTableElement
10
{
11
    /**
12
     * @var Table
13
     */
14
    protected $table;
15
16
    /**
17
     * @var array
18
     */
19
    protected $cells = array();
20
21
    /**
22
     * @return Table
23
     */
24
    public function getTable()
25
    {
26
        return $this->table;
27
    }
28
29
    /**
30
     * @param Table|null $table
31
     *
32
     * @return $this
33
     */
34
    public function setTable(Table $table = null)
35
    {
36
        $this->table = $table;
37
38
        if (!in_array($this, $table->getRows())) {
39
            $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...
40
        }
41
42
        return $this;
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function getCells()
49
    {
50
        return $this->cells;
51
    }
52
53
    /**
54
     * @param TableCell $cell
55
     *
56
     * @return $this
57
     */
58
    public function addCell(TableCell $cell)
59
    {
60
        $this->cells[] = $cell;
61
62
        if (!$cell->getRow()) {
63
            $cell->setRow($this);
64
        }
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param TableCell $cell
71
     */
72
    public function removeCell(TableCell $cell)
73
    {
74
        $key = array_search($cell, $this->cells, true);
75
76
        if ($key !== false) {
77
            unset($this->cells[$key]);
78
            if ($cell->getRow()) {
79
                $cell->setRow(null);
80
            }
81
        }
82
    }
83
84
    /**
85
     * @param int $index
86
     *
87
     * @return TableCell|null
88
     */
89
    public function getCell($index)
90
    {
91
        return isset($this->cells[$index]) ? $this->cells[$index] : null;
92
    }
93
94
    /**
95
     * @param array    $cells
96
     * @param null|int $position
97
     */
98 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...
99
    {
100
        if ($position === null) {
101
            $this->cells = array_merge($this->cells, $cells);
102
        } else {
103
            array_splice($this->cells, $position, 0, $cells);
104
        }
105
    }
106
}
107