Static properties fail to be accessed through RuntimeReflectionProperty

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: Doctrine
    • None
    • None
    • PHP Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Tracks https://github.com/doctrine/mongodb-odm/issues/3013

      UnitOfWork::doMerge() iterates over all properties of a class via $class->reflClass->getProperties(), including static properties, regardless of whether they are mapped (src/UnitOfWork.php:1910). For a static property, RuntimeReflectionProperty::getValue($document) returns null (it differs from native ReflectionProperty, which returns the actual value), so the merge assigns null back and crashes, e.g. "Cannot assign null to property ...::$type of type string".

      Reported after upgrading from 2.5.7 to 3.3.3.

      Reproducer (from the report):

      class Foo
      {
          public static $staticProp = 'staticPropValue';
      }
      
      $fooInstance = new Foo();
      
      $propNative   = new ReflectionProperty(Foo::class, 'staticProp');
      $propDoctrine = new \Doctrine\Persistence\Reflection\RuntimeReflectionProperty(Foo::class, 'staticProp');
      
      var_dump(
          $propNative->getValue(),               // string "staticPropValue"
          $propNative->getValue($fooInstance),   // string "staticPropValue"
          $propDoctrine->getValue(),             // string "staticPropValue"
          $propDoctrine->getValue($fooInstance), // NULL  <-- bug
      );
      

      Fix: ignore static properties systematically. doMerge() (and any other place iterating over reflection properties) must skip static properties since they are never mapped fields. This resolves the crash without depending on doctrine/persistence behavior.

            Assignee:
            Jérôme Tamarelle
            Reporter:
            Jérôme Tamarelle
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: