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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Persistence\Mapping;
  4. use Doctrine\Persistence\Reflection\RuntimePublicReflectionProperty;
  5. use Doctrine\Persistence\Reflection\TypedNoDefaultReflectionProperty;
  6. use Doctrine\Persistence\Reflection\TypedNoDefaultRuntimePublicReflectionProperty;
  7. use ReflectionClass;
  8. use ReflectionException;
  9. use ReflectionMethod;
  10. use ReflectionProperty;
  11. use function array_key_exists;
  12. use function assert;
  13. use function class_exists;
  14. use function class_parents;
  15. use function phpversion;
  16. use function version_compare;
  17. /**
  18.  * PHP Runtime Reflection Service.
  19.  */
  20. class RuntimeReflectionService implements ReflectionService
  21. {
  22.     /** @var bool */
  23.     private $supportsTypedPropertiesWorkaround;
  24.     public function __construct()
  25.     {
  26.         $this->supportsTypedPropertiesWorkaround version_compare(phpversion(), '7.4.0') >= 0;
  27.     }
  28.     /**
  29.      * {@inheritDoc}
  30.      */
  31.     public function getParentClasses(string $class)
  32.     {
  33.         if (! class_exists($class)) {
  34.             throw MappingException::nonExistingClass($class);
  35.         }
  36.         $parents class_parents($class);
  37.         assert($parents !== false);
  38.         return $parents;
  39.     }
  40.     /**
  41.      * {@inheritDoc}
  42.      */
  43.     public function getClassShortName(string $class)
  44.     {
  45.         $reflectionClass = new ReflectionClass($class);
  46.         return $reflectionClass->getShortName();
  47.     }
  48.     /**
  49.      * {@inheritDoc}
  50.      */
  51.     public function getClassNamespace(string $class)
  52.     {
  53.         $reflectionClass = new ReflectionClass($class);
  54.         return $reflectionClass->getNamespaceName();
  55.     }
  56.     /**
  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(string $class)
  65.     {
  66.         return new ReflectionClass($class);
  67.     }
  68.     /**
  69.      * {@inheritDoc}
  70.      */
  71.     public function getAccessibleProperty(string $classstring $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(string $classstring $method)
  90.     {
  91.         try {
  92.             $reflectionMethod = new ReflectionMethod($class$method);
  93.         } catch (ReflectionException $e) {
  94.             return false;
  95.         }
  96.         return $reflectionMethod->isPublic();
  97.     }
  98. }