Passed
Push — dev ( 25004f...be11ce )
by Janko
16:12
created

ColonyPopulationCalculator::getGrowth()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.8142

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 10
nop 0
dl 0
loc 26
ccs 10
cts 15
cp 0.6667
crap 8.8142
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Colony;
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 Stu\Lib\Colony\PlanetFieldHostInterface;
9
use Stu\Lib\ColonyProduction\ColonyProduction;
10
use Stu\Module\Commodity\CommodityTypeEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Commodity\CommodityTypeEnum 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...
11
use Stu\Orm\Entity\ColonyInterface;
12
13
final class ColonyPopulationCalculator implements ColonyPopulationCalculatorInterface
14
{
15
    private ?int $positive_effect_secondary = null;
16
17
    private ?int $positive_effect_primary = null;
18
19
    /**
20
     * @param array<int, ColonyProduction> $production
21
     */
22 10
    public function __construct(private PlanetFieldHostInterface $host, private array $production) {}
23
24 1
    #[Override]
25
    public function getFreeAssignmentCount(): int
26
    {
27 1
        if (!$this->host instanceof ColonyInterface) {
28
            return 0;
29
        }
30
31 1
        return max(0, $this->getCrewLimit() - $this->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

31
        return max(0, $this->getCrewLimit() - $this->host->/** @scrutinizer ignore-call */ getCrewAssignmentAmount());
Loading history...
32
    }
33
34 6
    #[Override]
35
    public function getCrewLimit(): int
36
    {
37 6
        return (int) floor(
38 6
            10 +
39 6
                min(
40 6
                    max(
41 6
                        ($this->getPositiveEffectPrimary() - (4 * max(
42 6
                            0,
43 6
                            $this->getNegativeEffect() - $this->getPositiveEffectSecondary()
44 6
                        ))),
45 6
                        0
46 6
                    ),
47 6
                    $this->host->getWorkers()
48 6
                ) / 5 * $this->getLifeStandardPercentage() / 100
49 6
        );
50
    }
51
52 10
    #[Override]
53
    public function getLifeStandardPercentage(): int
54
    {
55 10
        $colonyProduction = $this->production[CommodityTypeEnum::COMMODITY_EFFECT_LIFE_STANDARD] ?? null;
56 10
        $production = $colonyProduction !== null ? $colonyProduction->getProduction() : 0;
57
58 10
        if ($production == 0) {
59 10
            return 0;
60
        }
61
62
        if ($production > $this->host->getPopulation()) {
63
            return 100;
64
        }
65
66
        return (int)floor($production * 100 / $this->host->getPopulation());
67
    }
68
69 6
    #[Override]
70
    public function getNegativeEffect(): int
71
    {
72 6
        return (int) ceil($this->host->getPopulation() / 70);
73
    }
74
75 6
    #[Override]
76
    public function getPositiveEffectPrimary(): int
77
    {
78 6
        if ($this->positive_effect_primary === null) {
79 6
            $this->positive_effect_primary = 0;
80
81 6
            $commodity = $this->host->getUser()->getFaction()->getPrimaryEffectCommodity();
82
83 6
            if ($commodity !== null && array_key_exists($commodity->getId(), $this->production)) {
84
                $this->positive_effect_primary += $this->production[$commodity->getId()]->getProduction();
85
            }
86
        }
87
88 6
        return $this->positive_effect_primary;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->positive_effect_primary could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
89
    }
90
91 6
    #[Override]
92
    public function getPositiveEffectSecondary(): int
93
    {
94 6
        if ($this->positive_effect_secondary === null) {
95 6
            $this->positive_effect_secondary = 0;
96
97 6
            $commodity = $this->host->getUser()->getFaction()->getSecondaryEffectCommodity();
98
99 6
            if ($commodity !== null && array_key_exists($commodity->getId(), $this->production)) {
100
                $this->positive_effect_secondary += $this->production[$commodity->getId()]->getProduction();
101
            }
102
        }
103 6
        return $this->positive_effect_secondary;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->positive_effect_secondary could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
104
    }
105
106 5
    #[Override]
107
    public function getGrowth(): int
108
    {
109 5
        $host = $this->host;
110 5
        if (!$host instanceof ColonyInterface) {
111
            return 0;
112
        }
113
114 5
        $changeable = $host->getChangeable();
115
116 5
        if ($changeable->getImmigrationState() === false) {
117
            return 0;
118
        }
119
120
        // TBD: depends on social things. return dummy for now
121 5
        $im = ceil((($changeable->getMaxBev() - $host->getPopulation()) / 3) / 100 * $host->getColonyClass()->getBevGrowthRate() *  $this->getLifeStandardPercentage() / 50);
122 5
        if ($host->getPopulation() + $im > $changeable->getMaxBev()) {
123
            $im = $changeable->getMaxBev() - $host->getPopulation();
124
        }
125 5
        if ($changeable->getPopulationLimit() > 0 && $host->getPopulation() + $im > $changeable->getPopulationLimit()) {
126
            $im = $changeable->getPopulationLimit() - $host->getPopulation();
127
        }
128 5
        if ($im < 0) {
129
            return 0;
130
        }
131 5
        return (int) $im;
132
    }
133
}
134