RuntimeReflectionService::getClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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