ArrayGenerator::generateArray()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 0
crap 3
1
<?php
2
3
namespace PragmaRX\Random\Generators;
4
5
trait ArrayGenerator
6
{
7
    protected $array = false;
8
9
    protected $items = [];
10
11
    protected $count = 1;
12
13
    /**
14
     * Set the array items count.
15
     *
16
     * @param $count
17
     * @return $this
18
     */
19 1
    public function count($count)
20
    {
21 1
        $this->count = $count;
22
23 1
        return $this;
24
    }
25
26
    /**
27
     * Generate random array elements.
28
     *
29
     * @return array
30
     */
31 1
    protected function generateArray()
32
    {
33 1
        $result = [];
34
35 1
        $last = count($this->items)-1;
36
37 1
        for ($counter = 1; $counter <= $this->count; $counter++) {
38 1
            $result[] = $this->items[$this->generateRandomInt(0, $last)];
39
        }
40
41 1
        if ($this->count == 1) {
42 1
            return $result[0];
43
        }
44
45 1
        return $result;
46
    }
47
48
    /**
49
     * Generate a random integer.
50
     *
51
     * @param int $start
52
     * @param int $end
53
     * @return int
54
     */
55
    abstract protected function generateRandomInt($start, $end);
56
}
57