Passed
Push — add_typed_no_default_reflectio... ( d3ea32 )
by Benjamin
04:52
created

RuntimeReflectionService   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
eloc 23
c 2
b 0
f 0
dl 0
loc 80
ccs 28
cts 29
cp 0.9655
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getClass() 0 3 1
A getParentClasses() 0 7 2
A hasPublicMethod() 0 9 2
A getClassNamespace() 0 5 1
A __construct() 0 4 1
A getClassShortName() 0 5 1
A getAccessibleProperty() 0 13 4
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
17
/**
18
 * PHP Runtime Reflection Service.
19
 */
20
class RuntimeReflectionService implements ReflectionService
21
{
22
    private $supportsTypedPropertiesWorkaround;
0 ignored issues
show
introduced by
Property \Doctrine\Persistence\Mapping\RuntimeReflectionService::$supportsTypedPropertiesWorkaround does not have @var annotation.
Loading history...
23
24 7
    public function __construct()
25
    {
26 7
        $this->supportsTypedPropertiesWorkaround =
27 7
            version_compare(phpversion(), '7.4.0') >= 0;
0 ignored issues
show
introduced by
Function version_compare() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Function phpversion() should not be referenced via a fallback global name, but via a use statement.
Loading history...
28 7
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33 2
    public function getParentClasses(string $class)
34
    {
35 2
        if (! class_exists($class)) {
36 1
            throw MappingException::nonExistingClass($class);
37
        }
38
39 1
        return class_parents($class);
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 1
    public function getClassShortName(string $class)
46
    {
47 1
        $reflectionClass = new ReflectionClass($class);
48
49 1
        return $reflectionClass->getShortName();
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55 1
    public function getClassNamespace(string $class)
56
    {
57 1
        $reflectionClass = new ReflectionClass($class);
58
59 1
        return $reflectionClass->getNamespaceName();
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65 1
    public function getClass(string $class)
66
    {
67 1
        return new ReflectionClass($class);
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73 1
    public function getAccessibleProperty(string $class, string $property)
74
    {
75 1
        $reflectionProperty = new ReflectionProperty($class, $property);
76
77 1
        if ($this->supportsTypedPropertiesWorkaround && ! array_key_exists($property, $this->getClass($class)->getDefaultProperties())) {
78
            $reflectionProperty = new TypedNoDefaultReflectionProperty($class, $property);
79 1
        } elseif ($reflectionProperty->isPublic()) {
80 1
            $reflectionProperty = new RuntimePublicReflectionProperty($class, $property);
81
        }
82
83 1
        $reflectionProperty->setAccessible(true);
84
85 1
        return $reflectionProperty;
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 1
    public function hasPublicMethod(string $class, string $method)
92
    {
93
        try {
94 1
            $reflectionMethod = new ReflectionMethod($class, $method);
95 1
        } catch (ReflectionException $e) {
96 1
            return false;
97
        }
98
99 1
        return $reflectionMethod->isPublic();
100
    }
101
}
102