vendor/doctrine/persistence/src/Persistence/Mapping/RuntimeReflectionService.php line 38

Open in your IDE?
  1. <?php
  2. namespace Doctrine\Persistence\Mapping;
  3. use Doctrine\Persistence\Reflection\RuntimePublicReflectionProperty;
  4. use Doctrine\Persistence\Reflection\TypedNoDefaultReflectionProperty;
  5. use Doctrine\Persistence\Reflection\TypedNoDefaultRuntimePublicReflectionProperty;
  6. use ReflectionClass;
  7. use ReflectionException;
  8. use ReflectionMethod;
  9. use ReflectionProperty;
  10. use function array_key_exists;
  11. use function assert;
  12. use function class_exists;
  13. use function class_parents;
  14. use function phpversion;
  15. use function version_compare;
  16. /**
  17.  * PHP Runtime Reflection Service.
  18.  */
  19. class RuntimeReflectionService implements ReflectionService
  20. {
  21.     /** @var bool */
  22.     private $supportsTypedPropertiesWorkaround;
  23.     public function __construct()
  24.     {
  25.         $this->supportsTypedPropertiesWorkaround version_compare((string) phpversion(), '7.4.0') >= 0;
  26.     }
  27.     /**
  28.      * {@inheritDoc}
  29.      */
  30.     public function getParentClasses($class)
  31.     {
  32.         if (! class_exists($class)) {
  33.             throw MappingException::nonExistingClass($class);
  34.         }
  35.         $parents class_parents($class);
  36.         assert($parents !== false);
  37.         return $parents;
  38.     }
  39.     /**
  40.      * {@inheritDoc}
  41.      */
  42.     public function getClassShortName($class)
  43.     {
  44.         $reflectionClass = new ReflectionClass($class);
  45.         return $reflectionClass->getShortName();
  46.     }
  47.     /**
  48.      * {@inheritDoc}
  49.      */
  50.     public function getClassNamespace($class)
  51.     {
  52.         $reflectionClass = new ReflectionClass($class);
  53.         return $reflectionClass->getNamespaceName();
  54.     }
  55.     /**
  56.      * @param string $class
  57.      * @psalm-param class-string<T> $class
  58.      *
  59.      * @return ReflectionClass
  60.      * @psalm-return ReflectionClass<T>
  61.      *
  62.      * @template T of object
  63.      */
  64.     public function getClass($class)
  65.     {
  66.         return new ReflectionClass($class);
  67.     }
  68.     /**
  69.      * {@inheritDoc}
  70.      */
  71.     public function getAccessibleProperty($class$property)
  72.     {
  73.         $reflectionProperty = new ReflectionProperty($class$property);
  74.         if ($this->supportsTypedPropertiesWorkaround && ! array_key_exists($property$this->getClass($class)->getDefaultProperties())) {
  75.             if ($reflectionProperty->isPublic()) {
  76.                 $reflectionProperty = new TypedNoDefaultRuntimePublicReflectionProperty($class$property);
  77.             } else {
  78.                 $reflectionProperty = new TypedNoDefaultReflectionProperty($class$property);
  79.             }
  80.         } elseif ($reflectionProperty->isPublic()) {
  81.             $reflectionProperty = new RuntimePublicReflectionProperty($class$property);
  82.         }
  83.         $reflectionProperty->setAccessible(true);
  84.         return $reflectionProperty;
  85.     }
  86.     /**
  87.      * {@inheritDoc}
  88.      */
  89.     public function hasPublicMethod($class$method)
  90.     {
  91.         try {
  92.             $reflectionMethod = new ReflectionMethod($class$method);
  93.         } catch (ReflectionException $e) {
  94.             return false;
  95.         }
  96.         return $reflectionMethod->isPublic();
  97.     }
  98. }