Passed
Push — 1.3.x-merge-up-into-1.4.x_5e21... ( 5dd3ac )
by
unknown
31:54 queued 15:53
created

RuntimeReflectionService::getClassShortName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Doctrine\Persistence\Mapping;
4
5
use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
6
use Doctrine\Common\Reflection\TypedNoDefaultReflectionProperty;
7
use ReflectionClass;
8
use ReflectionException;
9
use ReflectionMethod;
10
use ReflectionProperty;
11
use function array_key_exists;
12
use function class_exists;
13
use function class_parents;
14
use function phpversion;
15
use function version_compare;
16
17
/**
18
 * PHP Runtime Reflection Service.
19
 */
20
class RuntimeReflectionService implements ReflectionService
21
{
22
    /** @var bool */
23
    private $supportsTypedPropertiesWorkaround;
24
25 7
    public function __construct()
26
    {
27 7
        $this->supportsTypedPropertiesWorkaround = version_compare((string) phpversion(), '7.4.0') >= 0;
28 7
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33 2
    public function getParentClasses($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($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($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($class)
66
    {
67 1
        return new ReflectionClass($class);
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73 1
    public function getAccessibleProperty($class, $property)
74
    {
75 1
        $reflectionProperty = new ReflectionProperty($class, $property);
76
77 1
        if ($reflectionProperty->isPublic()) {
78 1
            $reflectionProperty = new RuntimePublicReflectionProperty($class, $property);
79 1
        } elseif ($this->supportsTypedPropertiesWorkaround && ! array_key_exists($property, $this->getClass($class)->getDefaultProperties())) {
80
            $reflectionProperty = new TypedNoDefaultReflectionProperty($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($class, $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
103
class_exists(\Doctrine\Common\Persistence\Mapping\RuntimeReflectionService::class);
104