1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hgraca\Helper; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Hgraca\Helper\Concept\ReflectionHelperAbstract; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use ReflectionFunction; |
9
|
|
|
use ReflectionMethod; |
10
|
|
|
use ReflectionProperty; |
11
|
|
|
|
12
|
|
|
final class InstanceHelper extends ReflectionHelperAbstract |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param mixed $object |
16
|
|
|
* |
17
|
|
|
* @return ReflectionProperty[] |
18
|
|
|
*/ |
19
|
8 |
|
public static function getReflectionProperties($object): array |
20
|
|
|
{ |
21
|
8 |
|
return ClassHelper::getReflectionProperties(get_class($object)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return mixed |
26
|
|
|
*/ |
27
|
1 |
|
public static function createInstance(string $fqcn, array $constructorArguments = []) |
28
|
|
|
{ |
29
|
1 |
|
$reflectionClass = self::getReflectionClass($fqcn); |
30
|
|
|
|
31
|
1 |
|
return $reflectionClass->newInstanceArgs($constructorArguments); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
3 |
|
public static function createInstanceWithoutConstructor(string $fqcn) |
38
|
|
|
{ |
39
|
3 |
|
$reflectionClass = self::getReflectionClass($fqcn); |
40
|
|
|
|
41
|
3 |
|
return $reflectionClass->newInstanceWithoutConstructor(); |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
View Code Duplication |
public static function setProtectedProperty($object, string $propertyName, $value) |
|
|
|
|
45
|
|
|
{ |
46
|
3 |
|
$class = self::getReflectionClass(get_class($object)); |
47
|
|
|
|
48
|
3 |
|
$property = ClassHelper::getReflectionProperty($class, $propertyName); |
49
|
2 |
|
$property->setAccessible(true); |
50
|
2 |
|
$property->setValue($object, $value); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
3 |
View Code Duplication |
public static function getProtectedProperty($object, string $propertyName) |
|
|
|
|
54
|
|
|
{ |
55
|
3 |
|
$class = self::getReflectionClass(get_class($object)); |
56
|
|
|
|
57
|
3 |
|
$property = ClassHelper::getReflectionProperty($class, $propertyName); |
58
|
2 |
|
$property->setAccessible(true); |
59
|
|
|
|
60
|
2 |
|
return $property->getValue($object); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param callable $input Can be a callable array (method defaults to '__construct'), callable object or Closure |
65
|
|
|
* |
66
|
|
|
* @throws \InvalidArgumentException |
67
|
|
|
* |
68
|
|
|
* @return array [index => [name, class]] |
69
|
|
|
*/ |
70
|
4 |
|
public static function getParameters($input): array |
71
|
|
|
{ |
72
|
4 |
|
if (is_array($input)) { |
73
|
1 |
|
$dependentClass = is_string($input[0]) ? $input[0] : get_class($input[0]); |
74
|
1 |
|
$dependentMethod = $input[1] ?? '__construct'; |
75
|
1 |
|
$reflectionMethod = new ReflectionMethod($dependentClass, $dependentMethod); |
76
|
|
|
} elseif ($input instanceof Closure) { |
77
|
1 |
|
$reflectionMethod = new ReflectionFunction($input); |
78
|
2 |
|
} elseif (is_callable($input)) { |
79
|
1 |
|
$reflectionMethod = new ReflectionMethod(get_class($input), '__invoke'); |
80
|
|
|
} else { |
81
|
1 |
|
throw new InvalidArgumentException('$input needs to be a callable'); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
View Code Duplication |
foreach ($reflectionMethod->getParameters() as $index => $param) { |
|
|
|
|
85
|
3 |
|
$reflectionParameters[$index]['name'] = $param->getName(); |
|
|
|
|
86
|
3 |
|
if (null !== $param->getClass()) { |
87
|
3 |
|
$reflectionParameters[$index]['class'] = $param->getClass()->name; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
3 |
|
return $reflectionParameters ?? []; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.