Skip to content

Commit 1073c30

Browse files
committed
parentArray hints with distinct children will now result in an intersect typehint instead of repeated @field hints
1 parent af7628d commit 1073c30

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/XmlFileParser.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ private function writeClassAndFieldHints(Frame $frame): string
289289
foreach ($frame->getKeyValues() as $key => $value) {
290290
$data .= '--- @field ' . $key . ' ' . $value[1] . ' # ' . $value[0] . "\n";
291291
}
292+
$allParentKeys = [];
293+
$allParentArrays = [];
292294
foreach ($frame->getChildren() as $child) {
293295
$typehint = $this->childHasInterestingData($child) ? $child->getClassName() : $child->getType();
294296
$parentKeys = [];
@@ -301,9 +303,7 @@ private function writeClassAndFieldHints(Frame $frame): string
301303
$parentKeys[$inherit->getParentKey()] = $inheritTypehint;
302304
}
303305
}
304-
foreach ($parentKeys as $parentKey => $typehint) {
305-
$data .= '--- @field ' . $parentKey . ' ' . $typehint . "\n";
306-
}
306+
$allParentKeys[] = $parentKeys;
307307

308308
$parentArrays = [];
309309
if ($child->getParentArray()) {
@@ -315,10 +315,28 @@ private function writeClassAndFieldHints(Frame $frame): string
315315
$parentArrays[$inherit->getParentArray()] ??= $inheritTypehint;
316316
}
317317
}
318-
foreach ($parentArrays as $parentArray => $typehint) {
319-
$data .= '--- @field ' . $parentArray . ' table<number, ' . $typehint . ">\n";
318+
$allParentArrays[] = $parentArrays;
319+
}
320+
321+
$mergedParentKeys = [];
322+
foreach ($allParentKeys as $parentKeys) {
323+
foreach ($parentKeys as $key => $type) {
324+
$mergedParentKeys[$key][] = $type;
325+
}
326+
}
327+
foreach ($mergedParentKeys as $key => $types) {
328+
$data .= '--- @field ' . $key . ' ' . implode(' | ', $types) . "\n";
329+
}
330+
331+
$mergedParentArrays = [];
332+
foreach ($allParentArrays as $parentArrays) {
333+
foreach ($parentArrays as $key => $type) {
334+
$mergedParentArrays[$key][] = $type;
320335
}
321336
}
337+
foreach ($mergedParentArrays as $key => $types) {
338+
$data .= '--- @field ' . $key . ' table<number, ' . implode(' | ', $types) . ">\n";
339+
}
322340

323341
return $data;
324342
}

0 commit comments

Comments
 (0)