Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 055c0dc

Browse filesBrowse files
minor #47127 [Config] Add conditional types to conditional builders (wouterj)
This PR was merged into the 6.2 branch. Discussion ---------- [Config] Add conditional types to conditional builders | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | Fix #46546 | License | MIT | Doc PR | n/a Replaces #46581 , submitted to 6.2 instead. This matches with the changes proposed in #47016 and the config builder contained quite a big refactor for comments in 6.x so this avoids nasty conflicts. Original description: > One of the main benefits of using the new PHP config builders in application code is the ability to use PHP community tooling to autocomplete and validate the configuration. In this light, I think it makes sense to add the correct PHPdocs to make Psalm and PHPstan understand the config builders. > > On top of that, as these config classes are live generated, this is not something that can be easily stubbed in the special PHPstan and Psalm Symfony plugins. Commits ------- ce1087e [Config] Add conditional types to conditional builders
2 parents 02b8534 + ce1087e commit 055c0dc
Copy full SHA for 055c0dc

File tree

3 files changed

+22
-3
lines changed
Filter options

3 files changed

+22
-3
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Config/Builder/ConfigBuilderGenerator.php
+7-3Lines changed: 7 additions & 3 deletions
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Config/Tests/Builder/Fixtures/ScalarNormalizedTypes/Symfony/Config/ScalarNormalizedTypes/NestedConfig.php
+6Lines changed: 6 additions & 0 deletions
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Config/Tests/Builder/Fixtures/ScalarNormalizedTypes/Symfony/Config/ScalarNormalizedTypesConfig.php
+9Lines changed: 9 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.