Completed
Push — master ( d791dd...2d8ee5 )
by Ventaquil
02:14
created

Line::getDimensionNumber()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace PHPAlgorithms\GraphTools;
4
5
use PHPAlgorithms\GraphTools\Exceptions\LineException;
6
7
abstract class Line {
8
    private static function checkPoint($point)
9
    {
10
        if (!($point instanceof AbstractPoint)) {
11
            throw new LineException('This is not a point');
12
        }
13
    }
14
15
    private static function getDimensionNumber($point)
16
    {
17
        $pointDimensions = array(AbstractPoint::class, Point1D::class, Point2D::class, Point3D::class, Point4D::class);
18
19
        foreach ($pointDimensions as $dimension => $pointDimension)
20
        {
21
            if (!($point instanceof $pointDimension)) {
22
                return --$dimension;
23
            }
24
        }
25
26
        return $dimension;
0 ignored issues
show
Bug introduced by
The variable $dimension seems to be defined by a foreach iteration on line 19. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
27
    }
28
29
    private static function getLowestPointsDimension($from, $to)
30
    {
31
        $fromDimension = self::getDimensionNumber($from);
32
        $toDimension = self::getDimensionNumber($to);
33
34
        switch (true) {
35
            case $fromDimension <= $toDimension:
36
                return $fromDimension;
37
            case $fromDimension > $toDimension:
38
                return $toDimension;
39
        }
40
    }
41
42
    public static function create($from, $to)
43
    {
44
        self::checkPoint($from);
45
        self::checkPoint($to);
46
47
        $lowestDimension = self::getLowestPointsDimension($from, $to);
48
49
        switch ($lowestDimension) {
50
            case 0:
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $lowestDimension of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
51
                return new AbstractLine($from, $to);
52
            case 1:
53
                return new Line1D($from, $to);
54
            case 2:
55
                return new Line2D($from, $to);
56
            case 3:
57
                return new Line3D($from, $to);
58
            case 4:
59
                return new Line4D($from, $to);
60
            default:
61
                throw new LineException('Unknown dimension');
62
        }
63
    }
64
}
65