These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | declare(strict_types = 1); |
||
3 | |||
4 | namespace Innmind\Reflection; |
||
5 | |||
6 | use Innmind\Reflection\Exception\InvalidArgumentException; |
||
7 | use Innmind\Reflection\Exception\LogicException; |
||
8 | use Innmind\Immutable\Collection; |
||
9 | use Innmind\Immutable\CollectionInterface; |
||
10 | use Innmind\Immutable\TypedCollectionInterface; |
||
11 | |||
12 | class ReflectionObject |
||
13 | { |
||
14 | private $object; |
||
15 | private $properties; |
||
16 | private $injectionStrategies; |
||
17 | private $extractionStrategies; |
||
18 | |||
19 | 13 | public function __construct( |
|
20 | $object, |
||
21 | CollectionInterface $properties = null, |
||
22 | TypedCollectionInterface $injectionStrategies = null, |
||
23 | TypedCollectionInterface $extractionStrategies = null |
||
24 | ) { |
||
25 | 13 | if (!is_object($object)) { |
|
26 | 1 | throw new InvalidArgumentException; |
|
27 | } |
||
28 | |||
29 | 12 | $injectionStrategies = $injectionStrategies ?? InjectionStrategies::defaults(); |
|
30 | 12 | $extractionStrategies = $extractionStrategies ?? ExtractionStrategies::defaults(); |
|
31 | |||
32 | 12 | if ($injectionStrategies->getType() !== InjectionStrategyInterface::class) { |
|
33 | throw new InvalidArgumentException; |
||
34 | } |
||
35 | |||
36 | 12 | if ($extractionStrategies->getType() !== ExtractionStrategyInterface::class) { |
|
37 | throw new InvalidArgumentException; |
||
38 | } |
||
39 | |||
40 | 12 | $this->object = $object; |
|
41 | |||
42 | 12 | $this->properties = $properties ?? new Collection([]); |
|
43 | 12 | $this->injectionStrategies = $injectionStrategies; |
|
44 | 12 | $this->extractionStrategies = $extractionStrategies; |
|
45 | 12 | } |
|
46 | |||
47 | /** |
||
48 | * Add a property that will be injected |
||
49 | * |
||
50 | * @param string $name |
||
51 | * @param mixed $value |
||
52 | * |
||
53 | * @return self |
||
54 | */ |
||
55 | 4 | public function withProperty(string $name, $value) |
|
56 | { |
||
57 | 4 | return new self( |
|
58 | 4 | $this->object, |
|
59 | 4 | $this->properties->set($name, $value), |
|
60 | 4 | $this->injectionStrategies, |
|
61 | 4 | $this->extractionStrategies |
|
62 | ); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Add a set of properties that need to be injected |
||
67 | * |
||
68 | * @param array $properties |
||
69 | * |
||
70 | * @return self |
||
71 | */ |
||
72 | 1 | public function withProperties(array $properties): self |
|
73 | { |
||
74 | 1 | return new self( |
|
75 | 1 | $this->object, |
|
76 | 1 | $this->properties->merge(new Collection($properties)), |
|
77 | 1 | $this->injectionStrategies, |
|
78 | 1 | $this->extractionStrategies |
|
79 | ); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Return the collection of properties that will be injected in the object |
||
84 | * |
||
85 | * @return CollectionInterface |
||
86 | */ |
||
87 | 2 | public function getProperties(): CollectionInterface |
|
88 | { |
||
89 | 2 | return $this->properties; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Return the list of injection strategies used |
||
94 | * |
||
95 | * @return TypedCollectionInterface |
||
96 | */ |
||
97 | 1 | public function getInjectionStrategies(): TypedCollectionInterface |
|
98 | { |
||
99 | 1 | return $this->injectionStrategies; |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * Return the list of extraction strategies used |
||
104 | * |
||
105 | * @return TypedCollectionInterface |
||
106 | */ |
||
107 | 1 | public function getExtractionStrategies(): TypedCollectionInterface |
|
108 | { |
||
109 | 1 | return $this->extractionStrategies; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * Return the object with the list of properties set on it |
||
114 | * |
||
115 | * @return object |
||
116 | */ |
||
117 | 7 | public function buildObject() |
|
118 | { |
||
119 | 7 | foreach ($this->properties as $key => $value) { |
|
120 | 5 | $this->inject($key, $value); |
|
121 | } |
||
122 | |||
123 | 5 | return $this->object; |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * Extract the given list of properties |
||
128 | * |
||
129 | * @param array $properties |
||
130 | * |
||
131 | * @return CollectionInterface |
||
132 | */ |
||
133 | 2 | public function extract(array $properties): CollectionInterface |
|
134 | { |
||
135 | 2 | $values = []; |
|
136 | |||
137 | 2 | foreach ($properties as $property) { |
|
138 | 2 | $values[$property] = $this->extractProperty($property); |
|
139 | } |
||
140 | |||
141 | 1 | return new Collection($values); |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Inject the given key/value pair into the object |
||
146 | * |
||
147 | * @param string $key |
||
148 | * @param mixed $value |
||
149 | * |
||
150 | * @return void |
||
151 | */ |
||
152 | 5 | View Code Duplication | private function inject(string $key, $value) |
0 ignored issues
–
show
|
|||
153 | { |
||
154 | 5 | foreach ($this->injectionStrategies as $strategy) { |
|
155 | 5 | if ($strategy->supports($this->object, $key, $value)) { |
|
156 | 3 | $strategy->inject($this->object, $key, $value); |
|
157 | |||
158 | 5 | return; |
|
159 | } |
||
160 | } |
||
161 | |||
162 | 2 | throw new LogicException(sprintf( |
|
163 | 2 | 'Property "%s" cannot be injected', |
|
164 | $key |
||
165 | )); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Extract the given property out of the object |
||
170 | * |
||
171 | * @param string $property |
||
172 | * |
||
173 | * @return mixed |
||
174 | */ |
||
175 | 2 | View Code Duplication | private function extractProperty(string $property) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
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. ![]() |
|||
176 | { |
||
177 | 2 | foreach ($this->extractionStrategies as $strategy) { |
|
178 | 2 | if ($strategy->supports($this->object, $property)) { |
|
179 | 2 | return $strategy->extract($this->object, $property); |
|
180 | } |
||
181 | } |
||
182 | |||
183 | 1 | throw new LogicException(sprintf( |
|
184 | 1 | 'Property "%s" cannot be extracted', |
|
185 | $property |
||
186 | )); |
||
187 | } |
||
188 | } |
||
189 |
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.