Skip to content

Commit fbf15a8

Browse files
committed
[Config] Add conditional types to conditional builders
This allows static analysis tools to understand what type is returned.
1 parent 18aea63 commit fbf15a8

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
143143
);
144144
$body = $hasNormalizationClosures ? '
145145
/**
146+
* @template T
147+
* @psalm-param T $value
148+
* @psalm-return (T is array ? CLASS : static)
149+
*
146150
* @return CLASS|$this
147151
*/
148152
public function NAME($value = [])
@@ -262,6 +266,10 @@ public function NAME(string $VAR, $VALUE): self
262266
if (null === $key = $node->getKeyAttribute()) {
263267
$body = $hasNormalizationClosures ? '
264268
/**
269+
* @template T
270+
* @psalm-param T $value
271+
* @psalm-return (T is array ? CLASS : static)
272+
*
265273
* @return CLASS|$this
266274
*/
267275
public function NAME($value = [])
@@ -285,7 +293,11 @@ public function NAME(array $value = []): CLASS
285293
} else {
286294
$body = $hasNormalizationClosures ? '
287295
/**
296+
* @template T
297+
* @psalm-param T $value
298+
*
288299
* @return CLASS|$this
300+
* @psalm-return (T is array ? CLASS : static)
289301
*/
290302
public function NAME(string $VAR, $VALUE = [])
291303
{

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class NestedConfig
1717
private $_usedProperties = [];
1818

1919
/**
20+
* @template T
21+
* @psalm-param T $value
22+
* @psalm-return (T is array ? \Symfony\Config\ScalarNormalizedTypes\Nested\NestedObjectConfig : static)
23+
*
2024
* @return \Symfony\Config\ScalarNormalizedTypes\Nested\NestedObjectConfig|$this
2125
*/
2226
public function nestedObject($value = [])
@@ -39,6 +43,10 @@ public function nestedObject($value = [])
3943
}
4044

4145
/**
46+
* @template T
47+
* @psalm-param T $value
48+
* @psalm-return (T is array ? \Symfony\Config\ScalarNormalizedTypes\Nested\NestedListObjectConfig : static)
49+
*
4250
* @return \Symfony\Config\ScalarNormalizedTypes\Nested\NestedListObjectConfig|$this
4351
*/
4452
public function nestedListObject($value = [])

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function keyedArray(string $name, $value): self
4848
}
4949

5050
/**
51+
* @template T
52+
* @psalm-param T $value
53+
* @psalm-return (T is array ? \Symfony\Config\ScalarNormalizedTypes\ObjectConfig : static)
54+
*
5155
* @return \Symfony\Config\ScalarNormalizedTypes\ObjectConfig|$this
5256
*/
5357
public function object($value = [])
@@ -70,6 +74,10 @@ public function object($value = [])
7074
}
7175

7276
/**
77+
* @template T
78+
* @psalm-param T $value
79+
* @psalm-return (T is array ? \Symfony\Config\ScalarNormalizedTypes\ListObjectConfig : static)
80+
*
7381
* @return \Symfony\Config\ScalarNormalizedTypes\ListObjectConfig|$this
7482
*/
7583
public function listObject($value = [])
@@ -85,6 +93,10 @@ public function listObject($value = [])
8593
}
8694

8795
/**
96+
* @template T
97+
* @psalm-param T $value
98+
* @psalm-return (T is array ? \Symfony\Config\ScalarNormalizedTypes\KeyedListObjectConfig : static)
99+
*
88100
* @return \Symfony\Config\ScalarNormalizedTypes\KeyedListObjectConfig|$this
89101
*/
90102
public function keyedListObject(string $class, $value = [])

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy