Passed
Push — master ( fbbe5c...a34811 )
by Arkadiusz
07:00
created

src/Phpml/Clustering/KMeans/Point.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    public function __construct(array $coordinates)
22
    {
23
        $this->dimension = count($coordinates);
24
        $this->coordinates = $coordinates;
25
    }
26
27
    public function toArray(): array
28
    {
29
        return $this->coordinates;
30
    }
31
32
    /**
33
     * @return int|mixed
34
     */
35
    public function getDistanceWith(self $point, bool $precise = true)
36
    {
37
        $distance = 0;
38
        for ($n = 0; $n < $this->dimension; ++$n) {
39
            $difference = $this->coordinates[$n] - $point->coordinates[$n];
40
            $distance += $difference * $difference;
41
        }
42
43
        return $precise ? sqrt((float) $distance) : $distance;
44
    }
45
46
    /**
47
     * @return mixed
48
     */
49
    public function getClosest(array $points)
50
    {
51
        foreach ($points as $point) {
52
            $distance = $this->getDistanceWith($point, false);
53
54
            if (!isset($minDistance)) {
55
                $minDistance = $distance;
56
                $minPoint = $point;
57
                continue;
58
            }
59
60
            if ($distance < $minDistance) {
61
                $minDistance = $distance;
62
                $minPoint = $point;
63
            }
64
        }
65
66
        return $minPoint;
0 ignored issues
show
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...
67
    }
68
69
    public function getCoordinates(): array
70
    {
71
        return $this->coordinates;
72
    }
73
74
    /**
75
     * @param mixed $offset
76
     */
77
    public function offsetExists($offset): bool
78
    {
79
        return isset($this->coordinates[$offset]);
80
    }
81
82
    /**
83
     * @param mixed $offset
84
     *
85
     * @return mixed
86
     */
87
    public function offsetGet($offset)
88
    {
89
        return $this->coordinates[$offset];
90
    }
91
92
    /**
93
     * @param mixed $offset
94
     * @param mixed $value
95
     */
96
    public function offsetSet($offset, $value): void
97
    {
98
        $this->coordinates[$offset] = $value;
99
    }
100
101
    /**
102
     * @param mixed $offset
103
     */
104
    public function offsetUnset($offset): void
105
    {
106
        unset($this->coordinates[$offset]);
107
    }
108
}
109