Randstring::generate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 2
Metric Value
c 5
b 1
f 2
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Jamosaur\Randstring;
4
5
class Randstring
6
{
7
    public $combinations = [];
8
    public $adjective;
9
    public $animal;
10
    public $number;
11
    private $adjectives;
12
    private $animals;
13
    private $min;
14
    private $max;
15
    private $case;
16
    private $maxLength;
17
    private $string;
18
    private $first;
19
    private $second;
20
21
    /**
22
     * Randstring constructor.
23
     *
24
     * @param null $case (ucwords, ucfirst|sentence, camel)
25
     * @param int  $maxLength
26
     * @param int  $min
27
     * @param int  $max
28
     */
29
    public function __construct($case = null, $maxLength = 100, $min = 1, $max = 99)
30
    {
31
        $this->case = $case;
32
        $this->maxLength = $maxLength;
33
        $this->min = $min;
34
        $this->max = $max;
35
        $this->adjectives = explode(PHP_EOL, file_get_contents(__DIR__ . '/dictionaries/adjectives.txt'));
36
        $this->animals = explode(PHP_EOL, file_get_contents(__DIR__ . '/dictionaries/animals.txt'));
37
    }
38
39
    /**
40
     * @return mixed
41
     */
42
    public function generate()
43
    {
44
        $this->generateString();
45
        if (strlen($this->string) > $this->maxLength) {
46
            return $this->generate();
47
        }
48
49
        return $this->string;
50
    }
51
52
    /**
53
     * Generate a string.
54
     */
55
    private function generateString()
56
    {
57
        $this->generateNumbers();
58
        $this->adjective = $this->adjectives[$this->first];
59
        $this->animal = $this->animals[$this->second];
60
        switch ($this->case) {
61
            case 'ucfirst':
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...
62
                $this->string = ucfirst($this->adjective . $this->animal . $this->number);
63
                break;
64
            case 'ucwords':
65
            case 'sentence':
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...
66
                $this->string = ucfirst($this->adjective) . ucfirst($this->animal) . ucfirst($this->number);
67
                break;
68
            case 'camel':
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...
69
                $this->string = $this->adjective . ucfirst($this->animal) . $this->number;
70
                break;
71
            default:
72
                $this->string = $this->adjective . $this->animal . $this->number;
73
                break;
74
        }
75
    }
76
77
    /**
78
     * @param null $first
79
     * @param null $second
80
     */
81
    private function generateNumbers($first = null, $second = null)
82
    {
83
        $this->first = ($first) ? $first : mt_rand(0, count($this->adjectives) - 1);
84
        $this->second = ($second) ? $second : mt_rand(0, count($this->animals) - 1);
85
        $this->number = mt_rand($this->min, $this->max);
86
        if (array_key_exists($this->first . '.' . $this->second . '.' . $this->number, $this->combinations)) {
87
            $this->generateNumbers($this->first, $this->second);
88
        }
89
        $this->combinations[$this->first . '.' . $this->second . '.' . $this->number] = 1;
90
    }
91
}
92