Passed
Push — master ( 6d8b29...d7e053 )
by Janko
27:40
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
    #[Override]
21
    public function validate(
22
        ShipInterface $ship,
23
        int $destinationX,
24
        int $destinationY
25
    ): MapInterface|StarSystemMapInterface {
26 14
        $start = $ship->getLocation();
27
28 14
        if ($start->getX() !== $destinationX && $start->getY() !== $destinationY) {
29 2
            throw new SanityCheckException(
30 2
                sprintf(
31 2
                    'userId %d tried to navigate from %s to invalid position %d|%d',
32 2
                    $ship->getUser()->getId(),
33 2
                    $start->getSectorString(),
34 2
                    $destinationX,
35 2
                    $destinationY
36 2
                )
37 2
            );
38
        }
39 12
        if ($destinationX < 1) {
40
            $destinationX = 1;
41
        }
42 12
        if ($destinationY < 1) {
43
            $destinationY = 1;
44
        }
45 12
        if ($start instanceof StarSystemMapInterface) {
46
47 6
            $system = $start->getSystem();
48 6
            if ($destinationX > $system->getMaxX()) {
49 1
                $destinationX = $system->getMaxX();
50
            }
51 6
            if ($destinationY > $system->getMaxY()) {
52 1
                $destinationY = $system->getMaxY();
53
            }
54
55 6
            $result = $this->starSystemMapRepository->getByCoordinates($system->getId(), $destinationX, $destinationY);
56
        } else {
57 6
            $layer = $start->getLayer();
58 6
            if ($layer === null) {
59
                throw new RuntimeException('this should not happen');
60
            }
61
62 6
            if ($destinationX > $layer->getWidth()) {
63 1
                $destinationX = $layer->getWidth();
64
            }
65 6
            if ($destinationY > $layer->getHeight()) {
66 1
                $destinationY = $layer->getHeight();
67
            }
68
69 6
            $result = $this->mapRepository->getByCoordinates($layer, $destinationX, $destinationY);
70
        }
71
72 12
        if ($result === null) {
73 2
            throw new RuntimeException(sprintf('destination %d|%d does not exist', $destinationX, $destinationY));
74
        }
75
76 10
        return $result;
77
    }
78
}
79