Completed
Push — master ( ba64c6...2afc46 )
by Cas
01:56
created

Row::createFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 10
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\Vector;
4
5
use CL\Matrix\Element;
6
use CL\Matrix\Matrix;
7
8 View Code Duplication
class Row extends AbstractVector
0 ignored issues
show
Duplication introduced by
This class 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...
9
{
10
    /**
11
     * @param Matrix $matrix
12
     * @param int    $matrixOffset
13
     */
14 12
    public function setMatrix(Matrix $matrix, $matrixOffset)
15
    {
16 12
        $this->matrix = $matrix;
17
18 12
        foreach ($this->getElements() as $offset => $element) {
19 4
            if (!$this->matrix->hasColumn($offset)) {
20 4
                $this->matrix->setColumn($offset, new Column([]));
21 4
            }
22
23 4
            $this->matrix->getColumn($offset)->addElement($element);
24 12
        }
25
26 12
        $this->matrixOffset = $matrixOffset;
27 12
    }
28
29
    /**
30
     * @param Element $element
31
     */
32 4
    public function addElement(Element $element)
33
    {
34 4
        array_push($this->elements, $element);
35
36 4
        $keys = array_keys($this->elements);
37 4
        $offset = end($keys);
38
39 4
        if ($this->matrix) {
40 4
            if ($this->matrix->hasColumn($offset)) {
41 2
                $this->matrix->getColumn($offset)->addElement($element);
42 2
            }
43 4
        }
44 4
    }
45
46
    /**
47
     * @param int     $offset
48
     * @param Element $element
49
     */
50 8
    public function setElement($offset, Element $element)
51
    {
52 8
        $this->elements[$offset] = $element;
53
54 8
        if ($this->matrix) {
55
            if ($this->matrix->hasColumn($offset)) {
56
                $this->matrix->getColumn($offset)->setElement($this->matrixOffset, $element);
57
            }
58
        }
59 8
    }
60
61
    /**
62
     * @param mixed[] $values
63
     *
64
     * @return Row
65
     */
66 4
    public static function createFromArray(array $values)
67
    {
68 4
        $vector = new self;
69
70 4
        foreach ($values as $offset => $value) {
71 4
            $vector->setElement($offset, new Element($value));
72 4
        }
73
74 4
        return $vector;
75
    }
76
}
77