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.
Completed
Pull Request — master (#34)
by
unknown
03:38 queued 02:05
created

Vgng   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 136
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getName() 0 14 2
A _getUniqueWord() 0 10 3
A _getFileContents() 0 4 1
A _getSections() 0 4 1
A _parseSection() 0 7 1
A _getDefinitionsFromSection() 0 4 1
A _parseDefinition() 0 7 1
A __toString() 0 4 1
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 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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $randomizer can also be of type object<Cinam\Randomizer\Randomizer>. However, the property $_randomizer is declared as type object<Nubs\RandomNameGe...\Randomizer\Randomizer>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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
    /**
140
     * @return string
141
     */
142
    public function __toString()
143
    {
144
        return $this->getName();
145
    }    
146
}
147