Passed
Push — master ( 1d5530...fba2a1 )
by Nico
56:37 queued 25:57
created

CheckDestination::validate()   C

Complexity

Conditions 12
Paths 69

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 12.0988

Importance

Changes 0
Metric Value
cc 12
eloc 33
nc 69
nop 3
dl 0
loc 57
ccs 31
cts 34
cp 0.9118
crap 12.0988
rs 6.9666
c 0
b 0
f 0

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 Stu\Module\Ship\Lib\Movement\Route;
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\Exception\SanityCheckException;
10
use Stu\Orm\Entity\MapInterface;
11
use Stu\Orm\Entity\ShipInterface;
12
use Stu\Orm\Entity\StarSystemMapInterface;
13
use Stu\Orm\Repository\MapRepositoryInterface;
14
use Stu\Orm\Repository\StarSystemMapRepositoryInterface;
15
16
final class CheckDestination implements CheckDestinationInterface
17
{
18 14
    public function __construct(private MapRepositoryInterface $mapRepository, private StarSystemMapRepositoryInterface $starSystemMapRepository)
19
    {
20 14
    }
21
22 14
    #[Override]
23
    public function validate(
24
        ShipInterface $ship,
25
        int $destinationX,
26
        int $destinationY
27
    ): MapInterface|StarSystemMapInterface {
28 14
        $start = $ship->getCurrentMapField();
29
30 14
        if ($start->getX() !== $destinationX && $start->getY() !== $destinationY) {
31 2
            throw new SanityCheckException(
32 2
                sprintf(
33 2
                    'userId %d tried to navigate from %s to invalid position %d|%d',
34 2
                    $ship->getUser()->getId(),
35 2
                    $start->getSectorString(),
36 2
                    $destinationX,
37 2
                    $destinationY
38 2
                )
39 2
            );
40
        }
41 12
        if ($destinationX < 1) {
42
            $destinationX = 1;
43
        }
44 12
        if ($destinationY < 1) {
45
            $destinationY = 1;
46
        }
47 12
        if ($start instanceof StarSystemMapInterface) {
48
49 6
            $system = $start->getSystem();
50 6
            if ($destinationX > $system->getMaxX()) {
51 1
                $destinationX = $system->getMaxX();
52
            }
53 6
            if ($destinationY > $system->getMaxY()) {
54 1
                $destinationY = $system->getMaxY();
55
            }
56
57 6
            $result = $this->starSystemMapRepository->getByCoordinates($system->getId(), $destinationX, $destinationY);
58
        } else {
59 6
            $layer = $start->getLayer();
60 6
            if ($layer === null) {
61
                throw new RuntimeException('this should not happen');
62
            }
63
64 6
            if ($destinationX > $layer->getWidth()) {
65 1
                $destinationX = $layer->getWidth();
66
            }
67 6
            if ($destinationY > $layer->getHeight()) {
68 1
                $destinationY = $layer->getHeight();
69
            }
70
71 6
            $result = $this->mapRepository->getByCoordinates($layer, $destinationX, $destinationY);
72
        }
73
74 12
        if ($result === null) {
75 2
            throw new RuntimeException(sprintf('destination %d|%d does not exist', $destinationX, $destinationY));
76
        }
77
78 10
        return $result;
79
    }
80
}
81