Skip to content

Commit 0f1e96c

Browse files
author
Marc Bennewitz
committed
improved internal method EnumMap::initEnum()
1 parent 46bd372 commit 0f1e96c

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

src/MabeEnum/EnumMap.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ public function getFlags()
155155
*/
156156
public function attach($enum, $data = null)
157157
{
158-
$this->initEnum($enum);
159-
parent::attach($enum, $data);
158+
parent::attach($this->initEnum($enum), $data);
160159
}
161160

162161
/**
@@ -167,8 +166,7 @@ public function attach($enum, $data = null)
167166
public function contains($enum)
168167
{
169168
try {
170-
$this->initEnum($enum);
171-
return parent::contains($enum);
169+
return parent::contains($this->initEnum($enum));
172170
} catch (InvalidArgumentException $e) {
173171
// On an InvalidArgumentException the given argument can't be contained in this map
174172
return false;
@@ -183,8 +181,7 @@ public function contains($enum)
183181
*/
184182
public function detach($enum)
185183
{
186-
$this->initEnum($enum);
187-
parent::detach($enum);
184+
parent::detach($this->initEnum($enum));
188185
}
189186

190187
/**
@@ -195,10 +192,8 @@ public function detach($enum)
195192
*/
196193
public function getHash($enum)
197194
{
198-
$this->initEnum($enum);
199-
200195
// getHash is available since PHP 5.4
201-
return spl_object_hash($enum);
196+
return spl_object_hash($this->initEnum($enum));
202197
}
203198

204199
/**
@@ -220,8 +215,7 @@ public function offsetExists($enum)
220215
*/
221216
public function offsetGet($enum)
222217
{
223-
$this->initEnum($enum);
224-
return parent::offsetGet($enum);
218+
return parent::offsetGet($this->initEnum($enum));
225219
}
226220

227221
/**
@@ -234,8 +228,7 @@ public function offsetGet($enum)
234228
*/
235229
public function offsetSet($enum, $data = null)
236230
{
237-
$this->initEnum($enum);
238-
parent::offsetSet($enum, $data);
231+
parent::offsetSet($this->initEnum($enum), $data);
239232
}
240233

241234
/**
@@ -247,8 +240,7 @@ public function offsetSet($enum, $data = null)
247240
*/
248241
public function offsetUnset($enum)
249242
{
250-
$this->initEnum($enum);
251-
parent::offsetUnset($enum);
243+
parent::offsetUnset($this->initEnum($enum));
252244
}
253245

254246
/**
@@ -301,20 +293,19 @@ public function key()
301293
* @return Enum
302294
* @throws InvalidArgumentException On an invalid given enum
303295
*/
304-
private function initEnum(&$enum)
296+
private function initEnum($enum)
305297
{
306298
// auto instantiate
307299
if (is_scalar($enum)) {
308300
$enumClass = $this->enumClass;
309-
$enum = $enumClass::get($enum);
310-
return;
301+
return $enumClass::get($enum);
311302
}
312303

313304
// allow only enums of the same type
314305
// (don't allow instance of)
315306
$enumClass = get_class($enum);
316307
if ($enumClass && strcasecmp($enumClass, $this->enumClass) === 0) {
317-
return;
308+
return $enum;
318309
}
319310

320311
throw new InvalidArgumentException(sprintf(

0 commit comments

Comments
 (0)