Description
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"
}]]