Passed
Push — master ( 5ad2cb...35956d )
by Antonio Carlos
03:45
created

Generators   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 131
ccs 37
cts 37
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 14 4
A generateInteger() 0 4 1
A generateRandomInt() 0 4 1
A generateString() 0 10 2
A getAlphaGenerator() 0 6 1
A getNumericGenerator() 0 6 1
A generateAlpha() 0 4 1
A generateArray() 0 16 3
A generateNumeric() 0 8 3
1
<?php
2
3
namespace PragmaRX\Random;
4
5
trait Generators
6
{
7
    protected $fakerString;
8
9
    /**
10
     * Generate a random string.
11
     *
12
     * @return string
13
     */
14 12
    protected function generate()
15
    {
16 12
        if (! is_null($this->fakerString)) {
17 1
            return $this->fakerString;
18
        }
19
20 11
        if ($this->array) {
0 ignored issues
show
Bug introduced by
The property array does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21 1
            return $this->generateArray();
22
        }
23
24 10
        return $this->numeric
0 ignored issues
show
Bug introduced by
The property numeric does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25 3
            ? $this->generateNumeric()
26 10
            : $this->generateAlpha();
27
    }
28
29
    /**
30
     * Generate a ramdom integer.
31
     *
32
     * @return int
33
     */
34 1
    protected function generateInteger()
35
    {
36 1
        return $this->generateRandomInt($this->getStart(), $this->getEnd());
0 ignored issues
show
Bug introduced by
It seems like getStart() 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...
Bug introduced by
It seems like getEnd() 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...
37
    }
38
39
    /**
40
     * Generate a random integer.
41
     *
42
     * @return int
43
     */
44 2
    protected function generateRandomInt($start, $end)
45
    {
46 2
        return random_int($start, $end);
47
    }
48
49
    /**
50
     * Generate a random string.
51
     *
52
     * @param \Closure $generator
53
     * @return mixed
54
     */
55 10
    protected function generateString($generator)
56
    {
57 10
        $string = '';
58
59 10
        while (strlen($string) < $size = $this->getSize()) {
0 ignored issues
show
Bug introduced by
It seems like getSize() 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...
60 10
            $string .= $this->extractPattern($generator($size));
0 ignored issues
show
Bug introduced by
It seems like extractPattern() 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...
61
        }
62
63 10
        return $this->trimToExpectedSize($string, $size);
0 ignored issues
show
Bug introduced by
It seems like trimToExpectedSize() 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...
64
    }
65
66
    /**
67
     * Get the alpha generator.
68
     *
69
     * @return mixed
70
     */
71
    protected function getAlphaGenerator()
72
    {
73 9
        return function ($size) {
74 9
            return random_bytes($size);
75 9
        };
76
    }
77
78
    /**
79
     * Get the alpha generator.
80
     *
81
     * @return mixed
82
     */
83
    protected function getNumericGenerator()
84
    {
85 3
        return function () {
86 3
            return random_int(0, PHP_INT_MAX);
87 3
        };
88
    }
89
90
    /**
91
     * Generate a random string.
92
     *
93
     * @return int|string
94
     */
95 9
    protected function generateAlpha()
96
    {
97 9
        return $this->generateString($this->getAlphaGenerator());
98
    }
99
100
    /**
101
     * Generate random array elements.
102
     *
103
     * @return array
104
     */
105 1
    protected function generateArray()
106
    {
107 1
        $result = [];
108
109 1
        $last = count($this->items)-1;
0 ignored issues
show
Bug introduced by
The property items does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
110
111 1
        foreach (range(1, $this->count) as $counter) {
0 ignored issues
show
Bug introduced by
The property count does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
112 1
            $result[] = $this->items[$this->generateRandomInt(0, $last)];
113
        }
114
115 1
        if ($this->count == 1) {
116 1
            return $result[0];
117
        }
118
119 1
        return $result;
120
    }
121
122
    /**
123
     * Generate a numeric random value.
124
     *
125
     * @return int|string
126
     */
127 3
    protected function generateNumeric()
128
    {
129 3
        if (is_null($this->size) && $this->pattern == static::DEFAULT_PATTERN) {
0 ignored issues
show
Bug introduced by
The property size does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property pattern does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
130 1
            return $this->generateInteger();
131
        }
132
133 3
        return $this->generateString($this->getNumericGenerator());
134
    }
135
}
136