Passed
Push — master ( 18c276...54ad9c )
by Emmanuel
02:21
created

UniqueWord::extractARowFromDictionary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.1
6
 *
7
 * @author    Emmanuel Dyan
8
 * @copyright 2018 Emmanuel Dyan
9
 *
10
 * @package edyan/neuralyzer
11
 *
12
 * @license GNU General Public License v2.0
13
 *
14
 * @link https://github.com/edyan/neuralyzer
15
 */
16
17
namespace Edyan\Neuralyzer\Faker\Provider;
18
19
use Faker\Generator;
20
use Faker\Provider\Base;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Edyan\Neuralyzer\Faker\Provider\Base. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
21
22
/**
23
 * Class UniqueWord.
24
 */
25
class UniqueWord extends Base
26
{
27
    /**
28
     * Language to generate words from.
29
     *
30
     * @var string
31
     */
32
    private $language;
33
34
35
    private static $dictionary = [];
36
37
    /**
38
     * UniqueWord constructor.
39
     *
40
     * @param Generator $generator
41
     * @param string    $language
42
     */
43
    public function __construct(Generator $generator, string $language = 'en_US')
44
    {
45
        parent::__construct($generator);
46
        $this->language = $language;
47
    }
48
49
    /**
50
     * If not already done, load the file in memory as an array, then shuffle it
51
     *
52
     * @return string
53
     */
54
    public function uniqueWord(): string
55
    {
56
        if (empty(self::$dictionary)) {
57
            $this->loadFullDictionary();
58
        }
59
60
        return $this->extractARowFromDictionary();
61
    }
62
63
    /**
64
     *  Load the current language dictionary with a lot of words
65
     *  to always be able to get a unique value.
66
     */
67
    private function loadFullDictionary(): void
68
    {
69
        $file = __DIR__ . '/../Dictionary/' . $this->language;
70
        if (!file_exists($file)) {
71
            $file = __DIR__ . '/../Dictionary/en_US';
72
        }
73
74
        self::$dictionary = array_filter(explode(PHP_EOL, file_get_contents($file)));
75
    }
76
77
    /**
78
     * @return string
79
     * @throws \RuntimeException
80
     */
81
    private function extractARowFromDictionary(): string
82
    {
83
        if (empty(self::$dictionary)) {
84
            throw new \RuntimeException("Couldn't generate more unique words ... consider using another method");
85
        }
86
87
        $key = array_rand(self::$dictionary);
88
        $row = self::$dictionary[$key];
89
        unset(self::$dictionary[$key]);
90
91
        return $row;
92
    }
93
}
94