Completed
Push — master ( 805a4d...bf6d48 )
by BENOIT
08:56
created

StringCombinations::getRandomString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\StringCombinations;
4
5
use function BenTools\CartesianProduct\cartesian_product;
6
7
final class StringCombinations implements \IteratorAggregate, \Countable
8
{
9
    /**
10
     * @var string[]
11
     */
12
    private $charset;
13
14
    /**
15
     * @var int
16
     */
17
    private $min;
18
19
    /**
20
     * @var int
21
     */
22
    private $max;
23
24
    /**
25
     * @var int
26
     */
27
    private $count;
28
29
    /**
30
     * @var string
31
     */
32
    private $glue;
33
34
    /**
35
     * StringCombination constructor.
36
     * @param mixed  $charset
37
     * @param int    $min
38
     * @param int    $max
39
     * @param string $glue
40
     * @throws \InvalidArgumentException
41
     */
42
    public function __construct($charset, $min = 1, $max = null, $glue = '')
43
    {
44
        if (is_string($charset) || is_integer($charset)) {
45
            $this->charset = preg_split('/(?<!^)(?!$)/u', $charset);
46
            $this->validateCharset($this->charset);
47
        } elseif (is_array($charset)) {
48
            $this->charset = $charset;
49
            $this->validateCharset($this->charset);
50
        } else {
51
            $this->denyCharset();
52
        }
53
        $this->min = (int) $min;
54
        $this->max = is_null($max) ? count($this->charset) : (int) $max;
55
        $this->glue = $glue;
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    public function count()
62
    {
63
        if (null === $this->count) {
64
            $this->count = array_sum(array_map(function ($set) {
0 ignored issues
show
Documentation Bug introduced by
It seems like array_sum(array_map(func...this->generateSets()))) can also be of type double. However, the property $count is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
65
                return count(cartesian_product($set));
66
            }, iterator_to_array($this->generateSets())));
67
        }
68
        return $this->count;
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function getIterator()
75
    {
76
        foreach ($this->generateSets() as $set) {
77
            foreach (cartesian_product($set) as $combination) {
78
                yield implode($this->glue, $combination);
79
            }
80
        }
81
    }
82
83
    /**
84
     * Creates a random string from current charset
85
     * @return string
86
     */
87
    public function getRandomString()
88
    {
89
        $length = random_int($this->min, $this->max);
90
        $charset = $this->charset;
91
        for ($pos = 0, $str = []; $pos < $length; $pos++) {
92
            shuffle($charset);
93
            $str[] = $charset[0];
94
        }
95
        return implode('', $str);
96
    }
97
98
    /**
99
     * @return array
100
     */
101
    public function asArray()
102
    {
103
        return iterator_to_array($this);
104
    }
105
106
    /**
107
     * @return \Generator
108
     */
109
    private function generateSets()
110
    {
111
        for ($i = $this->min; $i <= $this->max; $i++) {
112
            $set = array_fill(0, $i, $this->charset);
113
            yield $set;
114
        }
115
    }
116
117
    private function validateCharset($charset)
118
    {
119
        if (is_null($charset)) {
120
            $this->denyCharset();
121
        }
122
        foreach ($charset as $value) {
123
            if (!is_string($value) && !is_integer($value)) {
124
                $this->denyCharset();
125
            }
126
        }
127
    }
128
129
    /**
130
     * @throws \InvalidArgumentException
131
     */
132
    private function denyCharset()
133
    {
134
        throw new \InvalidArgumentException('Charset should be a string or an array of strings.');
135
    }
136
}
137