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

Table::getPositionBefore()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 35
ccs 0
cts 25
cp 0
rs 4.909
cc 9
eloc 24
nc 6
nop 2
crap 90
1
<?php
2
3
namespace Caxy\HtmlDiff\Table;
4
5
/**
6
 * Class Table
7
 * @package Caxy\HtmlDiff\Table
8
 */
9
class Table extends AbstractTableElement
10
{
11
    /**
12
     * @var TableRow[]
13
     */
14
    protected $rows = array();
15
16
    /**
17
     * @return TableRow[]
18
     */
19
    public function getRows()
20
    {
21
        return $this->rows;
22
    }
23
24
    /**
25
     * @param TableRow $row
26
     */
27
    public function addRow(TableRow $row)
28
    {
29
        $this->rows[] = $row;
30
31
        if (!$row->getTable()) {
32
            $row->setTable($this);
33
        }
34
    }
35
36
    /**
37
     * @param TableRow $row
38
     */
39
    public function removeRow(TableRow $row)
40
    {
41
        $key = array_search($row, $this->rows, true);
42
43
        if ($key !== false) {
44
            unset($this->rows[$key]);
45
            if ($row->getTable()) {
46
                $row->setTable(null);
47
            }
48
        }
49
    }
50
51
    /**
52
     * @param int $index
53
     *
54
     * @return null|TableRow
55
     */
56
    public function getRow($index)
57
    {
58
        return isset($this->rows[$index]) ? $this->rows[$index] : null;
59
    }
60
61
    /**
62
     * @param TableRow[] $rows
63
     * @param null|int   $position
64
     */
65 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...
66
    {
67
        if ($position === null) {
68
            $this->rows = array_merge($this->rows, $rows);
69
        } else {
70
            array_splice($this->rows, $position, 0, $rows);
71
        }
72
    }
73
74
    /**
75
     * @param TablePosition $position
76
     *
77
     * @return null|TableCell
78
     */
79
    public function getCellByPosition(TablePosition $position)
80
    {
81
        $row = $this->getRow($position->getRow());
82
83
        return $row ? $row->getCell($position->getCell()) : null;
84
    }
85
86
    /**
87
     * @param TablePosition $position
88
     * @param int           $offset
89
     *
90
     * @return TablePosition|null
91
     */
92
    public function getPositionBefore(TablePosition $position, $offset = 1)
93
    {
94
        if ($position->getCell() > ($offset - 1)) {
95
            $newRow = $position->getRow();
96
            $newCell = $position->getCell() - $offset;
97
        } elseif ($position->getRow() > 0) {
98
            $cellsToMove = $offset;
99
            $newRow = $position->getRow();
100
            $newCell = $position->getCell();
101
102
            while ($cellsToMove > 0 && $newRow >= 0) {
103
                if ($cellsToMove > $newCell) {
104
                    $newRow--;
105
                    if ($newRow < 0) {
106
                        return null;
107
                    }
108
                    
109
                    $cellsToMove = $cellsToMove - ($newCell + 1);
110
                    $cellCount = count($this->getRow($newRow)->getCells());
111
                    $newCell = $cellCount - 1;
112
                } else {
113
                    $newCell = $newCell - $cellsToMove;
114
                    $cellsToMove -= $newCell;
115
                }
116
            }
117
        } else {
118
            return null;
119
        }
120
121
        if ($newRow >= 0 && $newCell >= 0) {
122
            return new TablePosition($newRow, $newCell);
123
        }
124
125
        return null;
126
    }
127
128
    /**
129
     * @param TablePosition $position
130
     * @param int           $offset
131
     *
132
     * @return TablePosition|null
133
     */
134
    public function getPositionAfter(TablePosition $position, $offset = 1)
135
    {
136
        $cellsToMove = $offset;
137
        $newRow = $position->getRow();
138
        $newCell = $position->getCell();
139
140
        while ($cellsToMove > 0 && $newRow < count($this->rows)) {
141
            $cellCount = count($this->getRow($newRow)->getCells());
142
143
            $cellsLeft = $cellCount - $newCell - 1;
144
145
            if ($cellsToMove > $cellsLeft) {
146
                $newRow++;
147
                $cellsToMove -= $cellsLeft - 1;
148
                $newCell = 0;
149
            } else {
150
                $newCell = $newCell + $cellsToMove;
151
                $cellsToMove -= $cellsLeft;
152
            }
153
        }
154
155
        if ($newRow >= 0 && $newCell >= 0) {
156
            return new TablePosition($newRow, $newCell);
157
        }
158
159
        return null;
160
    }
161
}
162