Skip to content

Cannot access uninitialized non-nullable property by reference #48562

Closed
@greg0ire

Description

@greg0ire

Symfony version(s) affected

6.2.*

Description

It seems that under some circumstances that have yet to be determined, making a service lazy with the lazy ghost trait causes the issue in the title.

How to reproduce

I might work on a reproducer. Until I have built one, here is what my service looks like:

<?php

declare(strict_types=1);

namespace App\Service;

class BuggyService
{
     /** @var array<string, int> */
    private array $foo;

    public function __construct()
    {
    }

    public function __invoke(): void
    {
        $this->foo['hello'] = 42;
    }
}

https://github.com/greg0ire/symfony-48562

A workaround is to initialize the variable to an empty array.

Possible Solution

No response

Additional Context

Here is the corresponding stack trace:

[critical] Error thrown while running command "expose:bug -vvv". Message: "Cannot access uninitialized non-nullable property App\Service\BuggyService::$coupons by reference"
[debug] Command "expose:bug -vvv" exited with code "1"

In LazyObjectRegistry.php line 99:

  [Error]
  Cannot access uninitialized non-nullable property App\Service\BuggyService::$coupons by reference


Exception trace:
  at /tmp/bug_app/vendor/symfony/var-exporter/Internal/LazyObjectRegistry.php:99
 App\Service\BuggyService::Symfony\Component\VarExporter\Internal\{closure}() at /tmp/bug_app/vendor/symfony/var-exporter/LazyGhostTrait.php:206
 Container26L4KTM\BuggyService_ba73178->__get() at /tmp/bug_app/src/Service/BuggyService.php:18
 App\Service\BuggyService->__invoke() at /tmp/bug_app/src/Command/ExposeBug.php:26
 App\Command\ExposeBug->execute() at /tmp/bug_app/vendor/symfony/console/Command/Command.php:312
 Symfony\Component\Console\Command\Command->run() at /tmp/bug_app/vendor/symfony/console/Application.php:1038
 Symfony\Component\Console\Application->doRunCommand() at /tmp/bug_app/vendor/symfony/framework-bundle/Console/Application.php:88
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /tmp/bug_app/vendor/symfony/console/Application.php:312
 Symfony\Component\Console\Application->doRun() at /tmp/bug_app/vendor/symfony/framework-bundle/Console/Application.php:77
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /tmp/bug_app/vendor/symfony/console/Application.php:168
 Symfony\Component\Console\Application->run() at /tmp/bug_app/vendor/symfony/runtime/Runner/Symfony/ConsoleApplicationRunner.php:54
 Symfony\Component\Runtime\Runner\Symfony\ConsoleApplicationRunner->run() at /tmp/bug_app/vendor/autoload_runtime.php:29
 require_once() at /tmp/bug_app/bin/console:11

expose:bug

What's really crazy with this bug is that changing the variable name to e.g. coupon or foo fixes it, while changing it to toto doesn't (yes, I have cleared my Symfony cache). 🤯

UPDATE: clearing the cache with rm -rf makes a difference

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