Completed
Push — master ( ef82f9...6cec72 )
by Antonio Carlos
01:48
created

ArrayGenerator::generateArray()   A

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)];
0 ignored issues
show
Bug introduced by
It seems like generateRandomInt() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39
        }
40
41 1
        if ($this->count == 1) {
42 1
            return $result[0];
43
        }
44
45 1
        return $result;
46
    }
47
}
48