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

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. /**
  16. * PHP Runtime Reflection Service.
  17. */
  18. class RuntimeReflectionService implements ReflectionService
  19. {
  20. /** @var bool */
  21. private $supportsTypedPropertiesWorkaround;
  22. public function __construct()
  23. {
  24. $this->supportsTypedPropertiesWorkaround = version_compare(phpversion(), '7.4.0') >= 0;
  25. }
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function getParentClasses(string $class)
  30. {
  31. if (! class_exists($class)) {
  32. throw MappingException::nonExistingClass($class);
  33. }
  34. $parents = class_parents($class);
  35. assert($parents !== false);
  36. return $parents;
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function getClassShortName(string $class)
  42. {
  43. $reflectionClass = new ReflectionClass($class);
  44. return $reflectionClass->getShortName();
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function getClassNamespace(string $class)
  50. {
  51. $reflectionClass = new ReflectionClass($class);
  52. return $reflectionClass->getNamespaceName();
  53. }
  54. /**
  55. * @phpstan-param class-string<T> $class
  56. *
  57. * @return ReflectionClass
  58. * @phpstan-return ReflectionClass<T>
  59. *
  60. * @template T of object
  61. */
  62. public function getClass(string $class)
  63. {
  64. return new ReflectionClass($class);
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function getAccessibleProperty(string $class, string $property)
  70. {
  71. $reflectionProperty = new RuntimeReflectionProperty($class, $property);
  72. if ($this->supportsTypedPropertiesWorkaround && ! array_key_exists($property, $this->getClass($class)->getDefaultProperties())) {
  73. $reflectionProperty = new TypedNoDefaultReflectionProperty($class, $property);
  74. }
  75. $reflectionProperty->setAccessible(true);
  76. return $reflectionProperty;
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function hasPublicMethod(string $class, string $method)
  82. {
  83. try {
  84. $reflectionMethod = new ReflectionMethod($class, $method);
  85. } catch (ReflectionException $e) {
  86. return false;
  87. }
  88. return $reflectionMethod->isPublic();
  89. }
  90. }