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.

Alliteration::_getRandomWord()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
namespace Nubs\RandomNameGenerator;
3
4
use Cinam\Randomizer\Randomizer;
5
6
/**
7
 * Defines an alliterative name generator.
8
 */
9
class Alliteration extends AbstractGenerator implements Generator
10
{
11
    /** @type array The definition of the potential adjectives. */
12
    protected $_adjectives;
13
14
    /** @type array The definition of the potential nouns. */
15
    protected $_nouns;
16
17
    /** @type Cinam\Randomizer\Randomizer The random number generator. */
18
    protected $_randomizer;
19
20
    /**
21
     * Initializes the Alliteration Generator with the default word lists.
22
     *
23
     * @api
24
     * @param \Cinam\Randomizer\Randomizer $randomizer The random number generator.
25
     */
26
    public function __construct(Randomizer $randomizer = null)
27
    {
28
        $this->_randomizer = $randomizer;
29
        $this->_adjectives = file(__DIR__ . '/adjectives.txt', FILE_IGNORE_NEW_LINES);
30
        $this->_nouns = file(__DIR__ . '/nouns.txt', FILE_IGNORE_NEW_LINES);
31
    }
32
33
    /**
34
     * Gets a randomly generated alliterative name.
35
     *
36
     * @api
37
     * @return string A random alliterative name.
38
     */
39
    public function getName()
40
    {
41
        $adjective = $this->_getRandomWord($this->_adjectives);
42
        $noun = $this->_getRandomWord($this->_nouns, $adjective[0]);
43
44
        return ucwords("{$adjective} {$noun}");
45
    }
46
47
    /**
48
     * Get a random word from the list of words, optionally filtering by starting letter.
49
     *
50
     * @param array $words An array of words to choose from.
51
     * @param string $startingLetter The desired starting letter of the word.
52
     * @return string The random word.
53
     */
54
    protected function _getRandomWord(array $words, $startingLetter = null)
55
    {
56
        $wordsToSearch = $startingLetter === null ? $words : preg_grep("/^{$startingLetter}/", $words);
57
        return $this->_randomizer ? $this->_randomizer->getArrayValue($wordsToSearch) : $wordsToSearch[array_rand($wordsToSearch)];
58
    }
59
}
60