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

nette / utils / 20431395313

22 Dec 2025 12:06PM UTC coverage: 93.164% (+71.8%) from 21.324%
20431395313

push

github

dg
Html::addText() accepts int|null for back compatibility [Closes #332][Closes #333]

1 of 1 new or added line in 1 file covered. (100.0%)

140 existing lines in 15 files now uncovered.

2058 of 2209 relevant lines covered (93.16%)

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
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
declare(strict_types=1);
9

10
namespace Nette\Utils;
11

12
use Nette;
13
use Random\Randomizer;
14
use function strlen;
15
use const PHP_VERSION_ID;
16

17

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

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

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

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

UNCOV
52
                return $res;
×
53
        }
54
}
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

© 2025 Coveralls, Inc