Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Config] useAttributeAsKey with numeric value triggers warning #36001

Closed
@jaapio

Description

@jaapio

Symfony version(s) affected: 5.0

Description
Given a config like this:

<version number="3.0">
        <folder>latest</folder>
</version>
<version number="2.0">
        <folder>other</folder>
</version>

We wanted to have the output as:

[
  '2.0' => [
     'folder' => 'other',
  ],
  '3.0' => [
      'folder' => 'latest',
]

The definition looks like this:

                ->arrayNode('versions')
                    ->useAttributeAsKey('number')
                    ->addDefaultChildrenIfNoneSet('1.0.0')
                    ->prototype('array')
                        ->scalarNode('folder')->defaultValue('')->end()

A warning is triggered in vendor/symfony/config/Definition/PrototypedArrayNode.php:252 because $k is a float. Normalization doesn't work because the value is extracted and cannot be normalized. The root cause is in \Symfony\Component\Config\Util\XmlUtils::phpize because number is converted to a float, which is correct in all cases. But not when using the attribute as a key. Than it should be converted back to a string or integer value when the type is not allowed as a array key.

In this case a string would be expected.

Possible Solution
Use var_export to convert the value of $k back to a string when it was a float / boolean

Additional context
phpDocumentor/phpDocumentor#2282

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions