Completed
Push — renovate/mocha-8.x ( 07030e...e8e64c )
by
unknown
28:17 queued 19:09
created

Tools::get_random_string()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Mocking tools.
4
 *
5
 * @package Jetpack
6
 */
7
8
namespace Automattic\Jetpack\Debug_Helper\Mocker;
9
10
/**
11
 * Mocking Tools.
12
 */
13
class Tools {
14
15
	const CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
16
17
	/**
18
	 * Generate a random string.
19
	 *
20
	 * @param int      $length Fixed string length, or minimum string length if maximum is provided.
21
	 * @param int|null $length_max Maximum string length, optional.
22
	 *
23
	 * @return string
24
	 */
25
	public static function get_random_string( $length = 15, $length_max = null ) {
26
		// phpcs:ignore WordPress.WP.AlternativeFunctions.rand_rand
27
		$length = $length_max ? rand( $length, $length_max ) : $length;
28
29
		$char_length = strlen( self::CHARS );
30
31
		for ( $string = '', $i = 0; $i < $length; $i++ ) {
32
			// phpcs:ignore WordPress.WP.AlternativeFunctions.rand_rand
33
			$string .= self::CHARS[ rand( 0, $char_length - 1 ) ];
34
		}
35
36
		return $string;
37
	}
38
39
}
40