VectorUtils   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 60
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 14 3
A angleBetween() 0 4 1
A subtract() 0 14 3
1
<?php
2
3
namespace PHP\Math\Vector;
4
5
use InvalidArgumentException;
6
7
class VectorUtils
8
{
9
    /**
10
     * Adds the two given vectors to each other.
11
     *
12
     * @param Vector $lft The left vector.
13
     * @param Vector $rgt The right vector.
14
     * @return Vector
15
     * @throws InvalidArgumentException Thrown when the given vector is of a different length.
16
     */
17 2
    public static function add(Vector $lft, Vector $rgt)
18
    {
19 2
        if ($lft->getSize() != $rgt->getSize()) {
20 1
            throw new InvalidArgumentException('Invalid vectors provided, should be of the same size.');
21
        }
22
23 1
        $result = new Vector($lft);
0 ignored issues
show
Documentation introduced by
$lft is of type object<PHP\Math\Vector\Vector>, but the function expects a array|object<Iterator>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
24
25 1
        for ($i = 0; $i < $lft->getSize(); ++$i) {
26 1
            $result[$i]->add($rgt[$i]);
27 1
        }
28
29 1
        return $result;
30
    }
31
32
    /**
33
     * Calculates the angle between the two given vectors.
34
     *
35
     * @param Vector $lft The left vector.
36
     * @param Vector $rgt The right vector.
37
     * @return float Returns the angle in radians.
38
     */
39 1
    public static function angleBetween(Vector $lft, Vector $rgt)
40
    {
41 1
        return acos($lft->dotProduct($rgt)->getValue());
42
    }
43
44
    /**
45
     * Subtracts the two given vectors from each other.
46
     *
47
     * @param Vector $lft The left vector.
48
     * @param Vector $rgt The right vector.
49
     * @return Vector
50
     * @throws InvalidArgumentException Thrown when the given vector is of a different length.
51
     */
52 2
    public static function subtract(Vector $lft, Vector $rgt)
53
    {
54 2
        if ($lft->getSize() != $rgt->getSize()) {
55 1
            throw new InvalidArgumentException('Invalid vectors provided, should be of the same size.');
56
        }
57
58 1
        $result = new Vector($lft);
0 ignored issues
show
Documentation introduced by
$lft is of type object<PHP\Math\Vector\Vector>, but the function expects a array|object<Iterator>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
60 1
        for ($i = 0; $i < $lft->getSize(); ++$i) {
61 1
            $result[$i]->subtract($rgt[$i]);
62 1
        }
63
64 1
        return $result;
65
    }
66
}
67