Skip to content

Commit ce1087e

Browse files
committed
[Config] Add conditional types to conditional builders
1 parent 9d7ff0c commit ce1087e

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/Symfony/Component/Config/Builder/ConfigBuilderGenerator.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
138138
$hasNormalizationClosures = $this->hasNormalizationClosures($node);
139139
$comment = $this->getComment($node);
140140
if ($hasNormalizationClosures) {
141-
$comment .= sprintf(' * @return %s|$this'."\n ", $childClass->getFqcn());
141+
$comment = sprintf(" * @template TValue\n * @param TValue \$value\n%s", $comment);
142+
$comment .= sprintf(' * @return %s|$this'."\n", $childClass->getFqcn());
143+
$comment .= sprintf(' * @psalm-return (TValue is array ? %s : static)'."\n ", $childClass->getFqcn());
142144
}
143145
if ('' !== $comment) {
144146
$comment = "/**\n$comment*/\n";
@@ -279,7 +281,9 @@ public function NAME(string $VAR, TYPE $VALUE): static
279281

280282
$comment = $this->getComment($node);
281283
if ($hasNormalizationClosures) {
282-
$comment .= sprintf(' * @return %s|$this'."\n ", $childClass->getFqcn());
284+
$comment = sprintf(" * @template TValue\n * @param TValue \$value\n%s", $comment);
285+
$comment .= sprintf(' * @return %s|$this'."\n", $childClass->getFqcn());
286+
$comment .= sprintf(' * @psalm-return (TValue is array ? %s : static)'."\n ", $childClass->getFqcn());
283287
}
284288
if ('' !== $comment) {
285289
$comment = "/**\n$comment*/\n";
@@ -337,7 +341,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
337341
return $this->PROPERTY[$VAR];
338342
}';
339343
$class->addUse(InvalidConfigurationException::class);
340-
$class->addMethod($methodName, $body, [
344+
$class->addMethod($methodName, str_replace('$value', '$VAR', $body), [
341345
'COMMENT' => $comment, 'PROPERTY' => $property->getName(),
342346
'CLASS' => $childClass->getFqcn(),
343347
'VAR' => '' === $key ? 'key' : $key,

src/Symfony/Component/Config/Tests/Builder/Fixtures/ScalarNormalizedTypes/Symfony/Config/ScalarNormalizedTypes/NestedConfig.php

+6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ class NestedConfig
1717
private $_usedProperties = [];
1818

1919
/**
20+
* @template TValue
21+
* @param TValue $value
2022
* @default {"enabled":null}
2123
* @return \Symfony\Config\ScalarNormalizedTypes\Nested\NestedObjectConfig|$this
24+
* @psalm-return (TValue is array ? \Symfony\Config\ScalarNormalizedTypes\Nested\NestedObjectConfig : static)
2225
*/
2326
public function nestedObject(mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\Nested\NestedObjectConfig|static
2427
{
@@ -40,7 +43,10 @@ public function nestedObject(mixed $value = []): \Symfony\Config\ScalarNormalize
4043
}
4144

4245
/**
46+
* @template TValue
47+
* @param TValue $value
4348
* @return \Symfony\Config\ScalarNormalizedTypes\Nested\NestedListObjectConfig|$this
49+
* @psalm-return (TValue is array ? \Symfony\Config\ScalarNormalizedTypes\Nested\NestedListObjectConfig : static)
4450
*/
4551
public function nestedListObject(mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\Nested\NestedListObjectConfig|static
4652
{

src/Symfony/Component/Config/Tests/Builder/Fixtures/ScalarNormalizedTypes/Symfony/Config/ScalarNormalizedTypesConfig.php

+9
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ public function keyedArray(string $name, mixed $value): static
4848
}
4949

5050
/**
51+
* @template TValue
52+
* @param TValue $value
5153
* @default {"enabled":null}
5254
* @return \Symfony\Config\ScalarNormalizedTypes\ObjectConfig|$this
55+
* @psalm-return (TValue is array ? \Symfony\Config\ScalarNormalizedTypes\ObjectConfig : static)
5356
*/
5457
public function object(mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\ObjectConfig|static
5558
{
@@ -71,7 +74,10 @@ public function object(mixed $value = []): \Symfony\Config\ScalarNormalizedTypes
7174
}
7275

7376
/**
77+
* @template TValue
78+
* @param TValue $value
7479
* @return \Symfony\Config\ScalarNormalizedTypes\ListObjectConfig|$this
80+
* @psalm-return (TValue is array ? \Symfony\Config\ScalarNormalizedTypes\ListObjectConfig : static)
7581
*/
7682
public function listObject(mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\ListObjectConfig|static
7783
{
@@ -86,7 +92,10 @@ public function listObject(mixed $value = []): \Symfony\Config\ScalarNormalizedT
8692
}
8793

8894
/**
95+
* @template TValue
96+
* @param TValue $value
8997
* @return \Symfony\Config\ScalarNormalizedTypes\KeyedListObjectConfig|$this
98+
* @psalm-return (TValue is array ? \Symfony\Config\ScalarNormalizedTypes\KeyedListObjectConfig : static)
9099
*/
91100
public function keyedListObject(string $class, mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\KeyedListObjectConfig|static
92101
{

0 commit comments

Comments
 (0)