Skip to content

Commit 93087a6

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents a091d65 + 5cfa80a commit 93087a6

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

src/Type/UnionType.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
use Throwable;
3636
use function array_diff_assoc;
3737
use function array_fill_keys;
38+
use function array_intersect;
39+
use function array_keys;
3840
use function array_map;
3941
use function array_merge;
4042
use function array_slice;
@@ -212,14 +214,35 @@ public function accepts(Type $type, bool $strictTypes): AcceptsResult
212214
break;
213215
}
214216

217+
$innerAccepts = [];
215218
$result = AcceptsResult::createNo();
216219
foreach ($this->getSortedTypes() as $i => $innerType) {
217-
$result = $result->or($innerType->accepts($type, $strictTypes)->decorateReasons(static fn (string $reason) => sprintf('Type #%d from the union: %s', $i + 1, $reason)));
220+
$innerResult = $innerType->accepts($type, $strictTypes);
221+
$innerAccepts[$i] = $innerResult;
222+
$result = $result->or($innerResult->decorateReasons(static fn (string $reason) => sprintf('Type #%d from the union: %s', $i + 1, $reason)));
218223
}
219224
if ($result->yes()) {
220225
return $result;
221226
}
222227

228+
$commonReasons = null;
229+
foreach ($innerAccepts as $innerResult) {
230+
if ($commonReasons === null) {
231+
$commonReasons = $innerResult->reasons;
232+
continue;
233+
}
234+
$commonReasons = array_values(array_intersect($commonReasons, $innerResult->reasons));
235+
}
236+
if ($commonReasons !== null && count($commonReasons) > 0) {
237+
$decorated = [];
238+
foreach (array_keys($innerAccepts) as $i) {
239+
foreach ($commonReasons as $reason) {
240+
$decorated[] = sprintf('Type #%d from the union: %s', $i + 1, $reason);
241+
}
242+
}
243+
$result = new AcceptsResult($result->result, $decorated);
244+
}
245+
223246
if ($type instanceof CompoundType && !$type instanceof CallableType && !$type instanceof TemplateType && !$type instanceof IntersectionType) {
224247
return $type->isAcceptedBy($this, $strictTypes);
225248
}

0 commit comments

Comments
 (0)