Completed
Push — master ( a1f222...7ceccc )
by Emmanuel
03:50
created

UniqueWord::loadFullDictionary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Edyan\Neuralyzer\Faker\Provider;
6
7
use Faker\Generator;
8
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...
9
10
/**
11
 * Class UniqueWord.
12
 */
13
class UniqueWord extends Base
14
{
15
    /**
16
     * Language to generate words from.
17
     *
18
     * @var string
19
     */
20
    private $language;
21
22
23
    private static $dictionary = [];
24
25
    /**
26
     * UniqueWord constructor.
27
     *
28
     * @param Generator $generator
29
     * @param string    $language
30
     */
31
    public function __construct(Generator $generator, string $language = 'en_US')
32
    {
33
        parent::__construct($generator);
34
        $this->language = $language;
35
    }
36
37
    /**
38
     * If not already done, load the file in memory as an array, then shuffle it
39
     *
40
     * @return string
41
     */
42
    public function uniqueWord(): string
43
    {
44
        if (empty(self::$dictionary)) {
45
            $this->loadFullDictionary();
46
        }
47
48
        return $this->extractARowFromDictionnary();
49
    }
50
51
    /**
52
     *  Load the current language dictionary with a lot of words
53
     *  to always be able to get a unique value.
54
     */
55
    private function loadFullDictionary(): void
56
    {
57
        $file = __DIR__ . '/../Dictionary/' . $this->language;
58
        if (!file_exists($file)) {
59
            $file = __DIR__ . '/../Dictionary/en_US';
60
        }
61
62
        self::$dictionary = array_filter(explode(PHP_EOL, file_get_contents($file)));
63
    }
64
65
    private function extractARowFromDictionnary(): string
66
    {
67
        if (empty(self::$dictionary)) {
68
            throw new \RuntimeException("Couldn't generate more unique words ... consider using another method");
69
        }
70
71
        $key = array_rand(self::$dictionary);
72
        $row = self::$dictionary[$key];
73
        unset(self::$dictionary[$key]);
74
75
        return $row;
76
    }
77
}
78