Skip to content

[DotEnv] Uninitialized string offset: 1 #34537

Closed
@olix21

Description

@olix21

Symfony version(s) affected: >= 4.4

Description
A php notice appears in DotEnv when dealing with empty default env variables:
PHP Notice: Uninitialized string offset: 1 in /vendor/symfony/dotenv/Dotenv.php on line 477

if ('' === $value && isset($matches['default_value'])) {
    $unsupportedChars = strpbrk($matches['default_value'], '\'"{$');
    if (false !== $unsupportedChars) {
         throw $this->createFormatException(sprintf('Unsupported character "%s" found in the default value of variable "$%s".', $unsupportedChars[0], $name));
    }

    $value = substr($matches['default_value'], 2);

    if ('=' === $matches['default_value'][1]) { // Here is the notice
        $this->values[$name] = $value;
    }
}

The issue is that $matches['default_value'] is equal to an empty string, not an array.

In my case:

Array
(
    [0] => ${RABBITMQ_USER}
    [backslashes] => 
    [1] => 
    [opening_brace] => {
    [2] => {
    [name] => RABBITMQ_USER
    [3] => RABBITMQ_USER
    [default_value] => 
    [4] => 
    [closing_brace] => }
    [5] => }
)

It's maybe due to how we wrote our env variable. The issue happens when resolving MESSENGER_DSN:

RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_HOST=127.0.0.1

MESSENGER_DSN=amqp://${RABBITMQ_USER}:${RABBITMQ_PASSWORD}@${RABBITMQ_HOST}:5672/%2f/queue

Possible Resolution

I'm not sure I'm allowed to use such notation for an env variable.
So maybe we should check that $matches['default_value'] is an array and throw an exception if not (and update the documentation?).
Or it could be an empty string and in this case, we should verify the $matches['default_value'] type before going further.

I will be more than happy to do a PR for that, I just need more insight on what to do

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions