bug #40878 [Config] Use plural name on array values (Nyholm)

This PR was squashed before being merged into the 5.3-dev branch.

Discussion
----------

[Config] Use plural name on array values

| Q             | A
| ------------- | ---
| Branch?       | 5.x
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        | Related: https://github.com/symfony/symfony-docs/pull/15264

If we have a PrototypedArrayNode and the prototype is does not have a key (`->useAttributeAsKey('name')`). Then we know for sure that the value should be an array.

See this example from `$framework->assets()`

```diff
    /**
+    * @param list<mixed>
     * @return $this
     */
-   public function baseUrl($value): self
+   public function baseUrls(array $value): self
    {
        $this->baseUrls = $value;

        return $this;
    }
```

Commits
-------

f564a7a11a [Config] Use plural name on array values
This commit is contained in:
Nicolas Grekas 2021-04-21 11:07:41 +02:00
commit f82c11883f
5 changed files with 10 additions and 5 deletions

View File

@ -172,17 +172,20 @@ public function NAME($valueDEFAULT): self
if (null !== $parameterType || $prototype instanceof ScalarNode) { if (null !== $parameterType || $prototype instanceof ScalarNode) {
$property = $class->addProperty($node->getName()); $property = $class->addProperty($node->getName());
if (null === $key = $node->getKeyAttribute()) { if (null === $key = $node->getKeyAttribute()) {
// This is an array of values; don't use singular name
$body = ' $body = '
/** /**
* @param list<TYPE> $value
* @return $this * @return $this
*/ */
public function NAME(TYPE$value): self public function NAME(array $value): self
{ {
$this->PROPERTY = $value; $this->PROPERTY = $value;
return $this; return $this;
}'; }';
$class->addMethod($methodName, $body, ['PROPERTY' => $property->getName(), 'TYPE' => '' === $parameterType ? '' : $parameterType.' ']);
$class->addMethod($node->getName(), $body, ['PROPERTY' => $property->getName(), 'TYPE' => '' === $parameterType ? 'mixed' : $parameterType]);
} else { } else {
$body = ' $body = '
/** /**

View File

@ -3,7 +3,7 @@
use Symfony\Config\AddToListConfig; use Symfony\Config\AddToListConfig;
return static function (AddToListConfig $config) { return static function (AddToListConfig $config) {
$config->translator()->fallback(['sv', 'fr', 'es']); $config->translator()->fallbacks(['sv', 'fr', 'es']);
$config->translator()->source('\\Acme\\Foo', 'yellow'); $config->translator()->source('\\Acme\\Foo', 'yellow');
$config->translator()->source('\\Acme\\Bar', 'green'); $config->translator()->source('\\Acme\\Bar', 'green');

View File

@ -35,6 +35,7 @@ class AddToList implements ConfigurationInterface
->useAttributeAsKey('message_class') ->useAttributeAsKey('message_class')
->prototype('array') ->prototype('array')
->performNoDeepMerging() ->performNoDeepMerging()
->fixXmlConfig('sender')
->children() ->children()
->arrayNode('senders') ->arrayNode('senders')
->requiresAtLeastOneElement() ->requiresAtLeastOneElement()

View File

@ -11,5 +11,5 @@ return static function (NodeInitialValuesConfig $config) {
$config->messenger() $config->messenger()
->transports('slow_queue') ->transports('slow_queue')
->dsn('doctrine://') ->dsn('doctrine://')
->option(['table'=>'my_messages']); ->options(['table'=>'my_messages']);
}; };

View File

@ -104,7 +104,8 @@ class GeneratedConfigTest extends TestCase
return new $fqcn(); return new $fqcn();
} }
$outputDir = sys_get_temp_dir(); $outputDir = sys_get_temp_dir().\DIRECTORY_SEPARATOR.uniqid('sf_config_builder', true);
// This line is helpful for debugging // This line is helpful for debugging
// $outputDir = __DIR__.'/.build'; // $outputDir = __DIR__.'/.build';