Skip to content
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

Using a PHP class constant for a key in a YAML hash as a method argument produces an ErrorException #35179

Closed
rhift opened this issue Jan 2, 2020 · 3 comments

Comments

@rhift
Copy link

rhift commented Jan 2, 2020

Symfony version(s) affected: 4.4.2

Description
When trying to use a PHP class constant as a YAML key for a hash (as a method argument), I receive an ErrorException with the message Uninitialized string offset: 0 and code 8.

How to reproduce
Using the following class and Symfony YAML and DI I receive an ErrorException with the message Uninitialized string offset: 0 and code 8.

<?php
declare(strict_types=1);

namespace VENDOR\PRODUCT;

class Example
{
    public const CONSTANT_1 = 'constant_1';
    public const CONSTANT_2 = 'constant_2';

    private $Options;

    public function addOptions(array $options): Example
    {
        $this->Options[] = $options;

        return $this;
    }
}
services:
  VENDOR\PRODUCT\Example:
    class: VENDOR\PRODUCT\Example
    public: false # probably unrelated
    shared: true # probably unrelated
    calls:
      - [addOptions, [{
          !php/const 'VENDOR\PRODUCT\Example::CONSTANT_1': !php/const VENDOR\PRODUCT\Example::CONSTANT_2,
          key_1: "value_1",
          key_2: "value_2"
        }]]

Possible Solution

Additional context
I am more than willing to bet that I am formatting the YAML incorrectly to accomplish this, but I have tried many variations of indentations and YAML hash expressions. Constants as YAML hash keys do work if I use them like the following. So my assumption of the behavior is that they should be able to be used for any valid YAML hash expression (like my example above).

    arguments:
      - !php/const '\VENDOR\PRODUCT\Example::CONSTANT_1': !php/const \VENDOR\PRODUCT\Example::CONSTANT_2

I have quoted the constant for the key in my example above due to this comment. However, I have tried it without quoting and I receive an error about the colons (which I expect due to YAML processing).

Thank you =)

Update
I should probably also mention that the control case works as expected, i.e.

services:
  VENDOR\PRODUCT\Example:
    class: VENDOR\PRODUCT\Example
    public: false # probably unrelated
    shared: true # probably unrelated
    calls:
      - [addOptions, [{
          key_0: "value_0",
          key_1: "value_1",
          key_2: "value_2"
        }]]
@fancyweb
Copy link
Contributor

fancyweb commented Jan 3, 2020

Status: reviewed.

That's not supported because we don't support spaces in mapping keys, so in your example the key is !php/const which fails (with a bad message that could be improved). A solution would be to quote the key but we logically only evaluate non quoted keys so that's not a solution. Fixing this case is however possible.

@nicolas-grekas Is that a bug fix or a new feature? 😕

@xabbuh
Copy link
Member

xabbuh commented Jan 3, 2020

I don't remember if we made a conscious decision not to support tags in making keys. We should check that first. Nonetheless, improving the reported error could still be done as a bugfix.

@xabbuh
Copy link
Member

xabbuh commented Jan 13, 2020

I think this actually is a bug (see #35208 (comment) as well as #35318 for the fix).

@xabbuh xabbuh added the Bug label Jan 13, 2020
@fabpot fabpot closed this as completed Jan 13, 2020
fabpot added a commit that referenced this issue Jan 13, 2020
…n (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Yaml] fix PHP const mapping keys using the inline notation

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

Commits
-------

45461c7 fix PHP const mapping keys using the inline notation
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

5 participants