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; |
|
|
|
|
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
|
|
|
|
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 theid
property of an instance of theAccount
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.