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

TableRow::getCell()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 6
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 TableCell[]
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 TableCell[]
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->cells, $cells) of type array is incompatible with the declared type array<integer,object<Cax...lDiff\Table\TableCell>> of property $cells.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
102
        } else {
103
            array_splice($this->cells, $position, 0, $cells);
104
        }
105
    }
106
}
107