Passed
Push — dev ( da5016...69a1ba )
by Janko
24:07 queued 09:11
created

ColonyRotationTrait::getColonyTimeHour()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Stu\Component\Colony\Trait;
4
5
use Stu\Component\Game\TimeConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\TimeConstants 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
7
trait ColonyRotationTrait
8
{
9 6
    public function getTwilightZone(int $timestamp): int
10
    {
11 6
        if (array_key_exists($timestamp, $this->twilightZones)) {
12 6
            return $this->twilightZones[$timestamp];
13
        }
14
15 1
        $twilightZone = 0;
16
17 1
        $width = $this->getSurfaceWidth();
0 ignored issues
show
Bug introduced by
It seems like getSurfaceWidth() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

17
        /** @scrutinizer ignore-call */ 
18
        $width = $this->getSurfaceWidth();
Loading history...
18 1
        $rotationTime = $this->getRotationTime();
19 1
        $colonyTimeSeconds = $this->getColonyTimeSeconds($timestamp);
20
21 1
        if ($this->getDayTimePrefix($timestamp) == 1) {
22
            $scaled = floor((((100 / ($rotationTime * 0.125)) * ($colonyTimeSeconds - $rotationTime * 0.25)) / 100) * $width);
23
            if ($scaled == 0) {
24
                $twilightZone = - (($width) - 1);
25
            } elseif ((int) - (($width) - ceil($scaled)) == 0) {
26
                $twilightZone = -1;
27
            } else {
28
                $twilightZone = (int) - (($width) - $scaled);
29
            }
30
        }
31 1
        if ($this->getDayTimePrefix($timestamp) == 2) {
32 1
            $twilightZone = $width;
33
        }
34 1
        if ($this->getDayTimePrefix($timestamp) == 3) {
35
            $scaled = floor((((100 / ($rotationTime * 0.125)) * ($colonyTimeSeconds - $rotationTime * 0.75)) / 100) * $width);
36
            $twilightZone = (int) ($width - $scaled);
37
        }
38 1
        if ($this->getDayTimePrefix($timestamp) == 4) {
39
            $twilightZone = 0;
40
        }
41
42 1
        $this->twilightZones[$timestamp] = $twilightZone;
0 ignored issues
show
Bug Best Practice introduced by
The property twilightZones does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
44 1
        return $twilightZone;
45
    }
46
47 4
    public function getRotationTime(): int
48
    {
49 4
        return (int) (TimeConstants::ONE_DAY_IN_SECONDS * $this->getRotationFactor() / 100);
0 ignored issues
show
Bug introduced by
The method getRotationFactor() does not exist on Stu\Component\Colony\Trait\ColonyRotationTrait. Did you maybe mean getRotationTime()? ( Ignorable by Annotation )

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

49
        return (int) (TimeConstants::ONE_DAY_IN_SECONDS * $this->/** @scrutinizer ignore-call */ getRotationFactor() / 100);

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...
50
    }
51
52 4
    public function getColonyTimeHour(int $timestamp): ?string
53
    {
54 4
        $rotationTime = $this->getRotationTime();
55
56 4
        return sprintf("%02d", (int) floor(($rotationTime / 3600) * ($this->getColonyTimeSeconds($timestamp) / $rotationTime)));
57
    }
58
59 4
    public function getColonyTimeMinute(int $timestamp): ?string
60
    {
61 4
        $rotationTime = $this->getRotationTime();
62
63 4
        return sprintf("%02d", (int) floor(60 * (($rotationTime / 3600) * ($this->getColonyTimeSeconds($timestamp) / $rotationTime) - ((int) $this->getColonyTimeHour($timestamp)))));
64
    }
65
66 4
    private function getColonyTimeSeconds(int $timestamp): int
67
    {
68 4
        return $timestamp % $this->getRotationTime();
69
    }
70
71 4
    public function getDayTimePrefix(int $timestamp): ?int
72
    {
73 4
        $daytimeprefix = null;
74 4
        $daypercent = (int) (($this->getColonyTimeSeconds($timestamp) / $this->getRotationTime()) * 100);
75 4
        if ($daypercent > 25 && $daypercent <= 37.5) {
76
            $daytimeprefix = 1; //Sonnenaufgang
77
        }
78 4
        if ($daypercent > 37.5 && $daypercent <= 75) {
79 4
            $daytimeprefix = 2; //Tag
80
        }
81 4
        if ($daypercent > 75 && $daypercent <= 87.5) {
82
            $daytimeprefix = 3; //Sonnenuntergang
83
        }
84 4
        if ($daypercent > 87.5 || $daypercent <= 25) {
85
            $daytimeprefix = 4; //Nacht
86
        }
87 4
        return $daytimeprefix;
88
    }
89
90 4
    public function getDayTimeName(int $timestamp): ?string
91
    {
92 4
        $daytimename = null;
93 4
        if ($this->getDayTimePrefix($timestamp) == 1) {
94
            $daytimename = 'Morgen';
95
        }
96
97 4
        if ($this->getDayTimePrefix($timestamp) == 2) {
98 4
            $daytimename = 'Tag';
99
        }
100
101 4
        if ($this->getDayTimePrefix($timestamp) == 3) {
102
            $daytimename = 'Abend';
103
        }
104
105 4
        if ($this->getDayTimePrefix($timestamp) == 4) {
106
            $daytimename = 'Nacht';
107
        }
108 4
        return $daytimename;
109
    }
110
}
111