PseudoRandomStringGeneratorTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A binToHex() 0 3 1
A validateLength() 0 8 3
1
<?php
2
3
namespace Maztech\PseudoRandomString;
4
5
trait PseudoRandomStringGeneratorTrait
6
{
7
    /**
8
     * Validates the length argument of a random string.
9
     *
10
     * @param int $length The length to validate.
11
     *
12
     * @throws \InvalidArgumentException
13
     */
14
    public function validateLength($length)
15
    {
16
        if (!is_int($length)) {
0 ignored issues
show
introduced by
The condition is_int($length) is always true.
Loading history...
17
            throw new \InvalidArgumentException('getPseudoRandomString() expects an integer for the string length');
18
        }
19
20
        if ($length < 1) {
21
            throw new \InvalidArgumentException('getPseudoRandomString() expects a length greater than 1');
22
        }
23
    }
24
25
    /**
26
     * Converts binary data to hexadecimal of arbitrary length.
27
     *
28
     * @param string $binaryData The binary data to convert to hex.
29
     * @param int    $length     The length of the string to return.
30
     *
31
     * @return string
32
     */
33
    public function binToHex($binaryData, $length)
34
    {
35
        return \substr(\bin2hex($binaryData), 0, $length);
36
    }
37
}
38