1 | <?php namespace BuildR\Utils\Enumeration; |
||
20 | class EnumerationBase implements StringConvertibleInterface { |
||
21 | |||
22 | /** |
||
23 | * @type mixed |
||
24 | */ |
||
25 | public $value; |
||
26 | |||
27 | /** |
||
28 | * @type array |
||
29 | */ |
||
30 | private static $cache = []; |
||
31 | |||
32 | /** |
||
33 | * EnumerationBase constructor |
||
34 | * |
||
35 | * @param mixed $value |
||
36 | * |
||
37 | * @throws \BuildR\Utils\Enumeration\Exception\EnumerationException |
||
38 | */ |
||
39 | 21 | public function __construct($value) { |
|
46 | |||
47 | /** |
||
48 | * Determines that the current value in the enumeration is valid. |
||
49 | * |
||
50 | * @param mixed $value |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | 21 | public function isValid($value) { |
|
57 | |||
58 | /** |
||
59 | * Returns the enumeration current value |
||
60 | * |
||
61 | * @return mixed |
||
62 | */ |
||
63 | 1 | public function getValue() { |
|
66 | |||
67 | /** |
||
68 | * Translate the current enumeration to an associative array |
||
69 | * |
||
70 | * @return array |
||
71 | */ |
||
72 | 21 | public static function toArray() { |
|
82 | |||
83 | /** |
||
84 | * Returns an indexed array that contains all key from this enumeration as value. |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | 2 | public static function getKeys() { |
|
91 | |||
92 | /** |
||
93 | * Determines that the given key is exist in the current enumeration or not. |
||
94 | * |
||
95 | * @param string $key |
||
96 | * |
||
97 | * @return bool |
||
98 | * |
||
99 | * @throws \BuildR\Utils\Enumeration\Exception\EnumerationException |
||
100 | */ |
||
101 | 7 | public static function isValidKey($key) { |
|
108 | |||
109 | /** |
||
110 | * Return the sie of the current enumeration |
||
111 | * |
||
112 | * @return int |
||
113 | */ |
||
114 | 1 | public static function size() { |
|
117 | |||
118 | /** |
||
119 | * This is a proxy method. This is called when the enumeration property called as a method. |
||
120 | * This method creates a new instance from the parent class with the value defined by |
||
121 | * the constant. |
||
122 | * |
||
123 | * @param string $name The constant name |
||
124 | * @param array $arguments This function not take any arguments |
||
125 | * |
||
126 | * @return object |
||
127 | */ |
||
128 | 21 | public static function __callStatic($name, $arguments) { |
|
129 | 21 | $calledClass = get_called_class(); |
|
130 | 21 | $args[] = $name; |
|
|
|||
131 | |||
132 | 21 | if(array_key_exists(EnumerationFieldDefinitionInterface::class, class_implements($calledClass))) { |
|
133 | 2 | $fields = static::defineFields()[$name]; |
|
134 | 2 | array_unshift($fields, $args[0]); |
|
135 | |||
136 | 2 | $args = $fields; |
|
137 | 2 | } |
|
138 | |||
139 | 21 | $reflector = new ReflectionClass($calledClass); |
|
140 | 21 | return $reflector->newInstanceArgs($args); |
|
141 | } |
||
142 | |||
143 | // ========================================== |
||
144 | // StringConvertibleInterface implementation |
||
145 | // ========================================== |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 1 | public function __toString() { |
|
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | 1 | public function toString() { |
|
160 | |||
161 | } |
||
162 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.