Skip to content

[Config] ConfigBuilderGenerator: define template types #54008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/Symfony/Component/Config/Builder/ConfigBuilderGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,11 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n

$hasNormalizationClosures = $this->hasNormalizationClosures($node);
$comment = $this->getComment($node);
$nodeTypes = $this->getParameterTypes($node);
$paramTypes = \in_array('mixed', $nodeTypes, true) ? 'mixed' : implode('|', $nodeTypes);

if ($hasNormalizationClosures) {
$comment = sprintf(" * @template TValue\n * @param TValue \$value\n%s", $comment);
$comment = sprintf(" * @template TValue of %s\n * @param TValue \$value\n%s", $paramTypes, $comment);
$comment .= sprintf(' * @return %s|$this'."\n", $childClass->getFqcn());
$comment .= sprintf(' * @psalm-return (TValue is array ? %s : static)'."\n ", $childClass->getFqcn());
}
Expand All @@ -141,7 +144,7 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
$node->getName(),
$this->getType($childClass->getFqcn(), $hasNormalizationClosures)
);
$nodeTypes = $this->getParameterTypes($node);

$body = $hasNormalizationClosures ? '
COMMENTpublic function NAME(PARAM_TYPE $value = []): CLASS|static
{
Expand Down Expand Up @@ -177,7 +180,7 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
'COMMENT' => $comment,
'PROPERTY' => $property->getName(),
'CLASS' => $childClass->getFqcn(),
'PARAM_TYPE' => \in_array('mixed', $nodeTypes, true) ? 'mixed' : implode('|', $nodeTypes),
'PARAM_TYPE' => $paramTypes,
]);

$this->buildNode($node, $childClass, $this->getSubNamespace($childClass));
Expand Down Expand Up @@ -280,8 +283,11 @@ public function NAME(string $VAR, TYPE $VALUE): static
);

$comment = $this->getComment($node);
$resolvedParamTypes = null === $node->getKeyAttribute() ? $nodeParameterTypes : $prototypeParameterTypes;
$paramTypes = \in_array('mixed', $resolvedParamTypes, true) ? 'mixed' : implode('|', $resolvedParamTypes);

if ($hasNormalizationClosures) {
$comment = sprintf(" * @template TValue\n * @param TValue \$value\n%s", $comment);
$comment = sprintf(" * @template TValue of %s\n * @param TValue \$value\n%s", $paramTypes, $comment);
$comment .= sprintf(' * @return %s|$this'."\n", $childClass->getFqcn());
$comment .= sprintf(' * @psalm-return (TValue is array ? %s : static)'."\n ", $childClass->getFqcn());
}
Expand Down Expand Up @@ -312,7 +318,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
'COMMENT' => $comment,
'PROPERTY' => $property->getName(),
'CLASS' => $childClass->getFqcn(),
'PARAM_TYPE' => \in_array('mixed', $nodeParameterTypes, true) ? 'mixed' : implode('|', $nodeParameterTypes),
'PARAM_TYPE' => $paramTypes,
]);
} else {
$body = $hasNormalizationClosures ? '
Expand Down Expand Up @@ -351,7 +357,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
'CLASS' => $childClass->getFqcn(),
'VAR' => '' === $key ? 'key' : $key,
'VALUE' => 'value' === $key ? 'data' : 'value',
'PARAM_TYPE' => \in_array('mixed', $prototypeParameterTypes, true) ? 'mixed' : implode('|', $prototypeParameterTypes),
'PARAM_TYPE' => $paramTypes,
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class NestedConfig
private $_usedProperties = [];

/**
* @template TValue
* @template TValue of mixed
* @param TValue $value
* @default {"enabled":null}
* @return \Symfony\Config\ScalarNormalizedTypes\Nested\NestedObjectConfig|$this
Expand All @@ -43,7 +43,7 @@ public function nestedObject(mixed $value = []): \Symfony\Config\ScalarNormalize
}

/**
* @template TValue
* @template TValue of mixed
* @param TValue $value
* @return \Symfony\Config\ScalarNormalizedTypes\Nested\NestedListObjectConfig|$this
* @psalm-return (TValue is array ? \Symfony\Config\ScalarNormalizedTypes\Nested\NestedListObjectConfig : static)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function keyedArray(string $name, ParamConfigurator|string|array $value):
}

/**
* @template TValue
* @template TValue of mixed
* @param TValue $value
* @default {"enabled":null}
* @return \Symfony\Config\ScalarNormalizedTypes\ObjectConfig|$this
Expand All @@ -74,7 +74,7 @@ public function object(mixed $value = []): \Symfony\Config\ScalarNormalizedTypes
}

/**
* @template TValue
* @template TValue of mixed
* @param TValue $value
* @return \Symfony\Config\ScalarNormalizedTypes\ListObjectConfig|$this
* @psalm-return (TValue is array ? \Symfony\Config\ScalarNormalizedTypes\ListObjectConfig : static)
Expand All @@ -92,7 +92,7 @@ public function listObject(mixed $value = []): \Symfony\Config\ScalarNormalizedT
}

/**
* @template TValue
* @template TValue of mixed
* @param TValue $value
* @return \Symfony\Config\ScalarNormalizedTypes\KeyedListObjectConfig|$this
* @psalm-return (TValue is array ? \Symfony\Config\ScalarNormalizedTypes\KeyedListObjectConfig : static)
Expand Down