1
0
Fork 0

Specify optional array keys in `ConfigReturnTypeExtension`

pull/10812/head
Martin Herndl 2022-06-02 12:55:21 +02:00 committed by Jordi Boggiano
parent 509c0fffbe
commit 3b204cb6f2
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
1 changed files with 5 additions and 1 deletions

View File

@ -123,19 +123,23 @@ final class ConfigReturnTypeExtension implements DynamicMethodReturnTypeExtensio
if (isset($def['properties'])) {
$keyNames = [];
$valTypes = [];
$optionalKeys = [];
$propIndex = 0;
foreach ($def['properties'] as $propName => $propdef) {
$keyNames[] = new ConstantStringType($propName);
$valType = $this->parseType($propdef, $path.'.'.$propName);
if (!isset($def['required']) || !in_array($propName, $def['required'], true)) {
$valType = TypeCombinator::addNull($valType);
$optionalKeys[] = $propIndex;
}
$valTypes[] = $valType;
$propIndex++;
}
if ($addlPropType !== null) {
$types[] = new ArrayType(TypeCombinator::union(new StringType(), ...$keyNames), TypeCombinator::union($addlPropType, ...$valTypes));
} else {
$types[] = new ConstantArrayType($keyNames, $valTypes);
$types[] = new ConstantArrayType($keyNames, $valTypes, [0], $optionalKeys);
}
} else {
$types[] = new ArrayType(new StringType(), $addlPropType ?? new MixedType());