Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Config] useAttributeAsKey with numeric value triggers warning #36001

Copy link
Copy link
Closed
@jaapio

Description

@jaapio
Issue body actions

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.