1
|
|
|
<?php |
2
|
|
|
namespace Nubs\RandomNameGenerator; |
3
|
|
|
|
4
|
|
|
use Cinam\Randomizer\Randomizer; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* A generator that uses all of the other generators randomly. |
8
|
|
|
*/ |
9
|
|
|
class All implements Generator |
10
|
|
|
{ |
11
|
|
|
/** @type array The other generators to use. */ |
12
|
|
|
protected $_generators; |
13
|
|
|
|
14
|
|
|
/** @type Cinam\Randomizer\Randomizer The random number generator. */ |
15
|
|
|
protected $_randomizer; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Initializes the All Generator with the list of generators to choose from. |
19
|
|
|
* |
20
|
|
|
* @api |
21
|
|
|
* @param array $generators The random generators to use. |
22
|
|
|
* @param \Cinam\Randomizer\Randomizer $randomizer The random number generator. |
23
|
|
|
*/ |
24
|
|
|
public function __construct(array $generators, Randomizer $randomizer = null) |
25
|
|
|
{ |
26
|
|
|
$this->_generators = $generators; |
27
|
|
|
$this->_randomizer = $randomizer; |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructs an All Generator using the default list of generators. |
32
|
|
|
* |
33
|
|
|
* @api |
34
|
|
|
* @param \Cinam\Randomizer\Randomizer $randomizer The random number generator. |
35
|
|
|
* @return \Nubs\RandomNameGenerator\All The constructed generator. |
36
|
|
|
*/ |
37
|
|
|
public static function create(Randomizer $randomizer = null) |
38
|
|
|
{ |
39
|
|
|
return new self([new Alliteration($randomizer), new Vgng($randomizer)], $randomizer); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Gets a randomly generated name using the configured generators. |
44
|
|
|
* |
45
|
|
|
* @api |
46
|
|
|
* @return string A random name. |
47
|
|
|
*/ |
48
|
|
|
public function getName() |
49
|
|
|
{ |
50
|
|
|
return $this->_getRandomGenerator()->getName(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get a random generator from the list of generators. |
55
|
|
|
* |
56
|
|
|
* @return \Nubs\RandomNameGenerator\Generator A random generator. |
57
|
|
|
*/ |
58
|
|
|
protected function _getRandomGenerator() |
59
|
|
|
{ |
60
|
|
|
return $this->_randomizer ? $this->_randomizer->getArrayValue($this->_generators) : $this->_generators[array_rand($this->_generators)]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function __toString() |
67
|
|
|
{ |
68
|
|
|
return $this->getName(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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.