Completed
Push — develop ( 01a249...bb9e1a )
by Arkadiusz
02:44
created

Point::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\Clustering\KMeans;
6
7
use ArrayAccess;
8
9
class Point implements ArrayAccess
10
{
11
    /**
12
     * @var int
13
     */
14
    protected $dimension;
15
16
    /**
17
     * @var array
18
     */
19
    protected $coordinates;
20
21
    /**
22
     * @param array $coordinates
23
     */
24
    public function __construct(array $coordinates)
25
    {
26
        $this->dimension = count($coordinates);
27
        $this->coordinates = $coordinates;
28
    }
29
30
    /**
31
     * @return array
32
     */
33
    public function toArray()
34
    {
35
        return $this->coordinates;
36
    }
37
38
    /**
39
     * @param Point $point
40
     * @param bool  $precise
41
     *
42
     * @return int|mixed
43
     */
44
    public function getDistanceWith(self $point, $precise = true)
45
    {
46
        $distance = 0;
47
        for ($n = 0; $n < $this->dimension; ++$n) {
48
            $difference = $this->coordinates[$n] - $point->coordinates[$n];
49
            $distance  += $difference * $difference;
50
        }
51
52
        return $precise ? sqrt($distance) : $distance;
53
    }
54
55
    /**
56
     * @param $points
57
     *
58
     * @return mixed
59
     */
60
    public function getClosest($points)
61
    {
62
        foreach ($points as $point) {
63
            $distance = $this->getDistanceWith($point, false);
64
65
            if (!isset($minDistance)) {
66
                $minDistance = $distance;
67
                $minPoint = $point;
68
                continue;
69
            }
70
71
            if ($distance < $minDistance) {
72
                $minDistance = $distance;
73
                $minPoint = $point;
74
            }
75
        }
76
77
        return $minPoint;
0 ignored issues
show
Bug introduced by
The variable $minPoint does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function getCoordinates()
84
    {
85
        return $this->coordinates;
86
    }
87
88
    /**
89
     * @param mixed $offset
90
     *
91
     * @return bool
92
     */
93
    public function offsetExists($offset)
94
    {
95
        return isset($this->coordinates[$offset]);
96
    }
97
98
    /**
99
     * @param mixed $offset
100
     *
101
     * @return mixed
102
     */
103
    public function offsetGet($offset)
104
    {
105
        return $this->coordinates[$offset];
106
    }
107
108
    /**
109
     * @param mixed $offset
110
     * @param mixed $value
111
     */
112
    public function offsetSet($offset, $value)
113
    {
114
        $this->coordinates[$offset] = $value;
115
    }
116
117
    /**
118
     * @param mixed $offset
119
     */
120
    public function offsetUnset($offset)
121
    {
122
        unset($this->coordinates[$offset]);
123
    }
124
}
125