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

ArrayGenerator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 43
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 6 1
A generateArray() 0 16 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