Completed
Push — master ( 3bd89b...647639 )
by Cas
14:54
created

AbstractVector::getElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace CL\Matrix\Vector;
4
5
use CL\Matrix\Element;
6
use CL\Matrix\Matrix;
7
8
abstract class AbstractVector implements VectorInterface
9
{
10
    /**
11
     * @var Element[]
12
     */
13
    protected $elements = [];
14
15
    /**
16
     * @var Matrix|null
17
     */
18
    protected $matrix;
19
20
    /**
21
     * @var int|null
22
     */
23
    protected $matrixOffset;
24
25
    /**
26
     * @param Element[] $elements
27
     * @param Matrix    $matrix
28
     * @param int|null  $matrixOffset
29
     */
30
    public function __construct(array $elements = [], Matrix $matrix = null, $matrixOffset = null)
31
    {
32
        foreach ($elements as $offset => $element) {
33
            $this->setElement($offset, $element);
34
        }
35
36
        if ($matrix) {
37
            if (!$matrixOffset) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matrixOffset of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
38
                throw new \InvalidArgumentException('You must also supply a matrix offset when setting the matrix');
39
            }
40
41
            $this->setMatrix($matrix, $matrixOffset);
42
        }
43
    }
44
45
    /**
46
     * @param int $offset
47
     *
48
     * @return Element
49
     */
50 View Code Duplication
    public function getElement($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...
51
    {
52
        if (!array_key_exists($offset, $this->elements)) {
53
            throw new \InvalidArgumentException(sprintf(
54
                'There is no element in this vector with that offset: %s',
55
                $offset
56
            ));
57
        }
58
59
        return $this->elements[$offset];
60
    }
61
62
    /**
63
     * @return Element[]
64
     */
65
    public function getElements()
66
    {
67
        return $this->elements;
68
    }
69
}
70