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

Table   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 117
Duplicated Lines 6.84 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 27
c 2
b 0
f 0
lcom 1
cbo 3
dl 8
loc 117
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getRows() 0 4 1
A addRow() 0 8 2
A removeRow() 0 11 3
A getRow() 0 4 2
A insertRows() 8 8 2
A getCellByPosition() 0 6 2
D getPositionBefore() 0 35 9
B getPositionAfter() 0 27 6

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 Table extends AbstractTableElement
6
{
7
    protected $rows = array();
8
9
    protected $domNode;
10
11
    public function getRows()
12
    {
13
        return $this->rows;
14
    }
15
16
    public function addRow(TableRow $row)
17
    {
18
        $this->rows[] = $row;
19
20
        if (!$row->getTable()) {
21
            $row->setTable($this);
22
        }
23
    }
24
25
    public function removeRow(TableRow $row)
26
    {
27
        $key = array_search($row, $this->rows, true);
28
29
        if ($key !== false) {
30
            unset($this->rows[$key]);
31
            if ($row->getTable()) {
32
                $row->setTable(null);
33
            }
34
        }
35
    }
36
37
    public function getRow($index)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
38
    {
39
        return isset($this->rows[$index]) ? $this->rows[$index] : null;
40
    }
41
42 View Code Duplication
    public function insertRows($rows, $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...
43
    {
44
        if ($position === null) {
45
            $this->rows = array_merge($this->rows, $rows);
46
        } else {
47
            array_splice($this->rows, $position, 0, $rows);
48
        }
49
    }
50
51
    public function getCellByPosition(TablePosition $position)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
52
    {
53
        $row = $this->getRow($position->getRow());
54
55
        return $row ? $row->getCell($position->getCell()) : null;
56
    }
57
58
    public function getPositionBefore(TablePosition $position, $offset = 1)
59
    {
60
        if ($position->getCell() > ($offset - 1)) {
61
            $newRow = $position->getRow();
62
            $newCell = $position->getCell() - $offset;
63
        } elseif ($position->getRow() > 0) {
64
            $cellsToMove = $offset;
65
            $newRow = $position->getRow();
66
            $newCell = $position->getCell();
67
68
            while ($cellsToMove > 0 && $newRow >= 0) {
69
                if ($cellsToMove > $newCell) {
70
                    $newRow--;
71
                    if ($newRow < 0) {
72
                        return null;
73
                    }
74
                    
75
                    $cellsToMove = $cellsToMove - ($newCell + 1);
76
                    $cellCount = count($this->getRow($newRow)->getCells());
77
                    $newCell = $cellCount - 1;
78
                } else {
79
                    $newCell = $newCell - $cellsToMove;
80
                    $cellsToMove -= $newCell;
81
                }
82
            }
83
        } else {
84
            return null;
85
        }
86
87
        if ($newRow >= 0 && $newCell >= 0) {
88
            return new TablePosition($newRow, $newCell);
89
        }
90
91
        return null;
92
    }
93
94
    public function getPositionAfter(TablePosition $position, $offset = 1)
95
    {
96
        $cellsToMove = $offset;
97
        $newRow = $position->getRow();
98
        $newCell = $position->getCell();
99
100
        while ($cellsToMove > 0 && $newRow < count($this->rows)) {
101
            $cellCount = count($this->getRow($newRow)->getCells());
102
103
            $cellsLeft = $cellCount - $newCell - 1;
104
105
            if ($cellsToMove > $cellsLeft) {
106
                $newRow++;
107
                $cellsToMove -= $cellsLeft - 1;
108
                $newCell = 0;
109
            } else {
110
                $newCell = $newCell + $cellsToMove;
111
                $cellsToMove -= $cellsLeft;
112
            }
113
        }
114
115
        if ($newRow >= 0 && $newCell >= 0) {
116
            return new TablePosition($newRow, $newCell);
117
        }
118
119
        return null;
120
    }
121
}
122