HBuckets::bucketing()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Histogram buckets
4
 * User: moyo
5
 * Date: 2018/5/18
6
 * Time: 11:49 AM
7
 */
8
9
namespace Carno\Monitor\Chips\Metrical;
10
11
trait HBuckets
12
{
13
    /**
14
     * @var array
15
     */
16
    private $bounds = [];
17
18
    /**
19
     * @param float ...$bounds
20
     * @return static
21
     */
22
    public function fixed(float ...$bounds) : self
23
    {
24
        return $this->bucketing($bounds);
25
    }
26
27
    /**
28
     * @param float $start
29
     * @param float $width
30
     * @param int $count
31
     * @return static
32
     */
33
    public function linear(float $start, float $width, int $count) : self
34
    {
35
        $bounds = [];
36
37
        for ($i = 0; $i < $count; $i ++) {
38
            $bounds[] = $start;
39
            $start += $width;
40
        }
41
42
        return $this->bucketing($bounds);
43
    }
44
45
    /**
46
     * @param float $start
47
     * @param float $factor
48
     * @param int $count
49
     * @return static
50
     */
51
    public function exponential(float $start, float $factor, int $count) : self
52
    {
53
        $bounds = [];
54
55
        for ($i = 0; $i < $count; $i ++) {
56
            $bounds[] = $start;
57
            $start *= $factor;
58
        }
59
60
        return $this->bucketing($bounds);
61
    }
62
63
    /**
64
     * @param array $bounds
65
     * @return static
66
     */
67
    private function bucketing(array $bounds) : self
68
    {
69
        $this->bounds = $bounds;
70
71
        foreach ($bounds as $idx => $bound) {
72
            $this->buckets[$idx] = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property buckets does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
73
        }
74
75
        return $this;
76
    }
77
}
78