Completed
Push — master ( 266bb3...ba64c6 )
by Cas
02:06 queued 12s
created

Matrix::createFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace CL\Matrix;
4
5
use CL\Matrix\Vector\Column;
6
use CL\Matrix\Vector\Row;
7
8
class Matrix
9
{
10
    /**
11
     * @var Row[]
12
     */
13
    private $rows = [];
14
15
    /**
16
     * @var Column[]
17
     */
18
    private $columns = [];
19
20
    /**
21
     * @param Row[] $rowVectors
22
     */
23 20
    public function __construct(array $rowVectors = [])
24
    {
25 20
        foreach ($rowVectors as $rowVector) {
26
            $this->addRow($rowVector);
27 20
        }
28 20
    }
29
30
    /**
31
     * @param Row $row
32
     */
33 6 View Code Duplication
    public function addRow(Row $row)
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...
34
    {
35 6
        array_push($this->rows, $row);
36
37 6
        $keys = array_keys($this->rows);
38 6
        $offset = end($keys);
39
40 6
        $row->setMatrix($this, $offset);
41 6
    }
42
43
    /**
44
     * @param int $offset
45
     * @param Row $row
46
     */
47 6
    public function setRow($offset, Row $row)
48
    {
49 6
        $row->setMatrix($this, $offset);
50
51 6
        $this->rows[$offset] = $row;
52 6
    }
53
54
    /**
55
     * @param int $offset
56
     *
57
     * @return Row
58
     */
59 10 View Code Duplication
    public function getRow($offset)
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...
60
    {
61 10
        if (!array_key_exists($offset, $this->rows)) {
62 2
            throw new \InvalidArgumentException(sprintf(
63 2
                'There is no row in this matrix with that offset: %s',
64
                $offset
65 2
            ));
66
        }
67
68 8
        return $this->rows[$offset];
69
    }
70
71
    /**
72
     * @return Row[]
73
     */
74 10
    public function getRows()
75
    {
76 10
        return $this->rows;
77
    }
78
79
    /**
80
     * @param int $offset
81
     *
82
     * @return bool
83
     */
84 8
    public function hasRow($offset)
85
    {
86 8
        return array_key_exists($offset, $this->rows);
87
    }
88
89
    /**
90
     * @param Column $column
91
     */
92 6 View Code Duplication
    public function addColumn(Column $column)
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...
93
    {
94 6
        array_push($this->columns, $column);
95
96 6
        $keys = array_keys($this->columns);
97 6
        $offset = end($keys);
98
99 6
        $column->setMatrix($this, $offset);
100 6
    }
101
102
    /**
103
     * @param int    $offset
104
     * @param Column $column
105
     */
106 6
    public function setColumn($offset, Column $column)
107
    {
108 6
        $column->setMatrix($this, $offset);
109
110 6
        $this->columns[$offset] = $column;
111 6
    }
112
113
    /**
114
     * @param int $offset
115
     *
116
     * @return Column
117
     */
118 10 View Code Duplication
    public function getColumn($offset)
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...
119
    {
120 10
        if (!array_key_exists($offset, $this->columns)) {
121 2
            throw new \InvalidArgumentException(sprintf(
122 2
                'There is no column in this matrix with that offset: %s',
123
                $offset
124 2
            ));
125
        }
126
127 8
        return $this->columns[$offset];
128
    }
129
130
    /**
131
     * @return Column[]
132
     */
133 10
    public function getColumns()
134
    {
135 10
        return $this->columns;
136
    }
137
138
    /**
139
     * @param int $offset
140
     *
141
     * @return bool
142
     */
143 8
    public function hasColumn($offset)
144
    {
145 8
        return array_key_exists($offset, $this->columns);
146
    }
147
148
    /**
149
     * @param array $rows
150
     *
151
     * @return Matrix
152
     */
153 2
    public static function createFromArray(array $rows)
154
    {
155 2
        $matrix = new Matrix();
156
157 2
        foreach ($rows as $offset => $row) {
158 2
            $matrix->setRow($offset, Row::createFromArray($row));
0 ignored issues
show
Bug introduced by
It seems like \CL\Matrix\Vector\Row::createFromArray($row) targeting CL\Matrix\Vector\AbstractVector::createFromArray() can also be of type object<CL\Matrix\Vector\Column>; however, CL\Matrix\Matrix::setRow() does only seem to accept object<CL\Matrix\Vector\Row>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
159 2
        }
160
161 2
        return $matrix;
162
    }
163
}
164