Completed
Push — master ( d7d013...aba289 )
by Ventaquil
02:50
created

Line::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 2
eloc 14
nc 2
nop 2
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
        $argsToClass = array(
50
            'AbstractLine',
51
            'Line1D',
52
            'Line2D',
53
            'Line3D',
54
            'Line4D',
55
        );
56
57
        if (!isset($argsToClass[$lowestDimension])) {
58
            throw new LineException('Unknown dimension');
59
        }
60
61
        $className = __NAMESPACE__ . "\\{$argsToClass[$lowestDimension]}";
62
63
        return new $className($from, $to);
64
    }
65
}
66