AbstractVector   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 17.74 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 77.78%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 7
c 3
b 1
f 0
lcom 1
cbo 0
dl 11
loc 62
ccs 14
cts 18
cp 0.7778
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
A getElement() 11 11 2
A getElements() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 34
    public function __construct(array $elements = [], Matrix $matrix = null, $matrixOffset = null)
31
    {
32 34
        foreach ($elements as $offset => $element) {
33 8
            $this->setElement($offset, $element);
34 34
        }
35
36 34
        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 34
    }
44
45
    /**
46
     * @param int $offset
47
     *
48
     * @return Element
49
     */
50 14 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 14
        if (!array_key_exists($offset, $this->elements)) {
53 4
            throw new \InvalidArgumentException(sprintf(
54 4
                'There is no element in this vector with that offset: %s',
55
                $offset
56 4
            ));
57
        }
58
59 10
        return $this->elements[$offset];
60
    }
61
62
    /**
63
     * @return Element[]
64
     */
65 18
    public function getElements()
66
    {
67 18
        return $this->elements;
68
    }
69
}
70