Failed Conditions
Pull Request — master (#94)
by Benjamin
05:33 queued 02:43
created

RuntimeReflectionService   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
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 81
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
0 ignored issues
show
introduced by
An error occurred during processing; checking has been aborted. The error message was: "else" without curly braces is not supported.
Loading history...
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 7
    public function __construct()
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function __construct()
Loading history...
28
    {
29 7
        $this->supportsTypedPropertiesWorkaround =
30 7
            version_compare(phpversion(), '7.4.0') >= 0;
31 7
    }
32
33
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$class" missing
Loading history...
34
     * {@inheritDoc}
35
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
36 2
    public function getParentClasses(string $class)
37
    {
38 2
        if (! class_exists($class)) {
39 1
            throw MappingException::nonExistingClass($class);
40
        }
41
42 1
        return class_parents($class);
43
    }
44
45
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$class" missing
Loading history...
46
     * {@inheritDoc}
47
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
48 1
    public function getClassShortName(string $class)
49
    {
50 1
        $reflectionClass = new ReflectionClass($class);
51
52 1
        return $reflectionClass->getShortName();
53
    }
54
55
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$class" missing
Loading history...
56
     * {@inheritDoc}
57
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
58 1
    public function getClassNamespace(string $class)
59
    {
60 1
        $reflectionClass = new ReflectionClass($class);
61
62 1
        return $reflectionClass->getNamespaceName();
63
    }
64
65
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$class" missing
Loading history...
66
     * {@inheritDoc}
67
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
68 1
    public function getClass(string $class)
69
    {
70 1
        return new ReflectionClass($class);
71
    }
72
73
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$class" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$property" missing
Loading history...
74
     * {@inheritDoc}
75
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
76 1
    public function getAccessibleProperty(string $class, string $property)
77
    {
78 1
        $reflectionProperty = new ReflectionProperty($class, $property);
79
80 1
        if ($reflectionProperty->isPublic()) {
81 1
            $reflectionProperty = new RuntimePublicReflectionProperty($class, $property);
82 1
        } else if ($this->supportsTypedPropertiesWorkaround && ! array_key_exists($property, $this->getClass($class)->getDefaultProperties())) {
83
            $reflectionProperty = new TypedNoDefaultReflectionProperty($class, $property);
84
        }
85
86 1
        $reflectionProperty->setAccessible(true);
87
88 1
        return $reflectionProperty;
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94 1
    public function hasPublicMethod(string $class, string $method)
95
    {
96
        try {
97 1
            $reflectionMethod = new ReflectionMethod($class, $method);
98 1
        } catch (ReflectionException $e) {
99 1
            return false;
100
        }
101
102 1
        return $reflectionMethod->isPublic();
103
    }
104
}
105