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