Skip to content

Commit 9d38089

Browse files
committed
[Config] Fix PHP Configuration type hints & tests
1 parent c7f15d6 commit 9d38089

File tree

8 files changed

+50
-30
lines changed

8 files changed

+50
-30
lines changed

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
145145
/**
146146
* @return CLASS|$this
147147
*/
148-
public function NAME($value = [])
148+
public function NAME(mixed $value = []): CLASS|static
149149
{
150150
if (!\is_array($value)) {
151151
$this->_usedProperties[\'PROPERTY\'] = true;
@@ -198,14 +198,19 @@ public function NAME(mixed $valueDEFAULT): static
198198
199199
return $this;
200200
}';
201-
$class->addMethod($node->getName(), $body, ['PROPERTY' => $property->getName(), 'COMMENT' => $comment, 'DEFAULT' => $node->hasDefaultValue() ? ' = '.var_export($node->getDefaultValue(), true) : '']);
201+
$class->addMethod($node->getName(), $body, [
202+
'PROPERTY' => $property->getName(),
203+
'COMMENT' => $comment,
204+
'DEFAULT' => $node->hasDefaultValue() ? ' = '.var_export($node->getDefaultValue(), true) : '',
205+
]);
202206
}
203207

204208
private function handlePrototypedArrayNode(PrototypedArrayNode $node, ClassBuilder $class, string $namespace): void
205209
{
206210
$name = $this->getSingularName($node);
207211
$prototype = $node->getPrototype();
208212
$methodName = $name;
213+
$hasNormalizationClosures = $this->hasNormalizationClosures($node) || $this->hasNormalizationClosures($prototype);
209214

210215
$parameterType = $this->getParameterType($prototype);
211216
if (null !== $parameterType || $prototype instanceof ScalarNode) {
@@ -215,19 +220,23 @@ private function handlePrototypedArrayNode(PrototypedArrayNode $node, ClassBuild
215220
// This is an array of values; don't use singular name
216221
$body = '
217222
/**
218-
* @param ParamConfigurator|list<ParamConfigurator|TYPE> $value
223+
* @param PHPDOC_TYPE $value
219224
*
220225
* @return $this
221226
*/
222-
public function NAME(ParamConfigurator|array $value): static
227+
public function NAME(TYPE $value): static
223228
{
224229
$this->_usedProperties[\'PROPERTY\'] = true;
225230
$this->PROPERTY = $value;
226231
227232
return $this;
228233
}';
229234

230-
$class->addMethod($node->getName(), $body, ['PROPERTY' => $property->getName(), 'TYPE' => '' === $parameterType ? 'mixed' : $parameterType]);
235+
$class->addMethod($node->getName(), $body, [
236+
'PROPERTY' => $property->getName(),
237+
'TYPE' => $hasNormalizationClosures ? 'mixed' : 'ParamConfigurator|array',
238+
'PHPDOC_TYPE' => $hasNormalizationClosures ? 'mixed' : sprintf('ParamConfigurator|list<ParamConfigurator|%s>', '' === $parameterType ? 'mixed' : $parameterType),
239+
]);
231240
} else {
232241
$body = '
233242
/**
@@ -241,7 +250,12 @@ public function NAME(string $VAR, TYPE $VALUE): static
241250
return $this;
242251
}';
243252

244-
$class->addMethod($methodName, $body, ['PROPERTY' => $property->getName(), 'TYPE' => '' === $parameterType ? 'mixed' : 'ParamConfigurator|'.$parameterType, 'VAR' => '' === $key ? 'key' : $key, 'VALUE' => 'value' === $key ? 'data' : 'value']);
253+
$class->addMethod($methodName, $body, [
254+
'PROPERTY' => $property->getName(),
255+
'TYPE' => $hasNormalizationClosures || '' === $parameterType ? 'mixed' : 'ParamConfigurator|'.$parameterType,
256+
'VAR' => '' === $key ? 'key' : $key,
257+
'VALUE' => 'value' === $key ? 'data' : 'value',
258+
]);
245259
}
246260

247261
return;
@@ -254,7 +268,6 @@ public function NAME(string $VAR, TYPE $VALUE): static
254268
$class->addRequire($childClass);
255269
$this->classes[] = $childClass;
256270

257-
$hasNormalizationClosures = $this->hasNormalizationClosures($node) || $this->hasNormalizationClosures($prototype);
258271
$property = $class->addProperty(
259272
$node->getName(),
260273
$this->getType($childClass->getFqcn().'[]', $hasNormalizationClosures)
@@ -265,7 +278,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
265278
/**
266279
* @return CLASS|$this
267280
*/
268-
public function NAME($value = [])
281+
public function NAME(mixed $value = []): CLASS|static
269282
{
270283
$this->_usedProperties[\'PROPERTY\'] = true;
271284
if (!\is_array($value)) {
@@ -288,7 +301,7 @@ public function NAME(array $value = []): CLASS
288301
/**
289302
* @return CLASS|$this
290303
*/
291-
public function NAME(string $VAR, $VALUE = [])
304+
public function NAME(string $VAR, mixed $VALUE = []): CLASS|static
292305
{
293306
if (!\is_array($VALUE)) {
294307
$this->_usedProperties[\'PROPERTY\'] = true;
@@ -318,7 +331,12 @@ public function NAME(string $VAR, array $VALUE = []): CLASS
318331
return $this->PROPERTY[$VAR];
319332
}';
320333
$class->addUse(InvalidConfigurationException::class);
321-
$class->addMethod($methodName, $body, ['PROPERTY' => $property->getName(), 'CLASS' => $childClass->getFqcn(), 'VAR' => '' === $key ? 'key' : $key, 'VALUE' => 'value' === $key ? 'data' : 'value']);
334+
$class->addMethod($methodName, $body, [
335+
'PROPERTY' => $property->getName(),
336+
'CLASS' => $childClass->getFqcn(),
337+
'VAR' => '' === $key ? 'key' : $key,
338+
'VALUE' => 'value' === $key ? 'data' : 'value',
339+
]);
322340
}
323341

324342
$this->buildNode($prototype, $childClass, $namespace.'\\'.$childClass->getName());

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class KeyedListObjectConfig
1919
* @param ParamConfigurator|bool $value
2020
* @return $this
2121
*/
22-
public function enabled($value): self
22+
public function enabled($value): static
2323
{
2424
$this->_usedProperties['enabled'] = true;
2525
$this->enabled = $value;
@@ -28,10 +28,11 @@ public function enabled($value): self
2828
}
2929

3030
/**
31-
* @param ParamConfigurator|list<mixed|ParamConfigurator> $value
31+
* @param ParamConfigurator|list<ParamConfigurator|mixed> $value
32+
*
3233
* @return $this
3334
*/
34-
public function settings($value): self
35+
public function settings(ParamConfigurator|array $value): static
3536
{
3637
$this->_usedProperties['settings'] = true;
3738
$this->settings = $value;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ListObjectConfig
1919
* @param ParamConfigurator|mixed $value
2020
* @return $this
2121
*/
22-
public function name($value): self
22+
public function name($value): static
2323
{
2424
$this->_usedProperties['name'] = true;
2525
$this->name = $value;
@@ -28,10 +28,11 @@ public function name($value): self
2828
}
2929

3030
/**
31-
* @param ParamConfigurator|list<mixed|ParamConfigurator> $value
31+
* @param ParamConfigurator|list<ParamConfigurator|mixed> $value
32+
*
3233
* @return $this
3334
*/
34-
public function data($value): self
35+
public function data(ParamConfigurator|array $value): static
3536
{
3637
$this->_usedProperties['data'] = true;
3738
$this->data = $value;

src/Symfony/Component/Config/Tests/Builder/Fixtures/ScalarNormalizedTypes/Symfony/Config/ScalarNormalizedTypes/Nested/NestedListObjectConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class NestedListObjectConfig
1818
* @param ParamConfigurator|mixed $value
1919
* @return $this
2020
*/
21-
public function name($value): self
21+
public function name($value): static
2222
{
2323
$this->_usedProperties['name'] = true;
2424
$this->name = $value;

src/Symfony/Component/Config/Tests/Builder/Fixtures/ScalarNormalizedTypes/Symfony/Config/ScalarNormalizedTypes/Nested/NestedObjectConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class NestedObjectConfig
1818
* @param ParamConfigurator|bool $value
1919
* @return $this
2020
*/
21-
public function enabled($value): self
21+
public function enabled($value): static
2222
{
2323
$this->_usedProperties['enabled'] = true;
2424
$this->enabled = $value;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class NestedConfig
1919
/**
2020
* @return \Symfony\Config\ScalarNormalizedTypes\Nested\NestedObjectConfig|$this
2121
*/
22-
public function nestedObject($value = [])
22+
public function nestedObject(mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\Nested\NestedObjectConfig|static
2323
{
2424
if (!\is_array($value)) {
2525
$this->_usedProperties['nestedObject'] = true;
@@ -41,7 +41,7 @@ public function nestedObject($value = [])
4141
/**
4242
* @return \Symfony\Config\ScalarNormalizedTypes\Nested\NestedListObjectConfig|$this
4343
*/
44-
public function nestedListObject($value = [])
44+
public function nestedListObject(mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\Nested\NestedListObjectConfig|static
4545
{
4646
$this->_usedProperties['nestedListObject'] = true;
4747
if (!\is_array($value)) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ObjectConfig
2020
* @param ParamConfigurator|bool $value
2121
* @return $this
2222
*/
23-
public function enabled($value): self
23+
public function enabled($value): static
2424
{
2525
$this->_usedProperties['enabled'] = true;
2626
$this->enabled = $value;
@@ -33,7 +33,7 @@ public function enabled($value): self
3333
* @param ParamConfigurator|mixed $value
3434
* @return $this
3535
*/
36-
public function dateFormat($value): self
36+
public function dateFormat($value): static
3737
{
3838
$this->_usedProperties['dateFormat'] = true;
3939
$this->dateFormat = $value;
@@ -46,7 +46,7 @@ public function dateFormat($value): self
4646
* @param ParamConfigurator|bool $value
4747
* @return $this
4848
*/
49-
public function removeUsedContextFields($value): self
49+
public function removeUsedContextFields($value): static
5050
{
5151
$this->_usedProperties['removeUsedContextFields'] = true;
5252
$this->removeUsedContextFields = $value;

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ class ScalarNormalizedTypesConfig implements \Symfony\Component\Config\Builder\C
2424
private $_usedProperties = [];
2525

2626
/**
27-
* @param ParamConfigurator|list<mixed|ParamConfigurator> $value
27+
* @param mixed $value
28+
*
2829
* @return $this
2930
*/
30-
public function simpleArray($value): self
31+
public function simpleArray(mixed $value): static
3132
{
3233
$this->_usedProperties['simpleArray'] = true;
3334
$this->simpleArray = $value;
@@ -36,10 +37,9 @@ public function simpleArray($value): self
3637
}
3738

3839
/**
39-
* @param ParamConfigurator|array $value
4040
* @return $this
4141
*/
42-
public function keyedArray(string $name, $value): self
42+
public function keyedArray(string $name, mixed $value): static
4343
{
4444
$this->_usedProperties['keyedArray'] = true;
4545
$this->keyedArray[$name] = $value;
@@ -50,7 +50,7 @@ public function keyedArray(string $name, $value): self
5050
/**
5151
* @return \Symfony\Config\ScalarNormalizedTypes\ObjectConfig|$this
5252
*/
53-
public function object($value = [])
53+
public function object(mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\ObjectConfig|static
5454
{
5555
if (!\is_array($value)) {
5656
$this->_usedProperties['object'] = true;
@@ -72,7 +72,7 @@ public function object($value = [])
7272
/**
7373
* @return \Symfony\Config\ScalarNormalizedTypes\ListObjectConfig|$this
7474
*/
75-
public function listObject($value = [])
75+
public function listObject(mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\ListObjectConfig|static
7676
{
7777
$this->_usedProperties['listObject'] = true;
7878
if (!\is_array($value)) {
@@ -87,7 +87,7 @@ public function listObject($value = [])
8787
/**
8888
* @return \Symfony\Config\ScalarNormalizedTypes\KeyedListObjectConfig|$this
8989
*/
90-
public function keyedListObject(string $class, $value = [])
90+
public function keyedListObject(string $class, mixed $value = []): \Symfony\Config\ScalarNormalizedTypes\KeyedListObjectConfig|static
9191
{
9292
if (!\is_array($value)) {
9393
$this->_usedProperties['keyedListObject'] = true;

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