Repository navigation
Expand file tree
/
Copy pathReflectionHelper.php
More file actions
189 lines (164 loc) · 5.27 KB
/
Copy pathReflectionHelper.php
File metadata and controls
189 lines (164 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
declare(strict_types=1);
namespace Codeception\Util;
use ReflectionException;
use ReflectionMethod;
use ReflectionParameter;
use ReflectionProperty;
use ReflectionType;
use function array_filter;
use function array_keys;
use function array_map;
use function array_pop;
use function count;
use function explode;
use function get_class;
use function implode;
use function in_array;
use function is_array;
use function is_null;
use function is_string;
use function json_encode;
use function method_exists;
use function range;
use function strpos;
use function substr;
use function var_export;
/**
* This class contains helper methods to help with common Reflection tasks.
*/
class ReflectionHelper
{
/**
* Read a private property of an object.
*
* @return mixed
* @throws ReflectionException
*/
public static function readPrivateProperty(object $object, string $property, string $class = null)
{
if (is_null($class)) {
$class = $object;
}
$property = new ReflectionProperty($class, $property);
$property->setAccessible(true);
return $property->getValue($object);
}
/**
* Invoke a private method of an object.
*
* @return mixed
* @throws ReflectionException
*/
public static function invokePrivateMethod(object $object, string $method, array $args = [], string $class = null)
{
if (is_null($class)) {
$class = $object;
}
$method = new ReflectionMethod($class, $method);
$method->setAccessible(true);
return $method->invokeArgs($object, $args);
}
/**
* Returns class name without namespace
*
* (does not use reflection actually)
*/
public static function getClassShortName(object $object): string
{
$path = explode('\\', get_class($object));
return array_pop($path);
}
public static function getClassFromParameter(ReflectionParameter $parameter): ?string
{
$type = $parameter->getType();
if (!$type instanceof ReflectionType || $type->isBuiltin()) {
return null;
}
$typeString = $type->getName();
if ($typeString === 'self') {
return $parameter->getDeclaringClass()->getName();
}
if ($typeString === 'parent') {
return $parameter->getDeclaringClass()->getParentClass()->getName();
}
return $typeString;
}
/**
* Infer default parameter from the reflection object and format it as PHP (code) string
*/
public static function getDefaultValue(ReflectionParameter $parameter): string
{
if ($parameter->isDefaultValueAvailable()) {
if (method_exists($parameter, 'isDefaultValueConstant') && $parameter->isDefaultValueConstant()) {
$constName = $parameter->getDefaultValueConstantName();
if (false !== strpos($constName, '::')) {
[$class, $const] = explode('::', $constName);
if (in_array($class, ['self', 'static'])) {
$constName = '\\' . $parameter->getDeclaringClass()->getName() . '::' . $const;
} elseif (substr($class, 0, 1) !== '\\') {
$constName = '\\' . $constName;
}
}
return $constName;
}
return self::phpEncodeValue($parameter->getDefaultValue());
}
$type = $parameter->getType();
// Default to 'null' if explicitly allowed or there is no specific type hint.
if (!$type || $type->allowsNull() || !$type->isBuiltin()) {
return 'null';
}
// Default value should match the parameter type if 'null' is NOT allowed.
switch ($type->getName()) {
case 'string':
return "''";
case 'array':
return '[]';
case 'boolean':
return 'false';
case 'int':
case 'integer':
case 'float':
case 'double':
case 'number':
case 'numeric':
return '0';
default:
return 'null';
}
}
/**
* PHP encode value
*
* @param mixed $value
*/
public static function phpEncodeValue($value): string
{
if (is_array($value)) {
return self::phpEncodeArray($value);
}
if (is_string($value)) {
return json_encode($value, JSON_THROW_ON_ERROR);
}
return var_export($value, true);
}
/**
* Recursively PHP encode an array
*/
public static function phpEncodeArray(array $array): string
{
$isPlainArray = fn(array $value): bool => ($value === [])
|| (
(array_keys($value) === range(0, count($value) - 1))
&& ([] === array_filter(array_keys($value), 'is_string')));
if ($isPlainArray($array)) {
return '[' . implode(', ', array_map([self::class, 'phpEncodeValue'], $array)) . ']';
}
$values = array_map(
fn($key): string => self::phpEncodeValue($key) . ' => ' . self::phpEncodeValue($array[$key]),
array_keys($array)
);
return '[' . implode(', ', $values) . ']';
}
}