Passed
Push — master ( d957b1...392da7 )
by Nico
26:32 queued 16:07
created

UpdateFlightDirection   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 63.16%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 40
c 1
b 1
f 0
dl 0
loc 70
ccs 24
cts 38
cp 0.6316
rs 10
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
B updateWhenTraversing() 0 43 9
A updateWhenSystemExit() 0 24 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Movement\Component;
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 RuntimeException;
9
use Stu\Component\Map\DirectionEnum;
10
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
11
use Stu\Orm\Entity\Location;
12
use Stu\Orm\Entity\StarSystemMap;
13
14
final class UpdateFlightDirection implements UpdateFlightDirectionInterface
15
{
16 6
    #[Override]
17
    public function updateWhenTraversing(
18
        Location $oldWaypoint,
19
        Location $waypoint,
20
        SpacecraftWrapperInterface $wrapper
21
    ): DirectionEnum {
22
23 6
        if (!$wrapper->get()->hasComputer()) {
24 1
            return DirectionEnum::NON;
25
        }
26
27 5
        $startX = $oldWaypoint->getX();
28 5
        $startY = $oldWaypoint->getY();
29
30 5
        $destinationX = $waypoint->getX();
31 5
        $destinationY = $waypoint->getY();
32
33 5
        $flightDirection = null;
34
35 5
        if ($destinationX === $startX) {
36 3
            $oldy = $startY;
37 3
            if ($destinationY > $oldy) {
38 1
                $flightDirection = DirectionEnum::BOTTOM;
39 2
            } elseif ($destinationY < $oldy) {
40 1
                $flightDirection = DirectionEnum::TOP;
41
            }
42
        }
43 5
        if ($destinationY === $startY) {
44 3
            $oldx = $startX;
45 3
            if ($destinationX > $oldx) {
46 1
                $flightDirection = DirectionEnum::RIGHT;
47 2
            } elseif ($destinationX < $oldx) {
48 1
                $flightDirection = DirectionEnum::LEFT;
49
            }
50
        }
51
52 5
        if ($flightDirection === null) {
53 1
            throw new RuntimeException('this should not happen');
54
        }
55
56 4
        $wrapper->getComputerSystemDataMandatory()->setFlightDirection($flightDirection)->update();
57
58 4
        return $flightDirection;
59
    }
60
    #[Override]
61
    public function updateWhenSystemExit(SpacecraftWrapperInterface $wrapper, StarSystemMap $starsystemMap): void
62
    {
63
        if ($wrapper->get()->getRump()->isEscapePods()) {
64
            return;
65
        }
66
67
        $system = $starsystemMap->getSystem();
68
69
        $shipX = $starsystemMap->getSx();
70
        $shipY = $starsystemMap->getSy();
71
72
        $rad12or34 = atan($shipY / $shipX);
73
        $rad14or23 = atan(($system->getMaxX() - $shipX) / $shipY);
74
75
        if ($rad12or34 < M_PI_4) {
76
            $flightDirection = $rad14or23 < M_PI_4 ? DirectionEnum::LEFT : DirectionEnum::BOTTOM;
77
        } elseif ($rad14or23 < M_PI_4) {
78
            $flightDirection = DirectionEnum::TOP;
79
        } else {
80
            $flightDirection = DirectionEnum::RIGHT;
81
        }
82
83
        $wrapper->getComputerSystemDataMandatory()->setFlightDirection($flightDirection)->update();
84
    }
85
}
86