Completed
Push — master ( ab40cf...ed5e37 )
by Marcus
06:16
created

getCardinalDirectionDistances()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 65

Duplication

Lines 18
Ratio 27.69 %

Importance

Changes 0
Metric Value
dl 18
loc 65
rs 6.8969
c 0
b 0
f 0
cc 10
nc 10
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Location\CardinalDirection;
6
7
use Location\Bounds;
8
use Location\Coordinate;
9
use Location\Distance\DistanceInterface;
10
11
class CardinalDirectionDistancesCalculator
12
{
13
    public function getCardinalDirectionDistances(
14
        Coordinate $point1,
15
        Coordinate $point2,
16
        DistanceInterface $distanceCalculator
17
    ): CardinalDirectionDistances {
18
        $cardinalDirection = (new CardinalDirection())->getCardinalDirection($point1, $point2);
19
        $directDistance = $point1->getDistance($point2, $distanceCalculator);
20
21
        switch ($cardinalDirection) {
22
            case CardinalDirection::CARDINAL_DIRECTION_NONE:
23
                return CardinalDirectionDistances::create();
24
25
            case CardinalDirection::CARDINAL_DIRECTION_NORTH:
26
                return CardinalDirectionDistances::create()->setSouth($directDistance);
27
28
            case CardinalDirection::CARDINAL_DIRECTION_EAST:
29
                return CardinalDirectionDistances::create()->setWest($directDistance);
30
31
            case CardinalDirection::CARDINAL_DIRECTION_SOUTH:
32
                return CardinalDirectionDistances::create()->setNorth($directDistance);
33
34
            case CardinalDirection::CARDINAL_DIRECTION_WEST:
35
                return CardinalDirectionDistances::create()->setEast($directDistance);
36
37
            case CardinalDirection::CARDINAL_DIRECTION_NORTHWEST:
38
                $bounds = new Bounds($point1, $point2);
39
                $point3 = new Coordinate($bounds->getNorth(), $bounds->getEast());
40
41
                return CardinalDirectionDistances::create()
42
                    ->setEast($point1->getDistance($point3, $distanceCalculator))
43
                    ->setSouth($point3->getDistance($point2, $distanceCalculator));
44
45 View Code Duplication
            case CardinalDirection::CARDINAL_DIRECTION_SOUTHWEST:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
                $bounds = new Bounds(
47
                    new Coordinate($point2->getLat(), $point1->getLng()),
48
                    new Coordinate($point1->getLat(), $point2->getLng())
49
                );
50
                $point3 = new Coordinate($bounds->getSouth(), $bounds->getEast());
51
52
                return CardinalDirectionDistances::create()
53
                    ->setNorth($point3->getDistance($point2, $distanceCalculator))
54
                    ->setEast($point1->getDistance($point3, $distanceCalculator));
55
56 View Code Duplication
            case CardinalDirection::CARDINAL_DIRECTION_NORTHEAST:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
                $bounds = new Bounds(
58
                    new Coordinate($point1->getLat(), $point2->getLng()),
59
                    new Coordinate($point2->getLat(), $point1->getLng())
60
                );
61
                $point3 = new Coordinate($bounds->getNorth(), $bounds->getWest());
62
63
                return CardinalDirectionDistances::create()
64
                    ->setSouth($point3->getDistance($point2, $distanceCalculator))
65
                    ->setWest($point1->getDistance($point3, $distanceCalculator));
66
67
            case CardinalDirection::CARDINAL_DIRECTION_SOUTHEAST:
68
                $bounds = new Bounds($point2, $point1);
69
                $point3 = new Coordinate($bounds->getSouth(), $bounds->getWest());
70
71
                return CardinalDirectionDistances::create()
72
                    ->setNorth($point3->getDistance($point2, $distanceCalculator))
73
                    ->setWest($point1->getDistance($point3, $distanceCalculator));
74
        }
75
76
        return CardinalDirectionDistances::create();
77
    }
78
}
79