Skip to content

Commit d68fa09

Browse files
committed
Fixed an issue when calling Enum::option1() and then calling Enum::from('option2'), option2 was not registered.
1 parent a8903ac commit d68fa09

File tree

3 files changed

+63
-22
lines changed

3 files changed

+63
-22
lines changed

CHANGELOG-1.x.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.1.1] - 2020-09-02
10+
### Fixed
11+
- There was an issue when calling `Enum::option1()` and then calling `Enum::from('option2')`, `option2` was not registered.
12+
13+
### Updated
14+
- Use `static::clas` instead of `get_called_class()`
15+
- Use `static::$cache` instead of `self::$cache`
16+
917
## [1.1.0] - 2020-08-31
1018
### Added
1119
- `keyString` static method to fetch as string, glued by comma by default
@@ -18,7 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1826
### Added
1927
- Array and illuminate collection implementations
2028

21-
[Unreleased]: https://github.com/ekvedaras/php-enum/compare/v1.1.0...HEAD
29+
[Unreleased]: https://github.com/ekvedaras/php-enum/compare/v1.1.1...HEAD
30+
[1.1.1]: https://github.com/ekvedaras/php-enum/compare/v1.1.0...v1.1.1
2231
[1.1.0]: https://github.com/ekvedaras/php-enum/compare/v1.0.1...v1.1.0
2332
[1.0.1]: https://github.com/ekvedaras/php-enum/compare/v1.0.0...v1.0.1
2433
[1.0.0]: https://github.com/ekvedaras/php-enum/releases/tag/v1.0.0

src/BaseEnum.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,30 +134,38 @@ private static function getClassKey(): int
134134
* Static method which returns all available types
135135
*
136136
* @return static[]
137-
* @noinspection PhpDocMissingThrowsInspection
138137
*/
139138
public static function enum()
140139
{
141140
$classKey = static::getClassKey();
142141

143-
if (!isset(self::$cache)) {
144-
self::$cache = static::getNewStorage();
142+
if (!isset(static::$cache)) {
143+
static::$cache = static::getNewStorage();
145144
}
146145

147-
if (!static::existsInStorage(self::$cache, $classKey)) {
148-
$methods = static::getNewStorage();
146+
if (!static::existsInStorage(static::$cache, $classKey)) {
147+
static::build($classKey);
148+
}
149149

150-
/** @noinspection PhpUnhandledExceptionInspection */
151-
foreach ((new ReflectionClass(get_called_class()))->getMethods(ReflectionMethod::IS_FINAL) as $method) {
152-
/** @var static $enum */
153-
$enum = $method->invoke(null);
154-
static::putToStorage($methods, $enum->id(), $enum);
155-
}
150+
return static::getFromStorage(static::$cache, $classKey);
151+
}
152+
153+
/**
154+
* Build instances of enum options
155+
*
156+
* @param int $classKey
157+
*/
158+
protected static function build(int $classKey)
159+
{
160+
$methods = static::getNewStorage();
156161

157-
static::putToStorage(self::$cache, $classKey, $methods);
162+
foreach ((new ReflectionClass(static::class))->getMethods(ReflectionMethod::IS_FINAL) as $method) {
163+
/** @var static $enum */
164+
$enum = $method->invoke(null);
165+
static::putToStorage($methods, $enum->id(), $enum);
158166
}
159167

160-
return static::getFromStorage(self::$cache, $classKey);
168+
static::putToStorage(static::$cache, $classKey, $methods);
161169
}
162170

163171
/**
@@ -215,7 +223,6 @@ public static function __set_state(array $state): self
215223
* @param string|int $id
216224
* @param string|null $name
217225
* @param null $meta
218-
* @noinspection PhpDocMissingThrowsInspection
219226
* @return static
220227
*/
221228
protected static function get($id, string $name = null, $meta = null): self
@@ -226,19 +233,19 @@ protected static function get($id, string $name = null, $meta = null): self
226233

227234
$classKey = static::getClassKey();
228235

229-
if (!isset(self::$cache)) {
230-
self::$cache = static::getNewStorage();
236+
if (!isset(static::$cache)) {
237+
static::$cache = static::getNewStorage();
231238
}
232239

233-
if (!static::existsInStorage(self::$cache, $classKey)) {
234-
static::putToStorage(self::$cache, $classKey, static::getNewStorage());
240+
if (!static::existsInStorage(static::$cache, $classKey)) {
241+
static::putToStorage(static::$cache, $classKey, static::getNewStorage());
242+
static::build($classKey);
235243
}
236244

237-
$instances = static::getFromStorage(self::$cache, $classKey);
245+
$instances = static::getFromStorage(static::$cache, $classKey);
238246

239247
if (!static::existsInStorage($instances, $id)) {
240-
/** @noinspection PhpUnhandledExceptionInspection */
241-
$reflection = new ReflectionClass(get_called_class());
248+
$reflection = new ReflectionClass(static::class);
242249
$instance = $reflection->newInstanceWithoutConstructor();
243250

244251
$refConstructor = $reflection->getConstructor();

tests/BaseEnumTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,31 @@ public function enums(): array
4949
];
5050
}
5151

52+
/**
53+
* @test
54+
* @dataProvider enums
55+
* @param BaseEnum|PaymentStatusOptions|string $enum
56+
*/
57+
public function it_does_not_crash_when_calling_from_for_yet_unregistered_id(string $enum)
58+
{
59+
$pendingKey = self::PAYMENT_STATUS_PENDING;
60+
$completedId = self::PAYMENT_STATUS_IDS[self::PAYMENT_STATUS_COMPLETED];
61+
62+
$this->assertInstanceOf($enum, $enum::$pendingKey());
63+
$this->assertInstanceOf($enum, $enum::from($completedId));
64+
}
65+
66+
/**
67+
* @test
68+
* @dataProvider enums
69+
* @runInSeparateProcess
70+
* @param BaseEnum|PaymentStatusOptions|string $enum
71+
*/
72+
public function it_does_not_crash_when_calling_enum_first(string $enum)
73+
{
74+
$this->assertNotEmpty($enum::enum());
75+
}
76+
5277
/**
5378
* @test
5479
* @dataProvider enums

0 commit comments

Comments
 (0)