1 | <?php |
||
42 | class Generator |
||
43 | { |
||
44 | |||
45 | /** |
||
46 | * @const Flag for uppercase letters |
||
47 | */ |
||
48 | const CHAR_UPPER = 1; |
||
49 | |||
50 | /** |
||
51 | * @const Flag for lowercase letters |
||
52 | */ |
||
53 | const CHAR_LOWER = 2; |
||
54 | |||
55 | /** |
||
56 | * @const Flag for alpha characters (combines UPPER + LOWER) |
||
57 | */ |
||
58 | const CHAR_ALPHA = 3; // CHAR_UPPER | CHAR_LOWER |
||
59 | |||
60 | /** |
||
61 | * @const Flag for digits |
||
62 | */ |
||
63 | const CHAR_DIGITS = 4; |
||
64 | |||
65 | /** |
||
66 | * @const Flag for alpha numeric characters |
||
67 | */ |
||
68 | const CHAR_ALNUM = 7; // CHAR_ALPHA | CHAR_DIGITS |
||
69 | |||
70 | /** |
||
71 | * @const Flag for uppercase hexadecimal symbols |
||
72 | */ |
||
73 | const CHAR_UPPER_HEX = 12; // 8 | CHAR_DIGITS |
||
74 | |||
75 | /** |
||
76 | * @const Flag for lowercase hexidecimal symbols |
||
77 | */ |
||
78 | const CHAR_LOWER_HEX = 20; // 16 | CHAR_DIGITS |
||
79 | |||
80 | /** |
||
81 | * @const Flag for base64 symbols |
||
82 | */ |
||
83 | const CHAR_BASE64 = 39; // 32 | CHAR_ALNUM |
||
84 | |||
85 | /** |
||
86 | * @const Flag for additional symbols accessible via the keyboard |
||
87 | */ |
||
88 | const CHAR_SYMBOLS = 64; |
||
89 | |||
90 | /** |
||
91 | * @const Flag for brackets |
||
92 | */ |
||
93 | const CHAR_BRACKETS = 128; |
||
94 | |||
95 | /** |
||
96 | * @const Flag for punctuation marks |
||
97 | */ |
||
98 | const CHAR_PUNCT = 256; |
||
99 | |||
100 | /** |
||
101 | * @const Flag for upper/lower-case and digits but without "B8G6I1l|0OQDS5Z2" |
||
102 | */ |
||
103 | const EASY_TO_READ = 512; |
||
104 | |||
105 | /** |
||
106 | * @var Mixer The mixing strategy to use for this generator instance |
||
107 | */ |
||
108 | protected $mixer = null; |
||
109 | |||
110 | /** |
||
111 | * @var array An array of random number sources to use for this generator |
||
112 | */ |
||
113 | protected $sources = array(); |
||
114 | |||
115 | /** |
||
116 | * @var array The different characters, by Flag |
||
117 | */ |
||
118 | protected $charArrays = array( |
||
119 | self::CHAR_UPPER => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', |
||
120 | self::CHAR_LOWER => 'abcdefghijklmnopqrstuvwxyz', |
||
121 | self::CHAR_DIGITS => '0123456789', |
||
122 | self::CHAR_UPPER_HEX => 'ABCDEF', |
||
123 | self::CHAR_LOWER_HEX => 'abcdef', |
||
124 | self::CHAR_BASE64 => '+/', |
||
125 | self::CHAR_SYMBOLS => '!"#$%&\'()* +,-./:;<=>?@[\]^_`{|}~', |
||
126 | self::CHAR_BRACKETS => '()[]{}<>', |
||
127 | self::CHAR_PUNCT => ',.;:', |
||
128 | ); |
||
129 | |||
130 | /** |
||
131 | * @internal |
||
132 | * @private |
||
133 | * @const string Ambiguous characters for "Easy To Read" sets |
||
134 | */ |
||
135 | const AMBIGUOUS_CHARS = 'B8G6I1l|0OQDS5Z2()[]{}:;,.'; |
||
136 | |||
137 | /** |
||
138 | * Build a new instance of the generator |
||
139 | * |
||
140 | * @param array $sources An array of random data sources to use |
||
141 | * @param Mixer $mixer The mixing strategy to use for this generator |
||
142 | */ |
||
143 | public function __construct(array $sources, Mixer $mixer) |
||
150 | |||
151 | /** |
||
152 | * Add a random number source to the generator |
||
153 | * |
||
154 | * @param Source $source The random number source to add |
||
155 | * |
||
156 | * @return Generator $this The current generator instance |
||
157 | */ |
||
158 | public function addSource(Source $source) |
||
164 | |||
165 | /** |
||
166 | * Generate a random number (string) of the requested size |
||
167 | * |
||
168 | * @param int $size The size of the requested random number |
||
169 | * |
||
170 | * @return string The generated random number (string) |
||
171 | */ |
||
172 | public function generate($size) |
||
181 | |||
182 | /** |
||
183 | * Generate a random integer with the given range |
||
184 | * |
||
185 | * @param int $min The lower bound of the range to generate |
||
186 | * @param int $max The upper bound of the range to generate |
||
187 | * |
||
188 | * @return int The generated random number within the range |
||
189 | */ |
||
190 | public function generateInt($min = 0, $max = PHP_INT_MAX) |
||
246 | |||
247 | /** |
||
248 | * Generate a random string of specified length. |
||
249 | * |
||
250 | * This uses the supplied character list for generating the new result |
||
251 | * string. |
||
252 | * |
||
253 | * @param int $length The length of the generated string |
||
254 | * @param mixed $characters String: An optional list of characters to use |
||
255 | * Integer: Character flags |
||
256 | * |
||
257 | * @return string The generated random string |
||
258 | */ |
||
259 | public function generateString($length, $characters = '') |
||
297 | |||
298 | /** |
||
299 | * Get the Mixer used for this instance |
||
300 | * |
||
301 | * @return Mixer the current mixer |
||
302 | */ |
||
303 | public function getMixer() |
||
307 | |||
308 | /** |
||
309 | * Get the Sources used for this instance |
||
310 | * |
||
311 | * @return Source[] the current mixer |
||
312 | */ |
||
313 | public function getSources() |
||
317 | |||
318 | /** |
||
319 | * Count the minimum number of bits to represent the provided number |
||
320 | * |
||
321 | * This is basically floor(log($number, 2)) |
||
322 | * But avoids float precision issues |
||
323 | * |
||
324 | * @param int $number The number to count |
||
325 | * |
||
326 | * @return int The number of bits |
||
327 | */ |
||
328 | protected function countBits($number) |
||
337 | |||
338 | /** |
||
339 | * Expand a character set bitwise spec into a string character set |
||
340 | * |
||
341 | * This will also replace EASY_TO_READ characters if the flag is set |
||
342 | * |
||
343 | * @param int $spec The spec to expand (bitwise combination of flags) |
||
344 | * |
||
345 | * @return string The expanded string |
||
346 | */ |
||
347 | protected function expandCharacterSets($spec) |
||
369 | } |
||
370 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.