Internet::domainWord()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 14
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0185
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace DummyGenerator\Core;
6
7
use DummyGenerator\Definitions\Extension\Awareness\GeneratorAwareExtensionInterface;
8
use DummyGenerator\Definitions\Extension\Awareness\GeneratorAwareExtensionTrait;
9
use DummyGenerator\Definitions\Extension\Awareness\RandomizerAwareExtensionInterface;
10
use DummyGenerator\Definitions\Extension\Awareness\RandomizerAwareExtensionTrait;
11
use DummyGenerator\Definitions\Extension\Awareness\ReplacerAwareExtensionInterface;
12
use DummyGenerator\Definitions\Extension\Awareness\ReplacerAwareExtensionTrait;
13
use DummyGenerator\Definitions\Extension\Exception\ExtensionRuntimeException;
14
use DummyGenerator\Definitions\Extension\InternetExtensionInterface;
15
16
class Internet implements
17
    InternetExtensionInterface,
18
    GeneratorAwareExtensionInterface,
19
    RandomizerAwareExtensionInterface,
20
    ReplacerAwareExtensionInterface
21
{
22
    use GeneratorAwareExtensionTrait;
23
    use RandomizerAwareExtensionTrait;
24
    use ReplacerAwareExtensionTrait;
25
26
    /** @var string[] */
27
    protected array $freeEmailDomain = ['gmail.com', 'yahoo.com', 'hotmail.com'];
28
29
    /** @var string[] */
30
    protected array $tld = ['com', 'com', 'com', 'com', 'com', 'com', 'biz', 'info', 'net', 'org'];
31
32
    /** @var string[] */
33
    protected array $userNameFormats = [
34
        '{{lastName}}.{{firstName}}',
35
        '{{firstName}}.{{lastName}}',
36
        '{{firstName}}##',
37
        '?{{lastName}}',
38
    ];
39
40
    /** @var string[] */
41
    protected array $emailFormats = [
42
        '{{userName}}@{{domainName}}',
43
        '{{userName}}@{{freeEmailDomain}}',
44
    ];
45
46
    /** @var string[] */
47
    protected array $urlFormats = [
48
        'http://www.{{domainName}}/',
49
        'http://{{domainName}}/',
50
        'http://www.{{domainName}}/{{slug}}',
51
        'http://www.{{domainName}}/{{slug}}',
52
        'https://www.{{domainName}}/{{slug}}',
53
        'http://www.{{domainName}}/{{slug}}.html',
54
        'http://{{domainName}}/{{slug}}',
55
        'http://{{domainName}}/{{slug}}',
56
        'http://{{domainName}}/{{slug}}.html',
57
        'https://{{domainName}}/{{slug}}.html',
58
    ];
59
60
    /**
61
     * @var array<int,array<string>>
62
     *
63
     * @see https://tools.ietf.org/html/rfc1918#section-3
64
     */
65
    protected array $localIpBlocks = [
66
        ['10.0.0.0', '10.255.255.255'],
67
        ['172.16.0.0', '172.31.255.255'],
68
        ['192.168.0.0', '192.168.255.255'],
69
    ];
70
71 1
    public function email(): string
72
    {
73 1
        $format = $this->randomizer->randomElement($this->emailFormats);
74
75 1
        return $this->generator->parse($format);
76
    }
77
78 1
    public function safeEmail(): string
79
    {
80 1
        return preg_replace('/\s/u', '', $this->userName() . '@' . $this->safeEmailDomain()) ?? '';
81
    }
82
83 1
    public function freeEmail(): string
84
    {
85 1
        return preg_replace('/\s/u', '', $this->userName() . '@' . $this->freeEmailDomain()) ?? '';
86
    }
87
88 2
    public function companyEmail(): string
89
    {
90 2
        return preg_replace('/\s/u', '', $this->userName() . '@' . $this->domainName()) ?? '';
91
    }
92
93 2
    public function freeEmailDomain(): string
94
    {
95 2
        return $this->randomizer->randomElement($this->freeEmailDomain);
96
    }
97
98 2
    public function safeEmailDomain(): string
99
    {
100 2
        $domains = [
101 2
            'example.com',
102 2
            'example.org',
103 2
            'example.net',
104 2
        ];
105
106 2
        return $this->randomizer->randomElement($domains);
107
    }
108
109 6
    public function userName(): string
110
    {
111 6
        $format = $this->randomizer->randomElement($this->userNameFormats);
112 6
        $username = $this->replacer->bothify($this->generator->parse($format));
113 6
        $username = $this->replacer->toLower($this->replacer->transliterate($username));
114
115
        // check if transliterate() didn't support the language and removed all letters
116 6
        if (trim($username, '._') === '') {
117
            throw new ExtensionRuntimeException('userName failed with the selected locale. Try a different locale or activate the "intl" PHP extension.');
118
        }
119
120
        // clean possible trailing dots from first/last names
121 6
        $username = str_replace('..', '.', $username);
122 6
        return rtrim($username, '.');
123
    }
124
125 1
    public function password(int $minLength = 6, int $maxLength = 20): string
126
    {
127 1
        $pattern = str_repeat('?', $this->randomizer->getInt($minLength, $maxLength));
128
129 1
        return $this->replacer->lexify($pattern, true);
130
    }
131
132 4
    public function domainName(): string
133
    {
134 4
        return $this->domainWord() . '.' . $this->tld();
135
    }
136
137 5
    public function domainWord(): string
138
    {
139
        // @phpstan-ignore-next-line
140 5
        $lastName = $this->generator->lastName();
0 ignored issues
show
Bug introduced by
The method lastName() does not exist on DummyGenerator\DummyGenerator. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
        /** @scrutinizer ignore-call */ 
141
        $lastName = $this->generator->lastName();
Loading history...
141
142 5
        $lastName = $this->replacer->toLower($this->replacer->transliterate($lastName));
143
144
        // check if transliterate() didn't support the language and removed all letters
145 5
        if (trim($lastName, '._') === '') {
146
            throw new ExtensionRuntimeException('domainWord failed with the selected locale. Try a different locale or activate the "intl" PHP extension.');
147
        }
148
149
        // clean possible trailing dot from last name
150 5
        return rtrim($lastName, '.');
151
    }
152
153 5
    public function tld(): string
154
    {
155 5
        return $this->randomizer->randomElement($this->tld);
156
    }
157
158 1
    public function url(): string
159
    {
160 1
        $format = $this->randomizer->randomElement($this->urlFormats);
161
162 1
        return $this->generator->parse($format);
163
    }
164
165 2
    public function slug(int $nbWords = 6, bool $variableNbWords = true): string
166
    {
167 2
        if ($nbWords <= 0) {
168
            return '';
169
        }
170
171 2
        if ($variableNbWords) {
172 1
            $nbWords = (int) ($nbWords * $this->randomizer->getInt(60, 140) / 100) + 1;
173
        }
174
175
        // @phpstan-ignore-next-line
176 2
        $words = $this->generator->words($nbWords);
0 ignored issues
show
Bug introduced by
The method words() does not exist on DummyGenerator\DummyGenerator. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

176
        /** @scrutinizer ignore-call */ 
177
        $words = $this->generator->words($nbWords);
Loading history...
177
178 2
        return implode('-', $words);
179
    }
180
181 1
    public function ipv4(): string
182
    {
183 1
        $ipv4 = long2ip($this->randomizer->getBool() ? $this->randomizer->getInt(-2147483648, -2) : $this->randomizer->getInt(16777216, 2147483647));
184
185 1
        if ($ipv4 === false) {
186
            throw new ExtensionRuntimeException('IPv4 failed with the selected data.');
187
        }
188
189 1
        return $ipv4;
190
    }
191
192 1
    public function ipv6(): string
193
    {
194 1
        $res = [];
195
196 1
        for ($i = 0; $i < 8; ++$i) {
197 1
            $res[] = dechex($this->randomizer->getInt(0, 65535));
198
        }
199
200 1
        return implode(':', $res);
201
    }
202
203 1
    public function localIpv4(): string
204
    {
205 1
        $ipBlock = $this->randomizer->randomElement($this->localIpBlocks);
206
207 1
        $localIpv4 = long2ip($this->randomizer->getInt((int) ip2long($ipBlock[0]), (int) ip2long($ipBlock[1])));
208
209 1
        if ($localIpv4 === false) {
210
            throw new ExtensionRuntimeException('IPv4 failed with the selected data.');
211
        }
212
213 1
        return $localIpv4;
214
    }
215
216 1
    public function macAddress(): string
217
    {
218 1
        $mac = [];
219
220 1
        for ($i = 0; $i < 6; ++$i) {
221 1
            $mac[] = sprintf('%02X', $this->randomizer->getInt(0, 0xff));
222
        }
223
224 1
        return implode(':', $mac);
225
    }
226
}
227