RandomGeneratorComponent::randomName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 3
Ratio 33.33 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 3
loc 9
rs 9.6666
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace NuvoleWeb\Drupal\DrupalExtension\Component;
4
5
/**
6
 * Class RandomGeneratorComponent.
7
 *
8
 * @package NuvoleWeb\Drupal\DrupalExtension\Component
9
 */
10
class RandomGeneratorComponent {
11
12
  /**
13
   * Generates a random string of ASCII characters of codes 32 to 126.
14
   *
15
   * The generated string includes alpha-numeric characters and common
16
   * miscellaneous characters. Use this method when testing general input
17
   * where the content is not restricted.
18
   *
19
   * Do not use this method when special characters are not possible (e.g., in
20
   * machine or file names that have already been validated); instead,
21
   * use self::randomName().
22
   *
23
   * @param int $length
24
   *   Length of random string to generate.
25
   *
26
   * @return string
27
   *   Randomly generated string.
28
   *
29
   * @see DrupalWebTestCase::randomString()
30
   */
31
  public static function randomString($length = 8) {
32
    $str = '';
33 View Code Duplication
    for ($i = 0; $i < $length; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
      $str .= chr(mt_rand(32, 126));
35
    }
36
    return $str;
37
  }
38
39
  /**
40
   * Generates a random string containing letters and numbers.
41
   *
42
   * The string will always start with a letter. The letters may be upper or
43
   * lower case. This method is better for restricted inputs that do not
44
   * accept certain characters. For example, when testing input fields that
45
   * require machine readable values (i.e. without spaces and non-standard
46
   * characters) this method is best.
47
   *
48
   * Do not use this method when testing unvalidated user input. Instead, use
49
   * self::randomString().
50
   *
51
   * @param int $length
52
   *   Length of random string to generate.
53
   *
54
   * @return string
55
   *   Randomly generated string.
56
   *
57
   * @see DrupalWebTestCase::randomName()
58
   */
59
  public static function randomName($length = 8) {
60
    $values = array_merge(range(65, 90), range(97, 122), range(48, 57));
61
    $max = count($values) - 1;
62
    $str = chr(mt_rand(97, 122));
63 View Code Duplication
    for ($i = 1; $i < $length; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
      $str .= chr($values[mt_rand(0, $max)]);
65
    }
66
    return $str;
67
  }
68
69
}
70