Passed
Push — master ( 41c1e2...7e39ad )
by Nico
17:44 queued 08:39
created

setTemplateVariables()   C

Complexity

Conditions 12
Paths 50

Size

Total Lines 80
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 49
nc 50
nop 3
dl 0
loc 80
ccs 0
cts 49
cp 0
crap 156
rs 6.9666
c 1
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\Spacecraft\View\ShowSystemSettings;
6
7
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
8
use Stu\Component\Spacecraft\System\SpacecraftSystemWrapperFactoryInterface;
9
use Stu\Module\Control\GameControllerInterface;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Control\GameControllerInterface 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...
10
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
11
use Stu\Orm\Repository\FlightSignatureRepositoryInterface;
12
13
class SubspaceSensorSystemSettings implements SystemSettingsProviderInterface
14
{
15 1
    public function __construct(
16
        private readonly SpacecraftSystemWrapperFactoryInterface $spacecraftSystemWrapperFactory,
17
        private FlightSignatureRepositoryInterface $flightSignatureRepository
18 1
    ) {}
19
20
    public function setTemplateVariables(
21
        SpacecraftSystemTypeEnum $systemType,
22
        SpacecraftWrapperInterface $wrapper,
23
        GameControllerInterface $game
24
    ): void {
25
        $game->setMacroInAjaxWindow('html/spacecraft/system/subspaceScanner.twig');
26
27
        $user = $game->getUser();
28
        $spacecraft = $wrapper->get();
29
30
        $isSubspaceScannerActive = $spacecraft->getSystemState(SpacecraftSystemTypeEnum::SUBSPACE_SCANNER);
31
32
33
        $isMatrixScannerHealthy = $spacecraft->isSystemHealthy(SpacecraftSystemTypeEnum::MATRIX_SCANNER);
34
        if ($isMatrixScannerHealthy && $isSubspaceScannerActive) {
35
36
            $location = $spacecraft->getLocation();
37
            $layerId = null;
38
39
            $subspaceSystemData = $wrapper->getSubspaceSystemData();
40
            if ($subspaceSystemData === null) {
41
                return;
42
            }
43
44
            $flightSigId = $subspaceSystemData->getFlightSigId();
45
            if ($flightSigId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $flightSigId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
46
                $flightSig = $this->flightSignatureRepository->find($flightSigId);
47
                $game->setTemplateVar('ANALYZED_SIGNATURE', $flightSig);
48
            }
49
50
            $analyzeTime = $subspaceSystemData->getAnalyzeTime();
51
            if ($analyzeTime) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $analyzeTime of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
52
                $currentTime = time();
53
                $maxTime = $analyzeTime + (10 * 60);
54
                $game->setTemplateVar('ANALYZE_TIME', $analyzeTime);
55
56
                if ($currentTime <= $maxTime) {
57
                    $game->setTemplateVar('ANALYZE_TIME', $analyzeTime);
58
                }
59
            }
60
61
62
            $system = $spacecraft->getSystem();
63
            if ($system) {
64
                $cx = $system->getCx();
65
                $cy = $system->getCy();
66
            } else {
67
                $cx = $location->getCx();
68
                $cy = $location->getCy();
69
            }
70
71
            if ($location->getLayer()) {
72
                $layerId = $location->getLayer()->getId();
73
            }
74
            $timeThreshold = time() - (12 * 3600);
75
76
            $sensorRange = $wrapper->getLssSystemData()?->getSensorRange() ?? 0;
77
78
            if ($cx && $cy && $layerId) {
79
                $signatures = $this->flightSignatureRepository->getSignaturesInSensorRange(
80
                    $user->getId(),
81
                    $cx,
82
                    $cy,
83
                    $layerId,
84
                    $sensorRange,
85
                    $timeThreshold
86
                );
87
                $game->setTemplateVar('SIGNATURES', $signatures);
88
            }
89
        } else {
90
            $game->setTemplateVar('SYSTEMWARNING', true);
91
        }
92
93
94
95
        $game->setTemplateVar('USER', $user);
96
        $game->setTemplateVar('SPACECRAFT', $spacecraft);
97
        $game->setTemplateVar(
98
            'systemWrapper',
99
            $this->spacecraftSystemWrapperFactory->create($wrapper->get(), $systemType)
100
        );
101
    }
102
}
103