symfony: Cannot assign Symfony\Component\VarDumper\Caster\CutStub to reference held by property ...

Symfony version(s) affected

6.2.4

Description

I have an error Cannot assign Symfony\Component\VarDumper\Caster\CutStub to reference held by property App\Entity\Preference::$distance of type ?App\Enum\DistanceEnum

I have a property in Entity:

#[ORM\Column(length: 255, nullable: true, enumType: DistanceEnum::class)]
private ?DistanceEnum $distance = null;

How to reproduce

With APP_ENV=dev, I have the error. With APP_ENV=prod, I have no error.

Possible Solution

No response

Additional Context

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 29 (21 by maintainers)

Commits related to this issue

Most upvoted comments

With this configuration, it’s works but we lose the functionality of the lazy ghost object

# doctrine.yaml 
doctrine:
    orm:
        enable_lazy_ghost_objects: false

Looks like the following patch would fix that issue. Could you please submit it as a bugfix on branch 5.4 with a test case derived from your reproducer?

--- a/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php
+++ b/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php
@@ -61,8 +61,16 @@ abstract class DataCollector implements DataCollectorInterface
             '*' => function ($v, array $a, Stub $s, $isNested) {
                 if (!$v instanceof Stub) {
                     foreach ($a as $k => $v) {
-                        if (\is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
+                        if (!\is_object($v) || $v instanceof \DateTimeInterface || $v instanceof Stub) {
+                            continue;
+                        }
+
+                        try {
+                            $a[$k] = new CutStub($v);
+                        } catch (\TypeError) {
+                            $a[$k] = &$v;
                             $a[$k] = new CutStub($v);
+                            unset($v);
                         }
                     }
                 }

Well, if you want to use a version of PHP missing 2 years of bugfixes, feel free to find a way to reimplement VarDumper without being impacted by that PHP bug (but without loosing any of the features of the component). But for that bug, it will probably be very hard (anyway, anything that has reimplement VarDumper in its title will be hard, even without additional constraints). However, I don’t see any benefit for the Symfony core team spending that time instead of doing more useful work on Symfony.

Please provide a reproducer that works on the latest version of PHP if you still experience the issue.

Please send this reproducer as a second test case on your upcoming PR and I’ll have a look?

You have to change your version of PHP as @nicolas-grekas said.

OK, I figured out the password 😅

I don’t reproduce. I think you’re affected by https://github.com/php/php-src/issues/8655

Please upgrade to the latest PHP 8.1

those are log messages, not a stack trace but at least, this gives the location of the error.

@nicolas-grekas shouldn’t we also exclude enums (like we exclude DateTimeInterface) the DataCollector fallback caster ? https://github.com/symfony/symfony/blob/e480a66395c3ef21fb9895a2b5a1fe6211e19456/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php#L67

However, I still don’t understand why this would assign it in the entity property.