Skip to content

Commit dc21d70

Browse files
committed
Merge branch '7.2' into 7.3
* 7.2: [Serializer] Handle invalid mapping type property type [Config] Do not generate unreachable configuration paths [WebProfilerBundle] Fix missing indent on non php files opended in the profiler
2 parents a2a17a5 + a2e1b3c commit dc21d70

File tree

10 files changed

+344
-18
lines changed

10 files changed

+344
-18
lines changed

Builder/ConfigBuilderGenerator.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,13 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
126126
$class->addRequire($childClass);
127127
$this->classes[] = $childClass;
128128

129+
$nodeTypes = $this->getParameterTypes($node);
130+
$paramType = $this->getParamType($nodeTypes);
131+
129132
$hasNormalizationClosures = $this->hasNormalizationClosures($node);
130133
$comment = $this->getComment($node);
131-
if ($hasNormalizationClosures) {
132-
$comment = \sprintf(" * @template TValue\n * @param TValue \$value\n%s", $comment);
134+
if ($hasNormalizationClosures && 'array' !== $paramType) {
135+
$comment = \sprintf(" * @template TValue of %s\n * @param TValue \$value\n%s", $paramType, $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,8 +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);
145-
$body = $hasNormalizationClosures ? '
147+
$body = $hasNormalizationClosures && 'array' !== $paramType ? '
146148
COMMENTpublic function NAME(PARAM_TYPE $value = []): CLASS|static
147149
{
148150
if (!\is_array($value)) {
@@ -177,7 +179,7 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
177179
'COMMENT' => $comment,
178180
'PROPERTY' => $property->getName(),
179181
'CLASS' => $childClass->getFqcn(),
180-
'PARAM_TYPE' => \in_array('mixed', $nodeTypes, true) ? 'mixed' : implode('|', $nodeTypes),
182+
'PARAM_TYPE' => $paramType,
181183
]);
182184

183185
$this->buildNode($node, $childClass, $this->getSubNamespace($childClass));
@@ -217,10 +219,11 @@ private function handlePrototypedArrayNode(PrototypedArrayNode $node, ClassBuild
217219

218220
$nodeParameterTypes = $this->getParameterTypes($node);
219221
$prototypeParameterTypes = $this->getParameterTypes($prototype);
222+
$noKey = null === $key = $node->getKeyAttribute();
220223
if (!$prototype instanceof ArrayNode || ($prototype instanceof PrototypedArrayNode && $prototype->getPrototype() instanceof ScalarNode)) {
221224
$class->addUse(ParamConfigurator::class);
222225
$property = $class->addProperty($node->getName());
223-
if (null === $key = $node->getKeyAttribute()) {
226+
if ($noKey) {
224227
// This is an array of values; don't use singular name
225228
$nodeTypesWithoutArray = array_filter($nodeParameterTypes, static fn ($type) => 'array' !== $type);
226229
$body = '
@@ -241,7 +244,7 @@ public function NAME(PARAM_TYPE $value): static
241244
'PROPERTY' => $property->getName(),
242245
'PROTOTYPE_TYPE' => implode('|', $prototypeParameterTypes),
243246
'EXTRA_TYPE' => $nodeTypesWithoutArray ? '|'.implode('|', $nodeTypesWithoutArray) : '',
244-
'PARAM_TYPE' => \in_array('mixed', $nodeParameterTypes, true) ? 'mixed' : 'ParamConfigurator|'.implode('|', $nodeParameterTypes),
247+
'PARAM_TYPE' => $this->getParamType($nodeParameterTypes, true),
245248
]);
246249
} else {
247250
$body = '
@@ -258,7 +261,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
258261

259262
$class->addMethod($methodName, $body, [
260263
'PROPERTY' => $property->getName(),
261-
'TYPE' => \in_array('mixed', $prototypeParameterTypes, true) ? 'mixed' : 'ParamConfigurator|'.implode('|', $prototypeParameterTypes),
264+
'TYPE' => $this->getParamType($prototypeParameterTypes, true),
262265
'VAR' => '' === $key ? 'key' : $key,
263266
'VALUE' => 'value' === $key ? 'data' : 'value',
264267
]);
@@ -279,18 +282,27 @@ public function NAME(string $VAR, TYPE $VALUE): static
279282
$this->getType($childClass->getFqcn().'[]', $hasNormalizationClosures)
280283
);
281284

285+
$paramType = $this->getParamType($noKey ? $nodeParameterTypes : $prototypeParameterTypes);
286+
282287
$comment = $this->getComment($node);
288+
<<<<<<< HEAD
283289
if ($hasNormalizationClosures) {
284290
$comment = \sprintf(" * @template TValue\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());
293+
=======
294+
if ($hasNormalizationClosures && 'array' !== $paramType) {
295+
$comment = sprintf(" * @template TValue of %s\n * @param TValue \$value\n%s", $paramType, $comment);
296+
$comment .= sprintf(' * @return %s|$this'."\n", $childClass->getFqcn());
297+
$comment .= sprintf(' * @psalm-return (TValue is array ? %s : static)'."\n ", $childClass->getFqcn());
298+
>>>>>>> 100c683018d ([Config] Do not generate unreachable configuration paths)
287299
}
288300
if ('' !== $comment) {
289301
$comment = "/**\n$comment*/\n";
290302
}
291303

292-
if (null === $key = $node->getKeyAttribute()) {
293-
$body = $hasNormalizationClosures ? '
304+
if ($noKey) {
305+
$body = $hasNormalizationClosures && 'array' !== $paramType ? '
294306
COMMENTpublic function NAME(PARAM_TYPE $value = []): CLASS|static
295307
{
296308
$this->_usedProperties[\'PROPERTY\'] = true;
@@ -312,10 +324,10 @@ public function NAME(string $VAR, TYPE $VALUE): static
312324
'COMMENT' => $comment,
313325
'PROPERTY' => $property->getName(),
314326
'CLASS' => $childClass->getFqcn(),
315-
'PARAM_TYPE' => \in_array('mixed', $nodeParameterTypes, true) ? 'mixed' : implode('|', $nodeParameterTypes),
327+
'PARAM_TYPE' => $paramType,
316328
]);
317329
} else {
318-
$body = $hasNormalizationClosures ? '
330+
$body = $hasNormalizationClosures && 'array' !== $paramType ? '
319331
COMMENTpublic function NAME(string $VAR, PARAM_TYPE $VALUE = []): CLASS|static
320332
{
321333
if (!\is_array($VALUE)) {
@@ -351,7 +363,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
351363
'CLASS' => $childClass->getFqcn(),
352364
'VAR' => '' === $key ? 'key' : $key,
353365
'VALUE' => 'value' === $key ? 'data' : 'value',
354-
'PARAM_TYPE' => \in_array('mixed', $prototypeParameterTypes, true) ? 'mixed' : implode('|', $prototypeParameterTypes),
366+
'PARAM_TYPE' => $paramType,
355367
]);
356368
}
357369

@@ -596,4 +608,9 @@ private function getType(string $classType, bool $hasNormalizationClosures): str
596608
{
597609
return $classType.($hasNormalizationClosures ? '|scalar' : '');
598610
}
611+
612+
private function getParamType(array $types, bool $withParamConfigurator = false): string
613+
{
614+
return \in_array('mixed', $types, true) ? 'mixed' : ($withParamConfigurator ? 'ParamConfigurator|' : '').implode('|', $types);
615+
}
599616
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Config\ArrayValuesConfig;
13+
14+
return static function (ArrayValuesConfig $config) {
15+
$config->transports('foo')->dsn('bar');
16+
$config->transports('bar', ['dsn' => 'foobar']);
17+
18+
$config->errorPages()->withTrace(false);
19+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
return [
13+
'transports' => [
14+
'foo' => [
15+
'dsn' => 'bar',
16+
],
17+
'bar' => [
18+
'dsn' => 'foobar',
19+
],
20+
],
21+
'error_pages' => [
22+
'with_trace' => false,
23+
]
24+
];
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Symfony\Component\Config\Tests\Builder\Fixtures;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
8+
class ArrayValues implements ConfigurationInterface
9+
{
10+
public function getConfigTreeBuilder(): TreeBuilder
11+
{
12+
$tb = new TreeBuilder('array_values');
13+
$rootNode = $tb->getRootNode();
14+
$rootNode
15+
->children()
16+
->arrayNode('transports')
17+
->normalizeKeys(false)
18+
->useAttributeAsKey('name')
19+
->arrayPrototype()
20+
->beforeNormalization()
21+
->ifString()
22+
->then(function (string $dsn) {
23+
return ['dsn' => $dsn];
24+
})
25+
->end()
26+
->fixXmlConfig('option')
27+
->children()
28+
->scalarNode('dsn')->end()
29+
->end()
30+
->end()
31+
->end()
32+
->arrayNode('error_pages')
33+
->canBeEnabled()
34+
->children()
35+
->booleanNode('with_trace')->end()
36+
->end()
37+
->end()
38+
->end();
39+
40+
return $tb;
41+
}
42+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Symfony\Config\ArrayValues;
4+
5+
use Symfony\Component\Config\Loader\ParamConfigurator;
6+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
7+
8+
/**
9+
* This class is automatically generated to help in creating a config.
10+
*/
11+
class ErrorPagesConfig
12+
{
13+
private $enabled;
14+
private $withTrace;
15+
private $_usedProperties = [];
16+
17+
/**
18+
* @default false
19+
* @param ParamConfigurator|bool $value
20+
* @return $this
21+
*/
22+
public function enabled($value): static
23+
{
24+
$this->_usedProperties['enabled'] = true;
25+
$this->enabled = $value;
26+
27+
return $this;
28+
}
29+
30+
/**
31+
* @default null
32+
* @param ParamConfigurator|bool $value
33+
* @return $this
34+
*/
35+
public function withTrace($value): static
36+
{
37+
$this->_usedProperties['withTrace'] = true;
38+
$this->withTrace = $value;
39+
40+
return $this;
41+
}
42+
43+
public function __construct(array $value = [])
44+
{
45+
if (array_key_exists('enabled', $value)) {
46+
$this->_usedProperties['enabled'] = true;
47+
$this->enabled = $value['enabled'];
48+
unset($value['enabled']);
49+
}
50+
51+
if (array_key_exists('with_trace', $value)) {
52+
$this->_usedProperties['withTrace'] = true;
53+
$this->withTrace = $value['with_trace'];
54+
unset($value['with_trace']);
55+
}
56+
57+
if ([] !== $value) {
58+
throw new InvalidConfigurationException(sprintf('The following keys are not supported by "%s": ', __CLASS__).implode(', ', array_keys($value)));
59+
}
60+
}
61+
62+
public function toArray(): array
63+
{
64+
$output = [];
65+
if (isset($this->_usedProperties['enabled'])) {
66+
$output['enabled'] = $this->enabled;
67+
}
68+
if (isset($this->_usedProperties['withTrace'])) {
69+
$output['with_trace'] = $this->withTrace;
70+
}
71+
72+
return $output;
73+
}
74+
75+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Symfony\Config\ArrayValues;
4+
5+
use Symfony\Component\Config\Loader\ParamConfigurator;
6+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
7+
8+
/**
9+
* This class is automatically generated to help in creating a config.
10+
*/
11+
class TransportsConfig
12+
{
13+
private $dsn;
14+
private $_usedProperties = [];
15+
16+
/**
17+
* @default null
18+
* @param ParamConfigurator|mixed $value
19+
* @return $this
20+
*/
21+
public function dsn($value): static
22+
{
23+
$this->_usedProperties['dsn'] = true;
24+
$this->dsn = $value;
25+
26+
return $this;
27+
}
28+
29+
public function __construct(array $value = [])
30+
{
31+
if (array_key_exists('dsn', $value)) {
32+
$this->_usedProperties['dsn'] = true;
33+
$this->dsn = $value['dsn'];
34+
unset($value['dsn']);
35+
}
36+
37+
if ([] !== $value) {
38+
throw new InvalidConfigurationException(sprintf('The following keys are not supported by "%s": ', __CLASS__).implode(', ', array_keys($value)));
39+
}
40+
}
41+
42+
public function toArray(): array
43+
{
44+
$output = [];
45+
if (isset($this->_usedProperties['dsn'])) {
46+
$output['dsn'] = $this->dsn;
47+
}
48+
49+
return $output;
50+
}
51+
52+
}

0 commit comments

Comments
 (0)