Bool::setDefaults()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Samshal\Rando\Packages\Basics;
4
5
use Samshal\Rando\Packages\PackageableInterface;
6
use Samshal\Rando\Packages\Packages;
7
8
class Bool extends Packages implements PackageableInterface
9
{
10
11
    protected $likelihood;
12
13
    public function setDefaults($defaultsArray = [])
14
    {
15
        $defaultsArray['likelihood'] = 50;
16
17
        return $defaultsArray;
18
    }
19
20
    protected function setLikelihood($likelihood)
21
    {
22
        $this->likelihood = $likelihood;
23
    }
24
25
    private function generateBooleans()
26
    {
27
        $successRatio = ($this->likelihood / 100) * 10;
28
        $failureRatio = 10 - $successRatio;
29
30
        $booleans = array_fill(0, $successRatio, (int)true);
31
        if ($successRatio !== 10) {
32
            $booleans = array_merge($booleans, array_fill($successRatio+1, $failureRatio, (int)false));
33
        }
34
35
        return $booleans;
36
    }
37
38
    private function doRandomization()
39
    {
40
        $booleans = self::generateBooleans();
41
        
42
        return (boolean)$booleans[array_rand($booleans)];
43
    }
44
45
    public function stringify()
46
    {
47
        return self::doRandomization();
48
    }
49
}
50