GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Vgng::getName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
namespace Nubs\RandomNameGenerator;
3
4
use Cinam\Randomizer\Randomizer;
5
6
/**
7
 * Defines a video game name generator based off of
8
 * https://github.com/nullpuppy/vgng which in turn is based off of
9
 * http://videogamena.me/vgng.js.
10
 */
11
class Vgng extends AbstractGenerator implements Generator
12
{
13
    /** @type array The definition of the potential names. */
14
    protected $_definitionSets;
15
16
    /** @type Cinam\Randomizer\Randomizer The random number generator. */
17
    protected $_randomizer;
18
19
    /**
20
     * Initializes the Video Game Name Generator from the default word list.
21
     *
22
     * @api
23
     * @param \Cinam\Randomizer\Randomizer $randomizer The random number generator.
24
     */
25
    public function __construct(Randomizer $randomizer = null)
26
    {
27
        $this->_randomizer = $randomizer;
28
        $this->_definitionSets = array_map(
29
            [$this, '_parseSection'],
30
            $this->_getSections($this->_getFileContents())
31
        );
32
    }
33
34
    /**
35
     * Gets a randomly generated video game name.
36
     *
37
     * @api
38
     * @return string A random video game name.
39
     */
40
    public function getName()
41
    {
42
        $similarWords = [];
43
        $words = [];
44
45
        foreach ($this->_definitionSets as $definitionSet) {
46
            $word = $this->_getUniqueWord($definitionSet, $similarWords);
47
            $words[] = $word['word'];
48
            $similarWords[] = $word['word'];
49
            $similarWords = array_merge($similarWords, $word['similarWords']);
50
        }
51
52
        return implode(' ', $words);
53
    }
54
55
    /**
56
     * Get a definition from the definitions that does not exist already.
57
     *
58
     * @param array $definitions The word list to pick from.
59
     * @param array $existingWords The already-chosen words to avoid.
60
     * @return array The definition of a previously unchosen word.
61
     */
62
    protected function _getUniqueWord(array $definitions, array $existingWords)
63
    {
64
        $definition = $this->_randomizer ? $this->_randomizer->getArrayValue($definitions) : $definitions[array_rand($definitions)];
65
66
        if (array_search($definition['word'], $existingWords) === false) {
67
            return $definition;
68
        }
69
70
        return $this->_getUniqueWord($definitions, $existingWords);
71
    }
72
73
    /**
74
     * Gets the file contents of the video_game_names.txt file.
75
     *
76
     * @return string The video_game_names.txt contents.
77
     */
78
    protected function _getFileContents()
79
    {
80
        return file_get_contents(__DIR__ . '/video_game_names.txt');
81
    }
82
83
    /**
84
     * Separates the contents into each of the word list sections.
85
     *
86
     * This builder operates by picking a random word from each of a consecutive
87
     * list of word lists.  These separate lists are separated by a line
88
     * consisting of four hyphens in the file.
89
     *
90
     * @param string $contents The file contents.
91
     * @return array Each section split into its own string.
92
     */
93
    protected function _getSections($contents)
94
    {
95
        return array_map('trim', explode('----', $contents));
96
    }
97
98
    /**
99
     * Parses the given section into the final definitions.
100
     *
101
     * @param string $section The newline-separated list of words in a section.
102
     * @return array The parsed section into its final form.
103
     */
104
    protected function _parseSection($section)
105
    {
106
        return array_map(
107
            [$this, '_parseDefinition'],
108
            $this->_getDefinitionsFromSection($section)
109
        );
110
    }
111
112
    /**
113
     * Gets the separate definitions from the given section.
114
     *
115
     * @param string $section The newline-separated list of words in a section.
116
     * @return array Each word split out, but unparsed.
117
     */
118
    protected function _getDefinitionsFromSection($section)
119
    {
120
        return array_map('trim', explode("\n", $section));
121
    }
122
123
    /**
124
     * Parses a single definition into its component pieces.
125
     *
126
     * The format of a definition in a file is word[^similarWord|...].
127
     *
128
     * @param string $definition The definition.
129
     * @return array The formatted definition.
130
     */
131
    protected function _parseDefinition($definition)
132
    {
133
        $word = strtok($definition, '^');
134
        $similarWords = array_filter(explode('|', strtok('^')));
135
136
        return ['word' => $word, 'similarWords' => $similarWords];
137
    }
138
}
139