Skip to content

Commit a200cee

Browse files
committed
replaced internal method Enum[Set|Map]::initEnum() by Enum::get() since this does the same and is a little bit faster
1 parent 3afa5be commit a200cee

File tree

2 files changed

+21
-70
lines changed

2 files changed

+21
-70
lines changed

src/MabeEnum/EnumMap.php

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ public function setFlags($flags)
123123
} elseif (!$keyFlag) {
124124
$keyFlag = $this->flags & 7;
125125
}
126-
127126

128127
$currentFlag = $flags & 56;
129128
if ($currentFlag > 40) {
@@ -155,7 +154,8 @@ public function getFlags()
155154
*/
156155
public function attach($enum, $data = null)
157156
{
158-
parent::attach($this->initEnum($enum), $data);
157+
$enumClass = $this->enumClass;
158+
parent::attach($enumClass::get($enum), $data);
159159
}
160160

161161
/**
@@ -166,7 +166,8 @@ public function attach($enum, $data = null)
166166
public function contains($enum)
167167
{
168168
try {
169-
return parent::contains($this->initEnum($enum));
169+
$enumClass = $this->enumClass;
170+
return parent::contains($enumClass::get($enum));
170171
} catch (InvalidArgumentException $e) {
171172
// On an InvalidArgumentException the given argument can't be contained in this map
172173
return false;
@@ -181,7 +182,8 @@ public function contains($enum)
181182
*/
182183
public function detach($enum)
183184
{
184-
parent::detach($this->initEnum($enum));
185+
$enumClass = $this->enumClass;
186+
parent::detach($enumClass::get($enum));
185187
}
186188

187189
/**
@@ -193,7 +195,8 @@ public function detach($enum)
193195
public function getHash($enum)
194196
{
195197
// getHash is available since PHP 5.4
196-
return spl_object_hash($this->initEnum($enum));
198+
$enumClass = $this->enumClass;
199+
return spl_object_hash($enumClass::get($enum));
197200
}
198201

199202
/**
@@ -215,7 +218,8 @@ public function offsetExists($enum)
215218
*/
216219
public function offsetGet($enum)
217220
{
218-
return parent::offsetGet($this->initEnum($enum));
221+
$enumClass = $this->enumClass;
222+
return parent::offsetGet($enumClass::get($enum));
219223
}
220224

221225
/**
@@ -228,7 +232,8 @@ public function offsetGet($enum)
228232
*/
229233
public function offsetSet($enum, $data = null)
230234
{
231-
parent::offsetSet($this->initEnum($enum), $data);
235+
$enumClass = $this->enumClass;
236+
parent::offsetSet($enumClass::get($enum), $data);
232237
}
233238

234239
/**
@@ -240,7 +245,8 @@ public function offsetSet($enum, $data = null)
240245
*/
241246
public function offsetUnset($enum)
242247
{
243-
parent::offsetUnset($this->initEnum($enum));
248+
$enumClass = $this->enumClass;
249+
parent::offsetUnset($enumClass::get($enum));
244250
}
245251

246252
/**
@@ -286,32 +292,4 @@ public function key()
286292
throw new RuntimeException('Invalid key flag');
287293
}
288294
}
289-
290-
/**
291-
* Initialize an enumeration
292-
* @param Enum|null|boolean|int|float|string $enum
293-
* @return Enum
294-
* @throws InvalidArgumentException On an invalid given enum
295-
*/
296-
private function initEnum($enum)
297-
{
298-
// auto instantiate
299-
if (is_scalar($enum)) {
300-
$enumClass = $this->enumClass;
301-
return $enumClass::get($enum);
302-
}
303-
304-
// allow only enums of the same type
305-
// (don't allow instance of)
306-
$enumClass = get_class($enum);
307-
if ($enumClass && strcasecmp($enumClass, $this->enumClass) === 0) {
308-
return $enum;
309-
}
310-
311-
throw new InvalidArgumentException(sprintf(
312-
"The given enum of type '%s' isn't same as the required type '%s'",
313-
get_class($enum) ?: gettype($enum),
314-
$this->enumClass
315-
));
316-
}
317295
}

src/MabeEnum/EnumSet.php

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public function getFlags()
9393
*/
9494
public function attach($enum)
9595
{
96-
$enum = $this->initEnum($enum);
97-
$ordinal = $enum->getOrdinal();
96+
$enumClass = $this->enumClass;
97+
$ordinal = $enumClass::get($enum)->getOrdinal();
9898

9999
if (!($this->flags & self::UNIQUE) || !in_array($ordinal, $this->list, true)) {
100100
$this->list[] = $ordinal;
@@ -112,8 +112,8 @@ public function attach($enum)
112112
*/
113113
public function contains($enum)
114114
{
115-
$enum = $this->initEnum($enum);
116-
return in_array($enum->getOrdinal(), $this->list, true);
115+
$enumClass = $this->enumClass;
116+
return in_array($enumClass::get($enum)->getOrdinal(), $this->list, true);
117117
}
118118

119119
/**
@@ -124,9 +124,10 @@ public function contains($enum)
124124
*/
125125
public function detach($enum)
126126
{
127-
$enum = $this->initEnum($enum);
127+
$enumClass = $this->enumClass;
128+
$ordinal = $enumClass::get($enum)->getOrdinal();
128129

129-
while (($index = array_search($enum->getOrdinal(), $this->list, true)) !== false) {
130+
while (($index = array_search($ordinal, $this->list, true)) !== false) {
130131
unset($this->list[$index]);
131132
}
132133

@@ -180,32 +181,4 @@ public function count()
180181
{
181182
return count($this->list);
182183
}
183-
184-
/**
185-
* Initialize an enumeration
186-
* @param Enum|null|boolean|int|float|string $enum
187-
* @return Enum
188-
* @throws InvalidArgumentException On an invalid given enum
189-
*/
190-
private function initEnum($enum)
191-
{
192-
// auto instantiate
193-
if (is_scalar($enum)) {
194-
$enumClass = $this->enumClass;
195-
return $enumClass::get($enum);
196-
}
197-
198-
// allow only enums of the same type
199-
// (don't allow instance of)
200-
$enumClass = get_class($enum);
201-
if ($enumClass && strcasecmp($enumClass, $this->enumClass) === 0) {
202-
return $enum;
203-
}
204-
205-
throw new InvalidArgumentException(sprintf(
206-
"The given enum of type '%s' isn't same as the required type '%s'",
207-
get_class($enum) ?: gettype($enum),
208-
$this->enumClass
209-
));
210-
}
211184
}

0 commit comments

Comments
 (0)