Point::offsetExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of PHP K-Means
5
 *
6
 * Copyright (c) 2014 Benjamin Delespierre
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is furnished
13
 * to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in all
16
 * copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace KMeans;
28
29
class Point implements \ArrayAccess
30
{
31
    protected $space;
32
    protected $dimention;
33
    protected $coordinates;
34
35
    public function __construct(Space $space, array $coordinates)
36
    {
37
        $this->space       = $space;
38
        $this->dimention   = $space->getDimention();
39
        $this->coordinates = $coordinates;
40
    }
41
42
    public function toArray(): array
43
    {
44
        return [
45
            'coordinates' => $this->coordinates,
46
            'data' => isset($this->space[$this]) ? $this->space[$this] : null,
47
        ];
48
    }
49
50
    public function getDistanceWith(self $point, bool $precise = true): float
51
    {
52
        if ($point->space !== $this->space) {
53
            throw new \LogicException("can only calculate distances from points in the same space");
54
        }
55
56
        $distance = 0;
57
        for ($n = 0; $n < $this->dimention; $n++) {
58
            $difference = $this->coordinates[$n] - $point->coordinates[$n];
59
            $distance  += $difference * $difference;
60
        }
61
62
        return $precise ? sqrt($distance) : $distance;
63
    }
64
65
    public function getClosest(iterable $points): Point
66
    {
67
        foreach ($points as $point) {
68
            $distance = $this->getDistanceWith($point, false);
69
70
            if (!isset($minDistance)) {
71
                $minDistance = $distance;
72
                $minPoint    = $point;
73
                continue;
74
            }
75
76
            if ($distance < $minDistance) {
77
                $minDistance = $distance;
78
                $minPoint    = $point;
79
            }
80
        }
81
82
        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...
83
    }
84
85
    public function belongsTo(Space $space): bool
86
    {
87
        return $this->space === $space;
88
    }
89
90
    public function getSpace(): Space
91
    {
92
        return $this->space;
93
    }
94
95
    public function getCoordinates(): array
96
    {
97
        return $this->coordinates;
98
    }
99
100
    public function offsetExists($offset): bool
101
    {
102
        return isset($this->coordinates[$offset]);
103
    }
104
105
    public function offsetGet($offset)
106
    {
107
        return $this->coordinates[$offset];
108
    }
109
110
    public function offsetSet($offset, $value): void
111
    {
112
        $this->coordinates[$offset] = $value;
113
    }
114
115
    public function offsetUnset($offset): void
116
    {
117
        unset($this->coordinates[$offset]);
118
    }
119
}
120