Skip to content

Using a PHP class constant for a key in a YAML hash as a method argument produces a ParseException  #36624

Closed
@rhift

Description

@rhift

Symfony version(s) affected: 4.4.7

Description
Virtually the same as #35179 but with different exceptions.

When trying to use a PHP class constant as a YAML key for a hash (as a method argument), I receive one of the following.

  • a \Symfony\Component\Yaml\Exception\ParseException with the message The constant "\VENDOR\PRODUCT\Example" is not defined at line 7 (near "!php/const \VENDOR\PRODUCT\Example"). if I use YAML Variant A below.
  • a \Symfony\Component\Yaml\Exception\ParseException with the message Malformed inline YAML string: ""\VENDOR\PRODUCT\Example" at line 7 (near ""\VENDOR\PRODUCT\Example"). if I use YAML Variant B below. I observe the same behavior when using single quotes.

How to reproduce
Using the following class and Symfony YAML DI files produces the above.

<?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;
    }
}
# YAML Variant A
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: 'value_0',
          key_1: "value_1",
          key_2: "value_2"
        }]]
# YAML Variant B
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": 'value_0',
          key_1: "value_1",
          key_2: "value_2"
        }]]

Also, the control case and global PHP constants (in both variants) work as expected, i.e:

# YAML Variant A
services:
  VENDOR\PRODUCT\Example:
    class: VENDOR\PRODUCT\Example
    public: false # probably unrelated
    shared: true # probably unrelated
    calls:
      - [addOptions, [{
          !php/const PHP_INT_MAX: "value_0",
          key_1: "value_1",
          key_2: "value_2"
        }]]
# YAML Variant B
services:
  VENDOR\PRODUCT\Example:
    class: VENDOR\PRODUCT\Example
    public: false # probably unrelated
    shared: true # probably unrelated
    calls:
      - [addOptions, [{
          !php/const "PHP_INT_MAX": "value_0",
          key_1: "value_1",
          key_2: "value_2"
        }]]

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