Passed
Pull Request — master (#1837)
by Nico
24:51
created

DistanceCalculation::shipToColonyDistance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Map;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Stu\Orm\Entity\ColonyInterface;
9
use Stu\Orm\Entity\ShipInterface;
10
use Stu\Orm\Entity\StarSystemInterface;
11
12
class DistanceCalculation implements DistanceCalculationInterface
13
{
14 21
    #[Override]
15
    public function shipToShipDistance(ShipInterface $ship, ShipInterface $station): int
16
    {
17
18 21
        return $this->calculateAbsoluteDistance(
19 21
            $ship->getSystem(),
20 21
            $station->getSystem(),
21 21
            $ship->getPosX(),
22 21
            $ship->getPosY(),
23 21
            $station->getPosX(),
24 21
            $station->getPosY()
25 21
        );
26
    }
27
28 13
    #[Override]
29
    public function shipToColonyDistance(ShipInterface $ship, ColonyInterface $colony): int
30
    {
31 13
        return $this->calculateAbsoluteDistance(
32 13
            $ship->getSystem(),
33 13
            $colony->getSystem(),
34 13
            $ship->getPosX(),
35 13
            $ship->getPosY(),
36 13
            $colony->getSx(),
37 13
            $colony->getSy()
38 13
        );
39
    }
40
41 34
    private function calculateAbsoluteDistance(
42
        ?StarSystemInterface $system1,
43
        ?StarSystemInterface $system2,
44
        int $x1,
45
        int $y1,
46
        int $x2,
47
        int $y2
48
    ): int {
49
50 34
        if ($this->isInSameSystem($system1, $system2)) {
51 10
            return $this->calculateDistance($x1, $x2, $y1, $y2, 1);
52
        }
53
54 24
        $jumpIntoSystemMarker = 0;
55 24
        if ($system1 !== null) {
56 15
            $jumpIntoSystemMarker += 1;
57
        }
58 24
        if ($system2 !== null) {
59 13
            $jumpIntoSystemMarker += 1;
60
        }
61
62 24
        $systemBorderDistance1 = $this->calculateSystemBorderDistance($system1, $x1, $y1);
63 24
        $systemBorderDistance2 = $this->calculateSystemBorderDistance($system2, $x2, $y2);
64
65 24
        $outerSystemDistance = $this->calculateDistance(
66 24
            $system1 === null ? $x1 : $system1->getCx(),
67 24
            $system2 === null ? $x2 : $system2->getCx(),
68 24
            $system1 === null ? $y1 : $system1->getCy(),
69 24
            $system2 === null ? $y2 : $system2->getCy(),
70 24
            1000
71 24
        );
72
73 24
        return $jumpIntoSystemMarker
74 24
            + $systemBorderDistance1
75 24
            + $systemBorderDistance2
76 24
            + $outerSystemDistance;
77
    }
78
79 34
    private function isInSameSystem(
80
        ?StarSystemInterface $system1,
81
        ?StarSystemInterface $system2
82
    ): bool {
83 34
        if ($system1 === null || $system2 === null) {
84 16
            return false;
85
        }
86
87 18
        return $system1 === $system2;
88
    }
89
90 24
    private function calculateSystemBorderDistance(
91
        ?StarSystemInterface $system,
92
        int $x,
93
        int $y,
94
    ): int {
95 24
        if ($system === null) {
96 16
            return 0;
97
        }
98
99 20
        $systemWidth = $system->getMaxX();
100 20
        $systemHeight = $system->getMaxY();
101
102 20
        $borderDistanceX = min($x - 1, $systemWidth - $x);
103 20
        $borderDistanceY = min($y - 1, $systemHeight - $y);
104
105 20
        return $borderDistanceX + $borderDistanceY;
106
    }
107
108 34
    private function calculateDistance(
109
        ?int $x1,
110
        ?int $x2,
111
        ?int $y1,
112
        ?int $y2,
113
        int $multiplier
114
    ): int {
115
        if (
116 34
            $x1 === null
117 34
            || $x2 === null
118 34
            || $y1 === null
119 34
            || $y2 === null
120
        ) {
121
            return PHP_INT_MAX;
122
        }
123
124 34
        return (abs($x1 - $x2) + abs($y1 - $y2)) * $multiplier;
0 ignored issues
show
Bug Best Practice introduced by
The expression return abs($x1 - $x2) + ...y1 - $y2) * $multiplier returns the type double which is incompatible with the type-hinted return integer.
Loading history...
125
    }
126
}
127