1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hgraca\Helper; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Hgraca\Helper\Concept\ReflectionHelperAbstract; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use ReflectionException; |
9
|
|
|
use ReflectionFunction; |
10
|
|
|
use ReflectionMethod; |
11
|
|
|
use ReflectionProperty; |
12
|
|
|
|
13
|
|
|
final class InstanceHelper extends ReflectionHelperAbstract |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param mixed $object |
17
|
|
|
* |
18
|
|
|
* @return ReflectionProperty[] |
19
|
|
|
*/ |
20
|
8 |
|
public static function getReflectionProperties($object): array |
21
|
|
|
{ |
22
|
8 |
|
return ClassHelper::getReflectionProperties(get_class($object)); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return mixed |
27
|
|
|
*/ |
28
|
1 |
|
public static function createInstance(string $fqcn, array $constructorArguments = []) |
29
|
|
|
{ |
30
|
1 |
|
$reflectionClass = self::getReflectionClass($fqcn); |
31
|
|
|
|
32
|
1 |
|
return $reflectionClass->newInstanceArgs($constructorArguments); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
3 |
|
public static function createInstanceWithoutConstructor(string $fqcn) |
39
|
|
|
{ |
40
|
3 |
|
$reflectionClass = self::getReflectionClass($fqcn); |
41
|
|
|
|
42
|
3 |
|
return $reflectionClass->newInstanceWithoutConstructor(); |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
View Code Duplication |
public static function setProtectedProperty($object, string $propertyName, $value) |
46
|
|
|
{ |
47
|
3 |
|
$class = self::getReflectionClass(get_class($object)); |
48
|
|
|
|
49
|
3 |
|
$property = ClassHelper::getReflectionProperty($class, $propertyName); |
50
|
2 |
|
$property->setAccessible(true); |
51
|
2 |
|
$property->setValue($object, $value); |
52
|
2 |
|
} |
53
|
|
|
|
54
|
3 |
View Code Duplication |
public static function getProtectedProperty($object, string $propertyName) |
55
|
|
|
{ |
56
|
3 |
|
$class = self::getReflectionClass(get_class($object)); |
57
|
|
|
|
58
|
3 |
|
$property = ClassHelper::getReflectionProperty($class, $propertyName); |
59
|
2 |
|
$property->setAccessible(true); |
60
|
|
|
|
61
|
2 |
|
return $property->getValue($object); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param callable $input Can be a callable array (method defaults to '__construct'), callable object or Closure |
66
|
|
|
* |
67
|
|
|
* @throws InvalidArgumentException |
68
|
|
|
* |
69
|
|
|
* @return array [index => [name, class]] |
70
|
|
|
*/ |
71
|
4 |
View Code Duplication |
public static function getParameters($input): array |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
4 |
|
$reflectionMethod = self::getCallableReflectionMethod($input); |
75
|
1 |
|
} catch (ReflectionException $e) { |
76
|
|
|
return []; |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
foreach ($reflectionMethod->getParameters() as $index => $param) { |
80
|
3 |
|
$reflectionParameters[$index]['name'] = $param->getName(); |
|
|
|
|
81
|
3 |
|
if (null !== $param->getClass()) { |
82
|
3 |
|
$reflectionParameters[$index]['class'] = $param->getClass()->name; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
return $reflectionParameters ?? []; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @throws InvalidArgumentException |
91
|
|
|
* |
92
|
|
|
* @return ReflectionFunction|ReflectionMethod |
93
|
|
|
*/ |
94
|
4 |
|
private static function getCallableReflectionMethod($input) |
95
|
|
|
{ |
96
|
4 |
|
if (is_array($input)) { |
97
|
1 |
|
$dependentClass = is_string($input[0]) ? $input[0] : get_class($input[0]); |
98
|
1 |
|
$dependentMethod = $input[1] ?? '__construct'; |
99
|
|
|
|
100
|
1 |
|
return new ReflectionMethod($dependentClass, $dependentMethod); |
101
|
|
|
} elseif ($input instanceof Closure) { |
102
|
|
|
|
103
|
1 |
|
return new ReflectionFunction($input); |
104
|
2 |
|
} elseif (is_callable($input)) { |
105
|
|
|
|
106
|
1 |
|
return new ReflectionMethod(get_class($input), '__invoke'); |
107
|
|
|
} else { |
108
|
1 |
|
throw new InvalidArgumentException('$input needs to be a callable'); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.