Completed
Push — master ( 6577e3...2afeb5 )
by Antonio Carlos
04:50
created

Generators::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace PragmaRX\Random;
4
5
trait Generators
6
{
7
    /**
8
     * Generate a random string.
9
     *
10
     * @return string
11
     */
12 8
    protected function generate()
13
    {
14 8
        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...
15 3
            ? $this->generateNumeric()
16 8
            : $this->generateAlpha();
17
    }
18
19
    /**
20
     * Generate a ramdom integer.
21
     *
22
     * @return int
23
     */
24 1
    protected function generateInteger()
25
    {
26 1
        return random_int($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...
27
    }
28
29
    /**
30
     * Generate a random string.
31
     *
32
     * @param \Closure $generator
33
     * @return mixed
34
     */
35 8
    protected function generateString($generator)
36
    {
37 8
        $string = '';
38
39 8
        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...
40 8
            $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...
41
        }
42
43 8
        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...
44
    }
45
46
    /**
47
     * Get the alpha generator.
48
     *
49
     * @return mixed
50
     */
51
    protected function getAlphaGenerator()
52
    {
53 7
        return function ($size) {
54 7
            return random_bytes($size);
55 7
        };
56
    }
57
58
    /**
59
     * Get the alpha generator.
60
     *
61
     * @return mixed
62
     */
63
    protected function getNumericGenerator()
64
    {
65 3
        return function () {
66 3
            return random_int(0, PHP_INT_MAX);
67 3
        };
68
    }
69
70
    /**
71
     * Generate a random string.
72
     *
73
     * @return int|string
74
     */
75 7
    protected function generateAlpha()
76
    {
77 7
        return $this->generateString($this->getAlphaGenerator());
78
    }
79
80
    /**
81
     * Generate a numeric random value.
82
     *
83
     * @return int|string
84
     */
85 3
    protected function generateNumeric()
86
    {
87 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...
88 1
            return $this->generateInteger();
89
        }
90
91 3
        return $this->generateString($this->getNumericGenerator());
92
    }
93
}
94