Completed
Pull Request — master (#13)
by Benjamin
02:12
created

Point   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 2
dl 0
loc 86
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getClosest() 0 19 4
A belongsTo() 0 4 1
A getSpace() 0 4 1
A getCoordinates() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A __construct() 0 6 2
A setDistanceAlgorithm() 0 4 1
A toArray() 0 7 2
A getDistanceWith() 0 4 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
use KMeans\Algorithms\EuclidianDistance;
30
use KMeans\Interfaces\DistanceAlgorithmInterface;
31
use KMeans\Interfaces\PointInterface;
32
use KMeans\Interfaces\SpaceInterface;
33
34
class Point implements PointInterface, \ArrayAccess
35
{
36
    protected $space;
37
    protected $coordinates;
38
    protected $distanceAlgo;
39
40
    public function __construct(SpaceInterface $space, array $coordinates, DistanceAlgorithmInterface $algo = null)
41
    {
42
        $this->space = $space;
43
        $this->coordinates = $coordinates;
44
        $this->setDistanceAlgorithm($algo ?: new EuclidianDistance());
45
    }
46
47
    public function setDistanceAlgorithm(DistanceAlgorithmInterface $algo): void
48
    {
49
        $this->distanceAlgo = $algo;
50
    }
51
52
    public function toArray(): array
53
    {
54
        return [
55
            'coordinates' => $this->coordinates,
56
            'data' => isset($this->space[$this]) ? $this->space[$this] : null,
57
        ];
58
    }
59
60
    public function getDistanceWith(self $point): float
61
    {
62
        return $this->distanceAlgo->getDistanceBetween($this, $point);
63
    }
64
65
    public function getClosest(iterable $points): Point
66
    {
67
        foreach ($points as $point) {
68
            $distance = $this->getDistanceWith($point, false);
0 ignored issues
show
Unused Code introduced by
The call to Point::getDistanceWith() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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