Passed
Push — master ( 805d33...d48cfb )
by Nico
51:39 queued 26:06
created

AcademyProvider::setTemplateVariables()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 54
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 33
c 1
b 0
f 0
nc 32
nop 2
dl 0
loc 54
ccs 0
cts 35
cp 0
crap 42
rs 8.7697

How to fix   Long Method   

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
namespace Stu\Module\Colony\Lib\Gui\Component;
4
5
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...
6
use Stu\Component\Crew\CrewCountRetrieverInterface;
7
use Stu\Lib\Colony\PlanetFieldHostInterface;
8
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Orm\Entity\ColonyInterface;
11
12
final class AcademyProvider implements GuiComponentProviderInterface
13
{
14
    public function __construct(private ColonyLibFactoryInterface $colonyLibFactory, private CrewCountRetrieverInterface $crewCountRetriever) {}
15
16
    /** @param ColonyInterface&PlanetFieldHostInterface $host */
17
    #[Override]
18
    public function setTemplateVariables(
19
        PlanetFieldHostInterface $host,
20
        GameControllerInterface $game
21
    ): void {
22
        $user = $game->getUser();
23
24
        $crewInTrainingCount = $this->crewCountRetriever->getInTrainingCount($user);
25
        $crewRemainingCount = $this->crewCountRetriever->getRemainingCount($user);
26
        $crewTrainableCount = $this->crewCountRetriever->getTrainableCount($user);
27
28
        $trainableCrew = $crewTrainableCount - $crewInTrainingCount;
29
        if ($trainableCrew > $crewRemainingCount) {
30
            $trainableCrew = $crewRemainingCount;
31
        }
32
33
        if ($trainableCrew > $host->getWorkless()) {
0 ignored issues
show
Bug introduced by
The method getWorkless() does not exist on Stu\Lib\Colony\PlanetFieldHostInterface. Did you maybe mean getWorkers()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        if ($trainableCrew > $host->/** @scrutinizer ignore-call */ getWorkless()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
            $trainableCrew = $host->getWorkless();
35
        }
36
37
        $freeAssignmentCount = $this->colonyLibFactory->createColonyPopulationCalculator(
38
            $host
39
        )->getFreeAssignmentCount();
40
41
        $localcrewlimit = $this->colonyLibFactory->createColonyPopulationCalculator(
42
            $host
43
        )->getCrewLimit();
44
45
        $crewinlocalpool = $host->getCrewAssignmentAmount();
0 ignored issues
show
Bug introduced by
The method getCrewAssignmentAmount() does not exist on Stu\Lib\Colony\PlanetFieldHostInterface. It seems like you code against a sub-type of Stu\Lib\Colony\PlanetFieldHostInterface such as Stu\Orm\Entity\ColonyInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        /** @scrutinizer ignore-call */ 
46
        $crewinlocalpool = $host->getCrewAssignmentAmount();
Loading history...
46
47
        if ($localcrewlimit - $crewinlocalpool < $trainableCrew) {
48
            $trainableCrew = $localcrewlimit - $crewinlocalpool;
49
        }
50
51
        if ($trainableCrew > $freeAssignmentCount) {
52
            $trainableCrew = $freeAssignmentCount;
53
        }
54
55
        if ($trainableCrew < 0) {
56
            $trainableCrew = 0;
57
        }
58
59
        $game->setTemplateVar('TRAINABLE_CREW_COUNT_PER_TICK', $trainableCrew);
60
        $game->setTemplateVar(
61
            'CREW_COUNT_TRAINING',
62
            $crewInTrainingCount
63
        );
64
        $game->setTemplateVar(
65
            'CREW_COUNT_REMAINING',
66
            $crewRemainingCount
67
        );
68
        $game->setTemplateVar(
69
            'CREW_COUNT_TRAINABLE',
70
            $crewTrainableCount
71
        );
72
    }
73
}
74