Completed
Pull Request — master (#153)
by
unknown
03:05
created

Vote::updateVote()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
ccs 0
cts 15
cp 0
cc 4
eloc 10
nc 4
nop 1
crap 20
1
<?php
2
3
namespace eXpansion\Bundle\VoteManager\Structures;
4
5
use eXpansion\Framework\Core\Storage\Data\Player;
6
7
class Vote
8
{
9
    const VOTE_YES = "yes";
10
    const VOTE_NO = "no";
11
12
    const STATUS_FAILED = -1;
13
    const STATUS_CANCEL = 0;
14
    const STATUS_RUNNING = 1;
15
    const STATUS_PASSED = 2;
16
17
    protected $status = 1;
18
19
    protected $elapsedTime = 0;
20
    protected $totalTime = 30;
21
22
    /** @var int */
23
    protected $startTime = 0;
24
25
    protected $votes = [];
26
27
    /** @var string */
28
    private $type;
29
30
    protected $ratio = 0.57;
31
    /**
32
     * @var Player
33
     */
34
    private $player;
35
36
    public function __construct(Player $player, $type)
37
    {
38
        $this->startTime = time();
39
        $this->type = $type;
40
        $this->totalTime = 30;
41
        $this->player = $player;
42
    }
43
44
45
    public function castYes($login)
46
    {
47
        $this->votes[$login] = self::VOTE_YES;
48
    }
49
50
    public function castNo($login)
51
    {
52
        $this->votes[$login] = self::VOTE_NO;
53
    }
54
55
    /**
56
     *
57
     */
58 View Code Duplication
    public function getYes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $value = 0;
61
        foreach ($this->votes as $login => $vote) {
62
            if ($vote === self::VOTE_YES) {
63
                $value++;
64
            }
65
        }
66
67
        return $value;
68
    }
69
70
    function updateVote($time)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
71
    {
72
        $this->elapsedTime = $time - $this->startTime;
0 ignored issues
show
Documentation Bug introduced by
It seems like $time - $this->startTime can also be of type double. However, the property $elapsedTime is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
73
        $total = $this->getYes() + $this->getNo();
74
75
        if ($this->elapsedTime >= $this->totalTime) {
76
            if ($total > 0) {
77
                if (($this->getYes() / $total) > $this->ratio) {
78
                    $this->status = self::STATUS_PASSED;
79
80
                    return;
81
                }
82
            }
83
            $this->status = self::STATUS_FAILED;
84
85
            return;
86
        }
87
    }
88
89
90
    /**
91
     *
92
     */
93 View Code Duplication
    public function getNo()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        $value = 0;
96
        foreach ($this->votes as $login => $vote) {
97
            if ($vote === self::VOTE_NO) {
98
                $value++;
99
            }
100
        }
101
102
        return $value;
103
    }
104
105
106
    /**
107
     * @return int
108
     */
109
    public function getStartTime(): int
110
    {
111
        return $this->startTime;
112
    }
113
114
    /**
115
     * @return float
116
     */
117
    public function getTotalTime(): int
118
    {
119
        return $this->totalTime;
120
    }
121
122
    /**
123
     * @return int
124
     */
125
    public function getElapsedTime(): int
126
    {
127
        return $this->elapsedTime;
128
    }
129
130
    /**
131
     * @return float
132
     */
133
    public function getRatio(): float
134
    {
135
        return $this->ratio;
136
    }
137
138
    /**
139
     * @param float $ratio
140
     */
141
    public function setRatio(float $ratio)
142
    {
143
        $this->ratio = $ratio;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getType(): string
150
    {
151
        return $this->type;
152
    }
153
154
    /**
155
     * @return Player
156
     */
157
    public function getPlayer(): Player
158
    {
159
        return $this->player;
160
    }
161
162
    /**
163
     * @return int
164
     */
165
    public function getStatus(): int
166
    {
167
        return $this->status;
168
    }
169
170
    /**
171
     * @param int $status
172
     */
173
    public function setStatus(int $status)
174
    {
175
        $this->status = $status;
176
    }
177
178
}
179