Point   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 10
eloc 15
c 2
b 1
f 0
dl 0
loc 59
ccs 18
cts 18
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getNthDimension() 0 6 3
A getDimensionsCount() 0 3 1
A validateDValues() 0 9 5
A __construct() 0 6 1
1
<?php
2
3
namespace Hexogen\KDTree;
4
5
use Hexogen\KDTree\Exception\ValidationException;
6
use Hexogen\KDTree\Interfaces\PointInterface;
7
8
class Point implements PointInterface
9
{
10
    private $dValues;
11
    private $length;
12
13
    /**
14
     * Item constructor.
15
     * @param array $dValues
16
     */
17 102
    public function __construct(array $dValues)
18
    {
19 102
        $this->length = count($dValues);
20 102
        $this->validateDValues($dValues);
21
22 102
        $this->dValues = $dValues;
23 102
    }
24
25
    /**
26
     * get nth dimension value from vector
27
     *
28
     * @api
29
     * @param int $d
30
     * @return float
31
     */
32 102
    public function getNthDimension(int $d): float
33
    {
34 102
        if ($d < 0 || $d >= $this->length) {
35 3
            throw new \OutOfRangeException('$d = ' . $d . '  should be between 0 and number of ' . $this->length);
36
        }
37 99
        return (float)$this->dValues[$d];
38
    }
39
40
41
    /**
42
     * validate multi dimension vector
43
     *
44
     * @param array $dValues
45
     * @throws ValidationException
46
     */
47 102
    private function validateDValues(array $dValues)
48
    {
49 102
        if ($this->length == 0) {
50 3
            throw new ValidationException('$dValues should be not empty');
51
        }
52
53 102
        for ($i = 0; $i < $this->length; $i++) {
54 102
            if (!isset($dValues[$i]) || !is_numeric($dValues[$i])) {
55 3
                throw new ValidationException('$dValues is not a simple array list');
56
            }
57
        }
58 102
    }
59
60
    /**
61
     * @api
62
     * @return int number of dimensions in the point
63
     */
64 105
    public function getDimensionsCount(): int
65
    {
66 105
        return $this->length;
67
    }
68
}
69