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

Alliteration::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Nubs\RandomNameGenerator;
3
4
use Cinam\Randomizer\Randomizer;
5
6
/**
7
 * Defines an alliterative name generator.
8
 */
9
class Alliteration 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;
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...
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
    /**
61
     * @return string
62
     */
63
    public function __toString()
64
    {
65
        return $this->getName();
66
    }    
67
}
68