ReflectionHelper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 95
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getClassFilename() 0 5 1
A getProtectedProperty() 0 8 1
A getStaticProtectedProperty() 0 5 1
A getClassFilemtime() 0 5 1
A setProtectedProperty() 0 7 1
A getReflectionProperty() 0 11 3
A instantiateWithoutConstructor() 0 5 1
A setStaticProtectedProperty() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hgraca\DoctrineTestDbRegenerationBundle\StdLib;
6
7
use ReflectionClass;
8
use ReflectionException;
9
use ReflectionProperty;
10
11
final class ReflectionHelper extends AbstractStaticClass
12
{
13
    /**
14
     * @throws ReflectionException
15
     */
16 18
    public static function getProtectedProperty($object, string $propertyName)
17
    {
18 18
        $class = new ReflectionClass(\get_class($object));
19
20 18
        $property = static::getReflectionProperty($class, $propertyName);
21 16
        $property->setAccessible(true);
22
23 16
        return $property->getValue($object);
24
    }
25
26
    /**
27
     * @throws ReflectionException
28
     */
29 12
    public static function setProtectedProperty($object, string $propertyName, $value): void
30
    {
31 12
        $class = new ReflectionClass(\get_class($object));
32
33 12
        $property = static::getReflectionProperty($class, $propertyName);
34 10
        $property->setAccessible(true);
35 10
        $property->setValue($object, $value);
36 10
    }
37
38
    /**
39
     * @throws ReflectionException
40
     *
41
     * @return mixed
42
     */
43 2
    public static function instantiateWithoutConstructor(string $classFqcn)
44
    {
45 2
        $class = new ReflectionClass($classFqcn);
46
47 2
        return $class->newInstanceWithoutConstructor();
48
    }
49
50
    /**
51
     * @throws ReflectionException
52
     */
53 6
    public static function getClassFilename(string $classFqcn): string
54
    {
55 6
        $class = new ReflectionClass($classFqcn);
56
57 6
        return $class->getFileName();
58
    }
59
60
    /**
61
     * @throws ReflectionException
62
     */
63 4
    public static function getClassFilemtime(string $classFqcn): int
64
    {
65 4
        $class = new ReflectionClass($classFqcn);
66
67 4
        return filemtime($class->getFileName());
68
    }
69
70
    /**
71
     * @throws ReflectionException
72
     */
73 4
    public static function getStaticProtectedProperty(string $classFqcn, string $propertyName)
74
    {
75 4
        $class = new ReflectionClass($classFqcn);
76
77 4
        return $class->getStaticProperties()[$propertyName];
78
    }
79
80
    /**
81
     * @throws ReflectionException
82
     */
83 4
    public static function setStaticProtectedProperty(string $classFqcn, string $propertyName, $value): void
84
    {
85 4
        $class = new ReflectionClass($classFqcn);
86
87 4
        $reflectedProperty = $class->getProperty($propertyName);
88 4
        $reflectedProperty->setAccessible(true);
89 4
        $reflectedProperty->setValue($value);
90 4
    }
91
92
    /**
93
     * @throws ReflectionException
94
     */
95 24
    private static function getReflectionProperty(ReflectionClass $class, string $propertyName): ReflectionProperty
96
    {
97
        try {
98 24
            return $class->getProperty($propertyName);
99 14
        } catch (ReflectionException $e) {
100 14
            $parentClass = $class->getParentClass();
101 14
            if ($parentClass === false) {
102 4
                throw $e;
103
            }
104
105 14
            return static::getReflectionProperty($parentClass, $propertyName);
106
        }
107
    }
108
}
109