Skip to content

Commit be84fa3

Browse files
committed
fixes #47: Handle inherited enumerations as different onces
1 parent 8cdc1c8 commit be84fa3

File tree

2 files changed

+19
-69
lines changed

2 files changed

+19
-69
lines changed

src/MabeEnum/Enum.php

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ abstract class Enum
3232
/**
3333
* An array of available constants by class
3434
*
35-
* @var array ["$class" => ["$const" => $value, ...], ...]
35+
* @var array ["$class" => ["$name" => $value, ...], ...]
3636
*/
3737
private static $constants = array();
3838

3939
/**
4040
* Already instantiated enumerators
4141
*
42-
* @var array ["$class" => ["$const" => $instance, ...], ...]
42+
* @var array ["$class" => ["$name" => $instance, ...], ...]
4343
*/
4444
private static $instances = array();
4545

@@ -139,41 +139,27 @@ final public function getOrdinal()
139139
}
140140

141141
/**
142-
* Compare this enumerator against another enumerator and check if it's the same
142+
* Compare this enumerator against another and check if it's the same.
143143
*
144144
* @param mixed $enum
145145
* @return bool
146146
*/
147147
final public function is($enum)
148148
{
149-
return $this->value === $enum
150-
|| (($enum instanceof static || $this instanceof $enum) && $this->value === $enum->getValue());
149+
return $this === $enum || $this->value === $enum;
151150
}
152151

153152
/**
154-
* Instantiate an enumerator of the given value or instance
155-
*
156-
* On passing an extended instance the instance will be returned if the value
157-
* is inherited by the called class or if $tradeExtendedAsUnknown is disabled
158-
* else an InvalidArgumentException will be thrown.
153+
* Get an enumerator instance of the given value or instance
159154
*
160155
* @param static|null|bool|int|float|string $value
161-
* @param bool $tradeExtendedAsUnknown
162156
* @return static
163157
* @throws InvalidArgumentException On an unknwon or invalid value
164158
* @throws LogicException On ambiguous constant values
165159
*/
166160
final public static function get($value, $tradeExtendedAsUnknown = true)
167161
{
168-
if ($value instanceof static) {
169-
if ($tradeExtendedAsUnknown && !defined('static::' . $value->getName())) {
170-
throw new InvalidArgumentException(sprintf(
171-
"%s::%s is not inherited from %s",
172-
get_class($value),
173-
$value->getName(),
174-
get_called_class()
175-
));
176-
}
162+
if ($value instanceof static && get_class($value) === get_called_class()) {
177163
return $value;
178164
}
179165

@@ -184,19 +170,22 @@ final public static function get($value, $tradeExtendedAsUnknown = true)
184170
if (is_scalar($value)) {
185171
throw new InvalidArgumentException('Unknown value ' . var_export($value, true));
186172
} else {
187-
throw new InvalidArgumentException('Invalid value of type ' . gettype($value));
173+
throw new InvalidArgumentException(sprintf(
174+
'Invalid value of type %s',
175+
is_object($value) ? get_class($value) : gettype($value)
176+
));
188177
}
189178
}
190179

191-
if (isset(self::$instances[$class][$name])) {
192-
return self::$instances[$class][$name];
180+
if (!isset(self::$instances[$class][$name])) {
181+
self::$instances[$class][$name] = new $class($constants[$name]);
193182
}
194183

195-
return self::$instances[$class][$name] = new $class($constants[$name]);
184+
return self::$instances[$class][$name];
196185
}
197186

198187
/**
199-
* Instantiate an enumarator by the given name
188+
* Get an enumarator instance by the given name
200189
*
201190
* @param string $name The name of the enumerator
202191
* @return static
@@ -220,7 +209,7 @@ final public static function getByName($name)
220209
}
221210

222211
/**
223-
* Instantiate an enumeration by the given ordinal number
212+
* Get an enumeration instance by the given ordinal number
224213
*
225214
* @param int $ordinal The ordinal number or the enumerator
226215
* @return static
@@ -314,7 +303,7 @@ private static function detectConstants($class)
314303
}
315304

316305
/**
317-
* Instantiate an enumarator by the given name.
306+
* Get an enumarator instance by the given name.
318307
*
319308
* This will be called automatically on calling a method
320309
* with the same name of a defined enumerator.

tests/MabeEnumTest/EnumTest.php

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,6 @@ public function testGetByInstance()
101101
public function testGetByExtendedInstanceOfKnownValue()
102102
{
103103
$enum = EnumInheritance::get(EnumInheritance::ONE);
104-
$this->assertSame($enum, EnumBasic::get($enum));
105-
}
106-
107-
public function testGetByExtendedInstanceOfUnknownValueExplicitAllowed()
108-
{
109-
$enum = EnumInheritance::get(EnumInheritance::INHERITANCE);
110-
$this->assertSame($enum, EnumBasic::get($enum, false));
111-
}
112-
113-
public function testGetByExtendedInstanceOfUnknownValueThrowsInvalidArgumentException()
114-
{
115-
$enum = EnumInheritance::get(EnumInheritance::INHERITANCE);
116-
117-
$this->setExpectedException(
118-
'InvalidArgumentException',
119-
'MabeEnumTest\TestAsset\EnumInheritance::INHERITANCE is not inherited from MabeEnumTest\TestAsset\EnumBasic'
120-
);
121-
EnumBasic::get($enum, true);
122-
}
123-
124-
public function testGetByInstanceOfDifferentBaseThrowsInvalidArgumentException()
125-
{
126-
$enum = EnumBasic2::get(EnumBasic2::ONE);
127104

128105
$this->setExpectedException('InvalidArgumentException');
129106
EnumBasic::get($enum);
@@ -148,25 +125,9 @@ public function testIsBasic()
148125
$this->assertFalse($enum->is('1')); // wrong value by strict comparison
149126

150127
// by instance
151-
$this->assertTrue($enum->is(EnumBasic::ONE())); // same
152-
$this->assertFalse($enum->is(EnumBasic::TWO())); // not the same
153-
$this->assertTrue($enum->is(EnumInheritance::ONE())); // same by extended instance
154-
$this->assertFalse($enum->is(EnumBasic2::ONE())); // same value but different instance
155-
}
156-
157-
public function testIsExtended()
158-
{
159-
$enum1Basic = EnumBasic::ONE();
160-
$enum1Inherited = EnumInheritance::ONE();
161-
$enumExtended = EnumInheritance::INHERITANCE();
162-
163-
$this->assertTrue($enum1Basic->is($enum1Inherited));
164-
$this->assertTrue($enum1Inherited->is($enum1Basic));
165-
166-
$this->assertFalse($enumExtended->is($enum1Basic));
167-
$this->assertFalse($enumExtended->is($enum1Inherited));
168-
$this->assertFalse($enum1Basic->is($enumExtended));
169-
$this->assertFalse($enum1Inherited->is($enumExtended));
128+
$this->assertTrue($enum->is(EnumBasic::ONE())); // same
129+
$this->assertFalse($enum->is(EnumBasic::TWO())); // different enumerators
130+
$this->assertFalse($enum->is(EnumInheritance::ONE())); // different enumeration type
170131
}
171132

172133
public function testCallingGetOrdinalTwoTimesWillResultTheSameValue()

0 commit comments

Comments
 (0)