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\RuntimeReflectionProperty;
  5. use Doctrine\Persistence\Reflection\TypedNoDefaultReflectionProperty;
  6. use ReflectionClass;
  7. use ReflectionException;
  8. use ReflectionMethod;
  9. use function array_key_exists;
  10. use function assert;
  11. use function class_exists;
  12. use function class_parents;
  13. use function phpversion;
  14. use function version_compare;
  15. use const PHP_VERSION_ID;
  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(phpversion(), '7.4.0') >= 0;
  26.     }
  27.     /**
  28.      * {@inheritDoc}
  29.      */
  30.     public function getParentClasses(string $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(string $class)
  43.     {
  44.         $reflectionClass = new ReflectionClass($class);
  45.         return $reflectionClass->getShortName();
  46.     }
  47.     /**
  48.      * {@inheritDoc}
  49.      */
  50.     public function getClassNamespace(string $class)
  51.     {
  52.         $reflectionClass = new ReflectionClass($class);
  53.         return $reflectionClass->getNamespaceName();
  54.     }
  55.     /**
  56.      * @phpstan-param class-string<T> $class
  57.      *
  58.      * @return ReflectionClass
  59.      * @phpstan-return ReflectionClass<T>
  60.      *
  61.      * @template T of object
  62.      */
  63.     public function getClass(string $class)
  64.     {
  65.         return new ReflectionClass($class);
  66.     }
  67.     /**
  68.      * {@inheritDoc}
  69.      */
  70.     public function getAccessibleProperty(string $classstring $property)
  71.     {
  72.         $reflectionProperty = new RuntimeReflectionProperty($class$property);
  73.         if ($this->supportsTypedPropertiesWorkaround && ! array_key_exists($property$this->getClass($class)->getDefaultProperties())) {
  74.             $reflectionProperty = new TypedNoDefaultReflectionProperty($class$property);
  75.         }
  76.         if (PHP_VERSION_ID 80100) {
  77.             $reflectionProperty->setAccessible(true);
  78.         }
  79.         return $reflectionProperty;
  80.     }
  81.     /**
  82.      * {@inheritDoc}
  83.      */
  84.     public function hasPublicMethod(string $classstring $method)
  85.     {
  86.         try {
  87.             $reflectionMethod = new ReflectionMethod($class$method);
  88.         } catch (ReflectionException $e) {
  89.             return false;
  90.         }
  91.         return $reflectionMethod->isPublic();
  92.     }
  93. }