Completed
Pull Request — 1.3.x (#71)
by Grégoire
13:22 queued 11:22
created

RuntimeReflectionService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 18
dl 0
loc 70
rs 10
c 0
b 0
f 0
ccs 23
cts 23
cp 1

6 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 getClassShortName() 0 5 1
A getAccessibleProperty() 0 11 2
1
<?php
2
3
namespace Doctrine\Persistence\Mapping;
4
5
use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
6
use ReflectionClass;
7
use ReflectionException;
8
use ReflectionMethod;
9
use ReflectionProperty;
10
use function class_exists;
11
use function class_parents;
12
use Doctrine\Persistence\Mapping\MappingException;
0 ignored issues
show
Coding Style introduced by
Use statements should be sorted alphabetically. The first wrong one is Doctrine\Persistence\Mapping\MappingException.
Loading history...
introduced by
Use Doctrine\Persistence\Mapping\MappingException is from the same namespace – that is prohibited.
Loading history...
13
use Doctrine\Persistence\Mapping\ReflectionService;
0 ignored issues
show
introduced by
Use Doctrine\Persistence\Mapping\ReflectionService is from the same namespace – that is prohibited.
Loading history...
14
15
/**
16
 * PHP Runtime Reflection Service.
17
 */
18
class RuntimeReflectionService implements ReflectionService
19
{
20
    /**
21
     * {@inheritDoc}
22
     */
23 11
    public function getParentClasses($class)
24
    {
25 11
        if (! class_exists($class)) {
26 6
            throw MappingException::nonExistingClass($class);
27
        }
28
29 5
        return class_parents($class);
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35 1
    public function getClassShortName($class)
36
    {
37 1
        $reflectionClass = new ReflectionClass($class);
38
39 1
        return $reflectionClass->getShortName();
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 1
    public function getClassNamespace($class)
46
    {
47 1
        $reflectionClass = new ReflectionClass($class);
48
49 1
        return $reflectionClass->getNamespaceName();
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55 1
    public function getClass($class)
56
    {
57 1
        return new ReflectionClass($class);
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63 1
    public function getAccessibleProperty($class, $property)
64
    {
65 1
        $reflectionProperty = new ReflectionProperty($class, $property);
66
67 1
        if ($reflectionProperty->isPublic()) {
68 1
            $reflectionProperty = new RuntimePublicReflectionProperty($class, $property);
69
        }
70
71 1
        $reflectionProperty->setAccessible(true);
72
73 1
        return $reflectionProperty;
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79 1
    public function hasPublicMethod($class, $method)
80
    {
81
        try {
82 1
            $reflectionMethod = new ReflectionMethod($class, $method);
83 1
        } catch (ReflectionException $e) {
84 1
            return false;
85
        }
86
87 1
        return $reflectionMethod->isPublic();
88
    }
89
}
90