• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / utils / 22290136219

23 Feb 2026 01:47AM UTC coverage: 93.125% (-0.003%) from 93.128%
22290136219

push

github

dg
added CLAUDE.md

2086 of 2240 relevant lines covered (93.13%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Utils/Random.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Nette\Utils;
9

10
use Nette;
11
use Random\Randomizer;
12
use function strlen;
13
use const PHP_VERSION_ID;
14

15

16
/**
17
 * Secure random string generator.
18
 */
19
final class Random
20
{
21
        use Nette\StaticClass;
22

23
        /**
24
         * Generates a random string of given length from characters specified in second argument.
25
         * Supports intervals, such as `0-9` or `A-Z`.
26
         */
27
        public static function generate(int $length = 10, string $charlist = '0-9a-z'): string
28
        {
29
                $charlist = preg_replace_callback(
×
30
                        '#.-.#',
×
31
                        fn(array $m): string => implode('', range($m[0][0], $m[0][2])),
×
32
                        $charlist,
33
                );
34
                $charlist = count_chars($charlist, mode: 3);
×
35
                $chLen = strlen($charlist);
×
36

37
                if ($length < 1) {
×
38
                        throw new Nette\InvalidArgumentException('Length must be greater than zero.');
×
39
                } elseif ($chLen < 2) {
×
40
                        throw new Nette\InvalidArgumentException('Character list must contain at least two chars.');
×
41
                } elseif (PHP_VERSION_ID >= 80300) {
×
42
                        return (new Randomizer)->getBytesFromString($charlist, $length);
×
43
                }
44

45
                $res = '';
×
46
                for ($i = 0; $i < $length; $i++) {
×
47
                        $res .= $charlist[random_int(0, $chLen - 1)];
×
48
                }
49

50
                return $res;
×
51
        }
52
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc