Skip to content

Commit f31c6ae

Browse files
committed
[fix] core
1. Prevented fatal error caused by invalid enum value assignment in property resolution.
1 parent 8aee686 commit f31c6ae

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

CHANGELOG.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
11
# CHANGELOG
22

3+
## [v3.1.1] - 2025-11-05
4+
5+
### Fixed
6+
7+
- Prevented fatal error caused by invalid enum value assignment in property resolution.
8+
39
## [v3.1.0] - 2025-10-29
410

5-
### ⚠️Note
11+
### ⚠️Note
12+
613
> Starting from this release, the CHANGELOG will be maintained in English only.<br>
714
> 自本版本後僅提供英文版 CHANGELOG,不再提供中文版內容。
815
916
### Added
1017

11-
* Added `fromArray()` and `fromJson()` as new object construction entry points.
18+
- Added `fromArray()` and `fromJson()` as new object construction entry points.
1219
Direct instantiation via `new` is no longer recommended.
13-
* Added **Traditional Chinese documentation** `README_TW.md`, providing bilingual documentation and badges.
14-
* Added **class-based object APIs**: `ReallifeKip\ImmutableBase\Objects\{DataTransferObject, ValueObject, Entity}`, allowing direct inheritance via `extends`.
15-
* Added **Attributes namespace**: `ReallifeKip\ImmutableBase\Attributes\{DataTransferObject, ValueObject, Entity, ArrayOf}`.
16-
* Added a set of **granular exception classes** (main ones listed):
20+
- Added **Traditional Chinese documentation** `README_TW.md`, providing bilingual documentation and badges.
21+
- Added **class-based object APIs**: `ReallifeKip\ImmutableBase\Objects\{DataTransferObject, ValueObject, Entity}`, allowing direct inheritance via `extends`.
22+
- Added **Attributes namespace**: `ReallifeKip\ImmutableBase\Attributes\{DataTransferObject, ValueObject, Entity, ArrayOf}`.
23+
- Added a set of **granular exception classes** (main ones listed):
1724
`ImmutableBaseException`, `RuntimeException`, `LogicException`,
1825
`InvalidTypeException`, `InvalidJsonException`,
1926
`InvalidArrayException`, `InvalidArrayItemException`, `InvalidArrayValueException`,
2027
`InvalidArrayOfClassException`, `InvalidPropertyVisibilityException`,
2128
`AttributeException`, `InheritanceException`, `NonNullablePropertyException`.
22-
* `with()` and `ArrayOf` now support automatic instantiation of array elements into specified classes.
29+
- `with()` and `ArrayOf` now support automatic instantiation of array elements into specified classes.
2330
They accept **JSON strings, plain arrays, or existing instances** as input.
24-
* Completed PHPDoc for the `toArrayOrValue()` core method.
31+
- Completed PHPDoc for the `toArrayOrValue()` core method.
2532

2633
### Changed
2734

28-
* Refactored and simplified parts of the core logic in `src/ImmutableBase.php`.
29-
* Extracted **Attributes** and **Objects** into separate namespaces to improve project structure and code readability.
30-
* `with()` now supports modifying nested properties that are themselves subclasses of `ImmutableBase`, without requiring re-instantiation.
31-
* Updated `composer.json` to include relevant keywords for better package discovery.
35+
- Refactored and simplified parts of the core logic in `src/ImmutableBase.php`.
36+
- Extracted **Attributes** and **Objects** into separate namespaces to improve project structure and code readability.
37+
- `with()` now supports modifying nested properties that are themselves subclasses of `ImmutableBase`, without requiring re-instantiation.
38+
- Updated `composer.json` to include relevant keywords for better package discovery.
3239

3340
### Deprecated
3441

35-
* Direct instantiation (e.g., `new Example()`) **will be removed in v4.0.0**.
42+
- Direct instantiation (e.g., `new Example()`) **will be removed in v4.0.0**.
3643
Please use `Example::fromArray()` or `Example::fromJson()` instead.
37-
* `#[DataTransferObject]`, `#[ValueObject]`, and `#[Entity]` **will be deprecated in v4.0.0**.
44+
- `#[DataTransferObject]`, `#[ValueObject]`, and `#[Entity]` **will be deprecated in v4.0.0**.
3845
Use the **class-based API** (`Objects\*`) going forward.
3946

40-
4147
## [v3.0.3] - 2025-10-17
4248

4349
- 修復 PHP 8.2 取得反射屬性名稱失敗 bug

src/ImmutableBase.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,9 @@ private function unionTypeDecide(ReflectionUnionType $type, mixed $value)
568568
* - If the value is an array and the target type extends {@see ImmutableBase}, a new instance is constructed.
569569
* - If the value is already an object, it is returned as-is.
570570
* - If the type allows null and the value is null, null is returned.
571-
* - If the type represents an enum, the method attempts to resolve it via `::tryFrom()` or constant lookup.
571+
* - If the type represents an enum, the method first attempts to resolve it by matching the case name
572+
* via constant lookup (e.g. `MyEnum::CASE`), and if that fails and the enum implements {@see BackedEnum},
573+
* it will attempt resolution via `::tryFrom($value)`.
572574
*
573575
* Any mismatch between the provided value and the expected class or enum type will result
574576
* in an {@see InvalidTypeException}.
@@ -588,11 +590,13 @@ private function namedTypeDecide(ReflectionNamedType $type, mixed $value)
588590
is_object($value) => $value,
589591
$this->validNullValue($type, $value) => null,
590592
is_string($value) && enum_exists($class) => (function () use ($class, $value) {
591-
try {
592-
return $class::tryFrom($value) ?? constant("$class::$value");
593-
} catch (Throwable) {
594-
throw new InvalidTypeException("is $class and does not include '$value'.");
593+
if (defined($case = "$class::$value")) {
594+
return constant($case);
595595
}
596+
if (is_subclass_of($class, \BackedEnum::class) && $case = $class::tryFrom($value)) {
597+
return $case;
598+
}
599+
throw new InvalidTypeException("is $class and does not include '$value'.");
596600
})(),
597601
default => throw new InvalidTypeException(
598602
"expected types: $class, got " .

0 commit comments

Comments
 (0)