Skip to content

[Config] Do not generate unreachable configuration paths #58995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions src/Symfony/Component/Config/Builder/ConfigBuilderGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,13 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
$class->addRequire($childClass);
$this->classes[] = $childClass;

$nodeTypes = $this->getParameterTypes($node);
$paramType = $this->getParamType($nodeTypes);

$hasNormalizationClosures = $this->hasNormalizationClosures($node);
$comment = $this->getComment($node);
if ($hasNormalizationClosures) {
$comment = \sprintf(" * @template TValue\n * @param TValue \$value\n%s", $comment);
if ($hasNormalizationClosures && 'array' !== $paramType) {
$comment = \sprintf(" * @template TValue of %s\n * @param TValue \$value\n%s", $paramType, $comment);
$comment .= \sprintf(' * @return %s|$this'."\n", $childClass->getFqcn());
$comment .= \sprintf(' * @psalm-return (TValue is array ? %s : static)'."\n ", $childClass->getFqcn());
}
Expand All @@ -142,8 +145,7 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
$node->getName(),
$this->getType($childClass->getFqcn(), $hasNormalizationClosures)
);
$nodeTypes = $this->getParameterTypes($node);
$body = $hasNormalizationClosures ? '
$body = $hasNormalizationClosures && 'array' !== $paramType ? '
COMMENTpublic function NAME(PARAM_TYPE $value = []): CLASS|static
{
if (!\is_array($value)) {
Expand Down Expand Up @@ -178,7 +180,7 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
'COMMENT' => $comment,
'PROPERTY' => $property->getName(),
'CLASS' => $childClass->getFqcn(),
'PARAM_TYPE' => \in_array('mixed', $nodeTypes, true) ? 'mixed' : implode('|', $nodeTypes),
'PARAM_TYPE' => $paramType,
]);

$this->buildNode($node, $childClass, $this->getSubNamespace($childClass));
Expand Down Expand Up @@ -218,10 +220,11 @@ private function handlePrototypedArrayNode(PrototypedArrayNode $node, ClassBuild

$nodeParameterTypes = $this->getParameterTypes($node);
$prototypeParameterTypes = $this->getParameterTypes($prototype);
$noKey = null === $key = $node->getKeyAttribute();
if (!$prototype instanceof ArrayNode || ($prototype instanceof PrototypedArrayNode && $prototype->getPrototype() instanceof ScalarNode)) {
$class->addUse(ParamConfigurator::class);
$property = $class->addProperty($node->getName());
if (null === $key = $node->getKeyAttribute()) {
if ($noKey) {
// This is an array of values; don't use singular name
$nodeTypesWithoutArray = array_filter($nodeParameterTypes, static fn ($type) => 'array' !== $type);
$body = '
Expand All @@ -242,7 +245,7 @@ public function NAME(PARAM_TYPE $value): static
'PROPERTY' => $property->getName(),
'PROTOTYPE_TYPE' => implode('|', $prototypeParameterTypes),
'EXTRA_TYPE' => $nodeTypesWithoutArray ? '|'.implode('|', $nodeTypesWithoutArray) : '',
'PARAM_TYPE' => \in_array('mixed', $nodeParameterTypes, true) ? 'mixed' : 'ParamConfigurator|'.implode('|', $nodeParameterTypes),
'PARAM_TYPE' => $this->getParamType($nodeParameterTypes, true),
]);
} else {
$body = '
Expand All @@ -259,7 +262,7 @@ public function NAME(string $VAR, TYPE $VALUE): static

$class->addMethod($methodName, $body, [
'PROPERTY' => $property->getName(),
'TYPE' => \in_array('mixed', $prototypeParameterTypes, true) ? 'mixed' : 'ParamConfigurator|'.implode('|', $prototypeParameterTypes),
'TYPE' => $this->getParamType($prototypeParameterTypes, true),
'VAR' => '' === $key ? 'key' : $key,
'VALUE' => 'value' === $key ? 'data' : 'value',
]);
Expand All @@ -280,18 +283,27 @@ public function NAME(string $VAR, TYPE $VALUE): static
$this->getType($childClass->getFqcn().'[]', $hasNormalizationClosures)
);

$paramType = $this->getParamType($noKey ? $nodeParameterTypes : $prototypeParameterTypes);

$comment = $this->getComment($node);
<<<<<<< HEAD
if ($hasNormalizationClosures) {
$comment = \sprintf(" * @template TValue\n * @param TValue \$value\n%s", $comment);
$comment .= \sprintf(' * @return %s|$this'."\n", $childClass->getFqcn());
$comment .= \sprintf(' * @psalm-return (TValue is array ? %s : static)'."\n ", $childClass->getFqcn());
=======
if ($hasNormalizationClosures && 'array' !== $paramType) {
$comment = sprintf(" * @template TValue of %s\n * @param TValue \$value\n%s", $paramType, $comment);
$comment .= sprintf(' * @return %s|$this'."\n", $childClass->getFqcn());
$comment .= sprintf(' * @psalm-return (TValue is array ? %s : static)'."\n ", $childClass->getFqcn());
>>>>>>> 100c683018d ([Config] Do not generate unreachable configuration paths)
}
if ('' !== $comment) {
$comment = "/**\n$comment*/\n";
}

if (null === $key = $node->getKeyAttribute()) {
$body = $hasNormalizationClosures ? '
if ($noKey) {
$body = $hasNormalizationClosures && 'array' !== $paramType ? '
COMMENTpublic function NAME(PARAM_TYPE $value = []): CLASS|static
{
$this->_usedProperties[\'PROPERTY\'] = true;
Expand All @@ -313,10 +325,10 @@ public function NAME(string $VAR, TYPE $VALUE): static
'COMMENT' => $comment,
'PROPERTY' => $property->getName(),
'CLASS' => $childClass->getFqcn(),
'PARAM_TYPE' => \in_array('mixed', $nodeParameterTypes, true) ? 'mixed' : implode('|', $nodeParameterTypes),
'PARAM_TYPE' => $paramType,
]);
} else {
$body = $hasNormalizationClosures ? '
$body = $hasNormalizationClosures && 'array' !== $paramType ? '
COMMENTpublic function NAME(string $VAR, PARAM_TYPE $VALUE = []): CLASS|static
{
if (!\is_array($VALUE)) {
Expand Down Expand Up @@ -352,7 +364,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
'CLASS' => $childClass->getFqcn(),
'VAR' => '' === $key ? 'key' : $key,
'VALUE' => 'value' === $key ? 'data' : 'value',
'PARAM_TYPE' => \in_array('mixed', $prototypeParameterTypes, true) ? 'mixed' : implode('|', $prototypeParameterTypes),
'PARAM_TYPE' => $paramType,
]);
}

Expand Down Expand Up @@ -597,4 +609,9 @@ private function getType(string $classType, bool $hasNormalizationClosures): str
{
return $classType.($hasNormalizationClosures ? '|scalar' : '');
}

private function getParamType(array $types, bool $withParamConfigurator = false): string
{
return \in_array('mixed', $types, true) ? 'mixed' : ($withParamConfigurator ? 'ParamConfigurator|' : '').implode('|', $types);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\Config\ArrayValuesConfig;

return static function (ArrayValuesConfig $config) {
$config->transports('foo')->dsn('bar');
$config->transports('bar', ['dsn' => 'foobar']);

$config->errorPages()->withTrace(false);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return [
'transports' => [
'foo' => [
'dsn' => 'bar',
],
'bar' => [
'dsn' => 'foobar',
],
],
'error_pages' => [
'with_trace' => false,
]
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Symfony\Component\Config\Tests\Builder\Fixtures;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class ArrayValues implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$tb = new TreeBuilder('array_values');
$rootNode = $tb->getRootNode();
$rootNode
->children()
->arrayNode('transports')
->normalizeKeys(false)
->useAttributeAsKey('name')
->arrayPrototype()
->beforeNormalization()
->ifString()
->then(function (string $dsn) {
return ['dsn' => $dsn];
})
->end()
->fixXmlConfig('option')
->children()
->scalarNode('dsn')->end()
->end()
->end()
->end()
->arrayNode('error_pages')
->canBeEnabled()
->children()
->booleanNode('with_trace')->end()
->end()
->end()
->end();

return $tb;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Symfony\Config\ArrayValues;

use Symfony\Component\Config\Loader\ParamConfigurator;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

/**
* This class is automatically generated to help in creating a config.
*/
class ErrorPagesConfig
{
private $enabled;
private $withTrace;
private $_usedProperties = [];

/**
* @default false
* @param ParamConfigurator|bool $value
* @return $this
*/
public function enabled($value): static
{
$this->_usedProperties['enabled'] = true;
$this->enabled = $value;

return $this;
}

/**
* @default null
* @param ParamConfigurator|bool $value
* @return $this
*/
public function withTrace($value): static
{
$this->_usedProperties['withTrace'] = true;
$this->withTrace = $value;

return $this;
}

public function __construct(array $value = [])
{
if (array_key_exists('enabled', $value)) {
$this->_usedProperties['enabled'] = true;
$this->enabled = $value['enabled'];
unset($value['enabled']);
}

if (array_key_exists('with_trace', $value)) {
$this->_usedProperties['withTrace'] = true;
$this->withTrace = $value['with_trace'];
unset($value['with_trace']);
}

if ([] !== $value) {
throw new InvalidConfigurationException(sprintf('The following keys are not supported by "%s": ', __CLASS__).implode(', ', array_keys($value)));
}
}

public function toArray(): array
{
$output = [];
if (isset($this->_usedProperties['enabled'])) {
$output['enabled'] = $this->enabled;
}
if (isset($this->_usedProperties['withTrace'])) {
$output['with_trace'] = $this->withTrace;
}

return $output;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Symfony\Config\ArrayValues;

use Symfony\Component\Config\Loader\ParamConfigurator;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

/**
* This class is automatically generated to help in creating a config.
*/
class TransportsConfig
{
private $dsn;
private $_usedProperties = [];

/**
* @default null
* @param ParamConfigurator|mixed $value
* @return $this
*/
public function dsn($value): static
{
$this->_usedProperties['dsn'] = true;
$this->dsn = $value;

return $this;
}

public function __construct(array $value = [])
{
if (array_key_exists('dsn', $value)) {
$this->_usedProperties['dsn'] = true;
$this->dsn = $value['dsn'];
unset($value['dsn']);
}

if ([] !== $value) {
throw new InvalidConfigurationException(sprintf('The following keys are not supported by "%s": ', __CLASS__).implode(', ', array_keys($value)));
}
}

public function toArray(): array
{
$output = [];
if (isset($this->_usedProperties['dsn'])) {
$output['dsn'] = $this->dsn;
}

return $output;
}

}
Loading
Loading
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