Skip to content

Commit 13162c9

Browse files
authored
Merge pull request #39 from wiz-develop:endou-mame/issue38
fix: 引数の型を修正し、エラーハンドリングを強化
2 parents 3bb020d + 0ddc5a5 commit 13162c9

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/Collection/ArrayList.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ArrayAccess;
88
use Closure;
99
use Generator;
10+
use LogicException;
1011
use Override;
1112
use WizDevelop\PhpMonad\Option;
1213
use WizDevelop\PhpMonad\Result;
@@ -109,7 +110,7 @@ final public static function tryFrom(iterable $elements): Result
109110
*
110111
* @template TTryFromValue of TValue
111112
*
112-
* @param iterable<int,(Result<TTryFromValue,IErrorValue>|Result)> $results
113+
* @param iterable<int,(Result<TTryFromValue,IErrorValue>|Result<TTryFromValue,IErrorValue[]>|Result)> $results
113114
* @return Result<static<TTryFromValue>,ValueObjectError>
114115
*/
115116
/**
@@ -122,9 +123,30 @@ final public static function tryFromResults(iterable $results): Result
122123

123124
$elementsResult = Result\combineWithErrorValue(...$elements);
124125
if ($elementsResult->isErr()) {
126+
$flattenErrs = [];
127+
foreach ($elementsResult->unwrapErr() as $err) {
128+
if ($err instanceof IErrorValue) { // @phpstan-ignore-line
129+
$flattenErrs[] = $err;
130+
} elseif (is_array($err)) {
131+
foreach ($err as $e) {
132+
if ($e instanceof IErrorValue) { // @phpstan-ignore-line
133+
$flattenErrs[] = $e;
134+
} else {
135+
throw new LogicException(
136+
'Invalid error value type in array. Expected IErrorValue.',
137+
);
138+
}
139+
}
140+
} else {
141+
throw new LogicException(
142+
'Invalid error value type. Expected IErrorValue or array of IErrorValue.',
143+
);
144+
}
145+
}
146+
125147
return Result\err(ValueObjectError::collection()->invalidElementValues(
126148
static::class,
127-
...$elementsResult->unwrapErr()
149+
...$flattenErrs,
128150
));
129151
}
130152

0 commit comments

Comments
 (0)