Passed
Pull Request — master (#407)
by Kirill
06:33
created

ReflectionHelperTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 74
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A callNotPublicMethod() 0 8 1
A setNotPublicProperty() 0 8 1
A getNotPublicConst() 0 4 1
A getNotPublicProperty() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tests\Storage\Traits;
6
7
trait ReflectionHelperTrait
8
{
9
    /**
10
     * @template T of object
11
     * @param T|class-string<T> $object
0 ignored issues
show
Documentation Bug introduced by
The doc comment T|class-string<T> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in T|class-string<T>.
Loading history...
12
     * @param string $constant
13
     * @return mixed
14
     * @throws \ReflectionException
15
     *
16
     * @deprecated Tests should not use this method to call internal implementation
17
     */
18
    protected function getNotPublicConst($object, string $constant)
19
    {
20
        return (new \ReflectionClass($object))
21
            ->getConstant($constant)
22
        ;
23
    }
24
25
    /**
26
     * @template T of object
27
     * @param T|class-string<T> $object
0 ignored issues
show
Documentation Bug introduced by
The doc comment T|class-string<T> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in T|class-string<T>.
Loading history...
28
     * @param string $property
29
     * @return mixed
30
     * @throws \ReflectionException
31
     *
32
     * @deprecated Tests should not use this method to call internal implementation
33
     */
34
    protected function getNotPublicProperty($object, string $property)
35
    {
36
        $reflection = new \ReflectionClass($object);
37
38
        $protectedProperty = $reflection->getProperty($property);
39
        $protectedProperty->setAccessible(true);
40
41
        return $protectedProperty->getValue($object);
42
    }
43
44
    /**
45
     * @template T of object
46
     * @param T|class-string<T> $object
0 ignored issues
show
Documentation Bug introduced by
The doc comment T|class-string<T> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in T|class-string<T>.
Loading history...
47
     * @param string $property
48
     * @param mixed $value
49
     * @throws \ReflectionException
50
     *
51
     * @deprecated Tests should not use this method to call internal implementation
52
     */
53
    protected function setNotPublicProperty($object, string $property, $value): void
54
    {
55
        $reflection = new \ReflectionClass($object);
56
57
        $protectedProperty = $reflection->getProperty($property);
58
        $protectedProperty->setAccessible(true);
59
60
        $protectedProperty->setValue($object, $value);
61
    }
62
63
    /**
64
     * @template T of object
65
     * @param T|class-string<T> $object
0 ignored issues
show
Documentation Bug introduced by
The doc comment T|class-string<T> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in T|class-string<T>.
Loading history...
66
     * @param string $method
67
     * @param array $args
68
     * @return mixed
69
     * @throws \ReflectionException
70
     *
71
     * @deprecated Tests should not use this method to call internal implementation
72
     */
73
    protected function callNotPublicMethod($object, string $method, array $args = [])
74
    {
75
        $reflection = new \ReflectionClass($object);
76
77
        $method = $reflection->getMethod($method);
78
        $method->setAccessible(true);
79
80
        return $method->invokeArgs($object, $args);
81
    }
82
}
83