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 |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|