vendor/doctrine/persistence/lib/Doctrine/Persistence/Reflection/TypedNoDefaultReflectionProperty.php line 19

Open in your IDE?
  1. <?php
  2. namespace Doctrine\Persistence\Reflection;
  3. use ReflectionProperty;
  4. /**
  5.  * PHP Typed No Default Reflection Property - special override for typed properties without a default value.
  6.  */
  7. class TypedNoDefaultReflectionProperty extends ReflectionProperty
  8. {
  9.     /**
  10.      * {@inheritDoc}
  11.      *
  12.      * Checks that a typed property is initialized before accessing its value.
  13.      * This is necessary to avoid PHP error "Error: Typed property must not be accessed before initialization".
  14.      * Should be used only for reflecting typed properties without a default value.
  15.      */
  16.     public function getValue($object null)
  17.     {
  18.         return $object !== null && $this->isInitialized($object) ? parent::getValue($object) : null;
  19.     }
  20.     /**
  21.      * {@inheritDoc}
  22.      *
  23.      * Works around the problem with setting typed no default properties to
  24.      * NULL which is not supported, instead unset() to uninitialize.
  25.      *
  26.      * @link https://github.com/doctrine/orm/issues/7999
  27.      */
  28.     public function setValue($object$value null)
  29.     {
  30.         if ($value === null && $this->hasType() && ! $this->getType()->allowsNull()) {
  31.             $propertyName $this->getName();
  32.             $unsetter = function () use ($propertyName) {
  33.                 unset($this->$propertyName);
  34.             };
  35.             $unsetter $unsetter->bindTo($object$this->getDeclaringClass()->getName());
  36.             $unsetter();
  37.             return;
  38.         }
  39.         parent::setValue($object$value);
  40.     }
  41. }