1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Reflection; |
5
|
|
|
|
6
|
|
|
use Innmind\Reflection\{ |
7
|
|
|
InjectionStrategy\InjectionStrategies, |
8
|
|
|
Instanciator\ReflectionInstanciator, |
9
|
|
|
Exception\InvalidArgumentException |
10
|
|
|
}; |
11
|
|
|
use Innmind\Immutable\{ |
12
|
|
|
MapInterface, |
13
|
|
|
Map |
14
|
|
|
}; |
15
|
|
|
|
16
|
|
|
class ReflectionClass |
17
|
|
|
{ |
18
|
|
|
private $class; |
19
|
|
|
private $properties; |
20
|
|
|
private $injectionStrategy; |
21
|
|
|
private $instanciator; |
22
|
|
|
|
23
|
5 |
View Code Duplication |
public function __construct( |
|
|
|
|
24
|
|
|
string $class, |
25
|
|
|
MapInterface $properties = null, |
26
|
|
|
InjectionStrategyInterface $injectionStrategy = null, |
27
|
|
|
InstanciatorInterface $instanciator = null |
28
|
|
|
) { |
29
|
5 |
|
$properties = $properties ?? new Map('string', 'mixed'); |
30
|
|
|
|
31
|
|
|
if ( |
32
|
5 |
|
(string) $properties->keyType() !== 'string' || |
33
|
5 |
|
(string) $properties->valueType() !== 'mixed' |
34
|
|
|
) { |
35
|
|
|
throw new InvalidArgumentException; |
36
|
|
|
} |
37
|
|
|
|
38
|
5 |
|
$this->class = $class; |
39
|
5 |
|
$this->properties = $properties; |
40
|
5 |
|
$this->injectionStrategy = $injectionStrategy ?? InjectionStrategies::default(); |
41
|
5 |
|
$this->instanciator = $instanciator ?? new ReflectionInstanciator; |
42
|
5 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Add a property to be injected in the new object |
46
|
|
|
* |
47
|
|
|
* @param string $property |
48
|
|
|
* @param mixed $value |
49
|
|
|
* |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
2 |
|
public function withProperty(string $property, $value): self |
53
|
|
|
{ |
54
|
2 |
|
return new self( |
55
|
2 |
|
$this->class, |
56
|
2 |
|
$this->properties->put($property, $value), |
|
|
|
|
57
|
2 |
|
$this->injectionStrategy, |
58
|
2 |
|
$this->instanciator |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add a set of properties that need to be injected |
64
|
|
|
* |
65
|
|
|
* @param array<string, mixed> $properties |
66
|
|
|
* |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
1 |
View Code Duplication |
public function withProperties(array $properties): self |
|
|
|
|
70
|
|
|
{ |
71
|
1 |
|
$map = $this->properties; |
72
|
|
|
|
73
|
1 |
|
foreach ($properties as $key => $value) { |
74
|
1 |
|
$map = $map->put($key, $value); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
return new self( |
78
|
1 |
|
$this->class, |
79
|
|
|
$map, |
80
|
1 |
|
$this->injectionStrategy, |
81
|
1 |
|
$this->instanciator |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return the collection of properties that will be injected in the object |
87
|
|
|
* |
88
|
|
|
* @return MapInterface<string, mixed> |
|
|
|
|
89
|
|
|
*/ |
90
|
1 |
|
public function properties(): MapInterface |
91
|
|
|
{ |
92
|
1 |
|
return $this->properties; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return the list of injection strategies used |
97
|
|
|
* |
98
|
|
|
* @return InjectionStrategiesInterface |
99
|
|
|
*/ |
100
|
1 |
|
public function injectionStrategy(): InjectionStrategyInterface |
101
|
|
|
{ |
102
|
1 |
|
return $this->injectionStrategy; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return the object instanciator |
107
|
|
|
* |
108
|
|
|
* @return InstanciatorInterface |
109
|
|
|
*/ |
110
|
1 |
|
public function instanciator(): InstanciatorInterface |
111
|
|
|
{ |
112
|
1 |
|
return $this->instanciator; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return a new instance of the class |
117
|
|
|
* |
118
|
|
|
* @return object |
119
|
|
|
*/ |
120
|
2 |
|
public function build() |
121
|
|
|
{ |
122
|
2 |
|
$object = $this->instanciator->build($this->class, $this->properties); |
123
|
2 |
|
$parameters = $this->instanciator->parameters($this->class); |
124
|
|
|
|
125
|
|
|
//avoid injecting the properties already used in the constructor |
126
|
|
|
$properties = $this |
127
|
2 |
|
->properties |
128
|
2 |
|
->filter(function(string $property) use ($parameters) { |
129
|
1 |
|
return !$parameters->contains($property); |
|
|
|
|
130
|
2 |
|
}); |
131
|
2 |
|
$refl = new ReflectionObject( |
132
|
|
|
$object, |
133
|
|
|
$properties, |
134
|
2 |
|
$this->injectionStrategy |
135
|
|
|
); |
136
|
|
|
|
137
|
2 |
|
return $refl->build(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
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.