Skip to content

Commit 2e524b4

Browse files
committed
ConfigBuilderGenerator: define template types
This helps phpstan to properly identify the return types, in our case for the `$framework->messenger()->transport('async')->dsn(env('MESSENGER_TRANSPORT_DSN'));` Where '->dns()` failed with the following Phpstan error: `Call to an undefined method Symfony\Config\Framework\Messenger\TransportConfig|Symfony\Config\Framework\MessengerConfig::dsn()`
1 parent b6d7264 commit 2e524b4

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
128128

129129
$hasNormalizationClosures = $this->hasNormalizationClosures($node);
130130
$comment = $this->getComment($node);
131+
$nodeTypes = $this->getParameterTypes($node);
132+
$paramTypes = \in_array('mixed', $nodeTypes, true) ? 'mixed' : implode('|', $nodeTypes);
133+
131134
if ($hasNormalizationClosures) {
132-
$comment = sprintf(" * @template TValue\n * @param TValue \$value\n%s", $comment);
135+
$comment = sprintf(" * @template TValue of {$paramTypes}\n * @param TValue \$value\n%s", $comment);
133136
$comment .= sprintf(' * @return %s|$this'."\n", $childClass->getFqcn());
134137
$comment .= sprintf(' * @psalm-return (TValue is array ? %s : static)'."\n ", $childClass->getFqcn());
135138
}
@@ -141,7 +144,7 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
141144
$node->getName(),
142145
$this->getType($childClass->getFqcn(), $hasNormalizationClosures)
143146
);
144-
$nodeTypes = $this->getParameterTypes($node);
147+
145148
$body = $hasNormalizationClosures ? '
146149
COMMENTpublic function NAME(PARAM_TYPE $value = []): CLASS|static
147150
{
@@ -177,7 +180,7 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
177180
'COMMENT' => $comment,
178181
'PROPERTY' => $property->getName(),
179182
'CLASS' => $childClass->getFqcn(),
180-
'PARAM_TYPE' => \in_array('mixed', $nodeTypes, true) ? 'mixed' : implode('|', $nodeTypes),
183+
'PARAM_TYPE' => $paramTypes,
181184
]);
182185

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

282285
$comment = $this->getComment($node);
286+
$resolvedParamTypes = null === $node->getKeyAttribute() ? $nodeParameterTypes : $prototypeParameterTypes;
287+
$paramTypes = \in_array('mixed', $resolvedParamTypes, true) ? 'mixed' : implode('|', $resolvedParamTypes);
288+
283289
if ($hasNormalizationClosures) {
284-
$comment = sprintf(" * @template TValue\n * @param TValue \$value\n%s", $comment);
290+
$comment = sprintf(" * @template TValue of {$paramTypes}\n * @param TValue \$value\n%s", $comment);
285291
$comment .= sprintf(' * @return %s|$this'."\n", $childClass->getFqcn());
286292
$comment .= sprintf(' * @psalm-return (TValue is array ? %s : static)'."\n ", $childClass->getFqcn());
287293
}
@@ -312,7 +318,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
312318
'COMMENT' => $comment,
313319
'PROPERTY' => $property->getName(),
314320
'CLASS' => $childClass->getFqcn(),
315-
'PARAM_TYPE' => \in_array('mixed', $nodeParameterTypes, true) ? 'mixed' : implode('|', $nodeParameterTypes),
321+
'PARAM_TYPE' => $paramTypes,
316322
]);
317323
} else {
318324
$body = $hasNormalizationClosures ? '
@@ -351,7 +357,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
351357
'CLASS' => $childClass->getFqcn(),
352358
'VAR' => '' === $key ? 'key' : $key,
353359
'VALUE' => 'value' === $key ? 'data' : 'value',
354-
'PARAM_TYPE' => \in_array('mixed', $prototypeParameterTypes, true) ? 'mixed' : implode('|', $prototypeParameterTypes),
360+
'PARAM_TYPE' => $paramTypes,
355361
]);
356362
}
357363

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
/**
1111
* This class is automatically generated to help in creating a config.
1212
*/
13-
class NestedConfig
13+
class NestedConfig
1414
{
1515
private $nestedObject;
1616
private $nestedListObject;
1717
private $_usedProperties = [];
1818

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

4545
/**
46-
* @template TValue
46+
* @template TValue of mixed
4747
* @param TValue $value
4848
* @return \Symfony\Config\ScalarNormalizedTypes\Nested\NestedListObjectConfig|$this
4949
* @psalm-return (TValue is array ? \Symfony\Config\ScalarNormalizedTypes\Nested\NestedListObjectConfig : static)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function keyedArray(string $name, ParamConfigurator|string|array $value):
4848
}
4949

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

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

9494
/**
95-
* @template TValue
95+
* @template TValue of mixed
9696
* @param TValue $value
9797
* @return \Symfony\Config\ScalarNormalizedTypes\KeyedListObjectConfig|$this
9898
* @psalm-return (TValue is array ? \Symfony\Config\ScalarNormalizedTypes\KeyedListObjectConfig : static)

0 commit comments

Comments
 (0)