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; |
|
|
|
|
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
|
|
|
|
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: