Completed
Pull Request — master (#152)
by
unknown
02:44
created

Vote::setStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
        if ($this->elapsedTime > $this->totalTime) {
74
            $this->status = self::STATUS_FAILED;
75
        }
76
77
        $total = $this->getYes() + $this->getNo();
78
        if ($total > 0) {
79
            if (($this->getYes() / $total) > $this->ratio) {
80
                $this->status = self::STATUS_PASSED;
81
            }
82
83
            if (1 - ($this->getYes() / $total) > 0.9) {
84
                $this->status = self::STATUS_FAILED;
85
            }
86
87
        }
88
    }
89
90
91
    /**
92
     *
93
     */
94 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...
95
    {
96
        $value = 0;
97
        foreach ($this->votes as $login => $vote) {
98
            if ($vote === self::VOTE_NO) {
99
                $value++;
100
            }
101
        }
102
103
        return $value;
104
    }
105
106
107
    /**
108
     * @return int
109
     */
110
    public function getStartTime(): int
111
    {
112
        return $this->startTime;
113
    }
114
115
    /**
116
     * @return float
117
     */
118
    public function getTotalTime(): int
119
    {
120
        return $this->totalTime;
121
    }
122
123
    /**
124
     * @return int
125
     */
126
    public function getElapsedTime(): int
127
    {
128
        return $this->elapsedTime;
129
    }
130
131
    /**
132
     * @return float
133
     */
134
    public function getRatio(): float
135
    {
136
        return $this->ratio;
137
    }
138
139
    /**
140
     * @param float $ratio
141
     */
142
    public function setRatio(float $ratio)
143
    {
144
        $this->ratio = $ratio;
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getType(): string
151
    {
152
        return $this->type;
153
    }
154
155
    /**
156
     * @return Player
157
     */
158
    public function getPlayer(): Player
159
    {
160
        return $this->player;
161
    }
162
163
    /**
164
     * @return int
165
     */
166
    public function getStatus(): int
167
    {
168
        return $this->status;
169
    }
170
171
    /**
172
     * @param int $status
173
     */
174
    public function setStatus(int $status)
175
    {
176
        $this->status = $status;
177
    }
178
179
}
180