summaryrefslogtreecommitdiff
path: root/vendor/doctrine/persistence/src/Persistence/Mapping/RuntimeReflectionService.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/persistence/src/Persistence/Mapping/RuntimeReflectionService.php')
-rw-r--r--vendor/doctrine/persistence/src/Persistence/Mapping/RuntimeReflectionService.php111
1 files changed, 111 insertions, 0 deletions
diff --git a/vendor/doctrine/persistence/src/Persistence/Mapping/RuntimeReflectionService.php b/vendor/doctrine/persistence/src/Persistence/Mapping/RuntimeReflectionService.php
new file mode 100644
index 0000000..399e057
--- /dev/null
+++ b/vendor/doctrine/persistence/src/Persistence/Mapping/RuntimeReflectionService.php
@@ -0,0 +1,111 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\Persistence\Mapping;
6
7use Doctrine\Persistence\Reflection\RuntimeReflectionProperty;
8use Doctrine\Persistence\Reflection\TypedNoDefaultReflectionProperty;
9use ReflectionClass;
10use ReflectionException;
11use ReflectionMethod;
12
13use function array_key_exists;
14use function assert;
15use function class_exists;
16use function class_parents;
17use function phpversion;
18use function version_compare;
19
20/**
21 * PHP Runtime Reflection Service.
22 */
23class RuntimeReflectionService implements ReflectionService
24{
25 /** @var bool */
26 private $supportsTypedPropertiesWorkaround;
27
28 public function __construct()
29 {
30 $this->supportsTypedPropertiesWorkaround = version_compare(phpversion(), '7.4.0') >= 0;
31 }
32
33 /**
34 * {@inheritDoc}
35 */
36 public function getParentClasses(string $class)
37 {
38 if (! class_exists($class)) {
39 throw MappingException::nonExistingClass($class);
40 }
41
42 $parents = class_parents($class);
43
44 assert($parents !== false);
45
46 return $parents;
47 }
48
49 /**
50 * {@inheritDoc}
51 */
52 public function getClassShortName(string $class)
53 {
54 $reflectionClass = new ReflectionClass($class);
55
56 return $reflectionClass->getShortName();
57 }
58
59 /**
60 * {@inheritDoc}
61 */
62 public function getClassNamespace(string $class)
63 {
64 $reflectionClass = new ReflectionClass($class);
65
66 return $reflectionClass->getNamespaceName();
67 }
68
69 /**
70 * @psalm-param class-string<T> $class
71 *
72 * @return ReflectionClass
73 * @psalm-return ReflectionClass<T>
74 *
75 * @template T of object
76 */
77 public function getClass(string $class)
78 {
79 return new ReflectionClass($class);
80 }
81
82 /**
83 * {@inheritDoc}
84 */
85 public function getAccessibleProperty(string $class, string $property)
86 {
87 $reflectionProperty = new RuntimeReflectionProperty($class, $property);
88
89 if ($this->supportsTypedPropertiesWorkaround && ! array_key_exists($property, $this->getClass($class)->getDefaultProperties())) {
90 $reflectionProperty = new TypedNoDefaultReflectionProperty($class, $property);
91 }
92
93 $reflectionProperty->setAccessible(true);
94
95 return $reflectionProperty;
96 }
97
98 /**
99 * {@inheritDoc}
100 */
101 public function hasPublicMethod(string $class, string $method)
102 {
103 try {
104 $reflectionMethod = new ReflectionMethod($class, $method);
105 } catch (ReflectionException $e) {
106 return false;
107 }
108
109 return $reflectionMethod->isPublic();
110 }
111}