8000 [Config] useAttributeAsKey with numeric value triggers warning · Issue #36001 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Config] useAttributeAsKey with numeric value triggers warning #36001

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

Closed
jaapio opened this issue Mar 8, 2020 · 1 comment
Closed

[Config] useAttributeAsKey with numeric value triggers warning #36001

jaapio opened this issue Mar 8, 2020 · 1 comment

Comments

@jaapio
Copy link
Contributor
jaapio commented Mar 8, 2020

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

@xabbuh
Copy link
Member
xabbuh commented Jan 8, 2021

Status: Reviewed

see #39764

nicolas-grekas added a commit that referenced this issue Jan 12, 2021
…bbuh)

This PR was merged into the 4.4 branch.

Discussion
----------

[Config]  fix handling float-like key attribute values

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36001
| License       | MIT
| Doc PR        |

Commits
-------

a2ad4fa fix handling float-like key attribute values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0