8000 [DotEnv] Uninitialized string offset: 1 · Issue #34537 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DotEnv] Uninitialized string offset: 1 #34537

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
olix21 opened this issue Nov 23, 2019 · 1 comment
Closed

[DotEnv] Uninitialized string offset: 1 #34537

olix21 opened this issue Nov 23, 2019 · 1 comment

Comments

@olix21
Copy link
olix21 commented Nov 23, 2019

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

@xabbuh
Copy link
Member
xabbuh commented 8393 Nov 23, 2019

see #34552

@fabpot fabpot closed this as completed Nov 24, 2019
fabpot added a commit that referenced this issue Nov 24, 2019
… (xabbuh)

This PR was merged into the 4.4 branch.

Discussion
----------

[Dotenv] don't fail when referenced env var does not exist

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

Commits
-------

8026609 don't fail when referenced env var does not exist
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

3 participants
0